summaryrefslogtreecommitdiffstats
path: root/clock.c
diff options
context:
space:
mode:
Diffstat (limited to 'clock.c')
-rw-r--r--clock.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/clock.c b/clock.c
new file mode 100644
index 0000000..66b278b
--- /dev/null
+++ b/clock.c
@@ -0,0 +1,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.
+ }
+}