summaryrefslogtreecommitdiffstats
path: root/clock.c
blob: 66b278b674daa008c28925a3b88bdb84aee68862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Light_WS2812 library example - RGB_blinky
*
* cycles one LED through red, green, blue
*
* This example is configured for a ATtiny85 with PLL clock fuse set and
* the WS2812 string connected to PB4.
*/

#include "project.h"


#define N_COLORS (288*2)
#define N_LEDS 24


struct RGB led[N_LEDS];



static uint32_t ticks;          /* 4 ticks per second */


static void
do_hand (int w, uint8_t r, uint8_t g, uint8_t b)
{
  int i, j;

  for (i = 0; w >= 50; ++i, w -= 50);

  i %= N_LEDS;
  j = i + 1;
  j %= N_LEDS;

  led[i].r += (r * (49 - w)) / 49;
  led[i].g += (g * (49 - w)) / 49;
  led[i].b += (b * (49 - w)) / 49;

  led[j].r += (r * w) / 49;
  led[j].g += (g * w) / 49;
  led[j].b += (b * w) / 49;

}


static void
tick (void)
{
  uint32_t r;
  ticks++;



  if ((ticks%20)<10)  {
  	PORTD&=~(1<<5);
  } else {
  	PORTD|=1<<5;
 }


  memset (led, 0, sizeof (led));

  r = ticks % 1200;

  do_hand (r, 255, 0, 0);

  r = (ticks / 60) % 1200;

  do_hand (r, 0, 255, 0);

  r = (ticks / 720) % 1200;

  

  do_hand (r, 0, 0, 255);

  ws2812_setleds (led, N_LEDS);

}

static void off(void)
{
  memset (led, 0, sizeof (led));
  ws2812_setleds (led, N_LEDS);
}

int get_int(char *buf)
{
int r;

r=buf[0]-'0';
r*=10;
r+=buf[1]-'0';

return r;
}


static void command_dispatch(char *buf)
{
uint32_t r=ticks/20;
int h,m,s;

if ((buf[2]==':') && (buf[5]==':')) {
	h=get_int(buf);
	m=get_int(buf+3);
	s=get_int(buf+6);

	r=h;
	r*=60;
	r+=m;
	r*=60;
	r+=s;
	ticks=r*20;
}


h=r/3600;
r -= h*3600;
m=r/60;
r -= m*60;
s=r;
printf("%02d.%02d.%02d\n",h,m,s);
}



static void serial_dispatch(void)
{
static char buf[64];
static int bptr=0;

int r=usb_serial_getchar();
if (r==-1) return;


if (r==10) {
	buf[bptr]=0;
	command_dispatch(buf);
	bptr=0;
	return;
}

buf[bptr++]=r;

if (bptr==(sizeof(buf) -1 )) {
	bptr=0;
	return;
}
}



int
main (void)
{
  int i, j, k;

  setup_clocks ();
  off();
  stdio_init ();


  DDRD|=1<<5;

  ticks=0;
  ticks*=60;
  ticks+=30;
  ticks*=60;
  ticks+=15;
  ticks*=20;




  while (1)
    {
      tick ();
      serial_dispatch();
      _delay_ms (50);          // wait for 500ms.
    }
}