/* Analogue Clock David Johnson-Davies - www.technoblogy.com - 27th August 2014 ATtiny85 @ 8 MHz (external crystal; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ // Constants const int Delta = 9; // Approximation to 1 degree in radians * 2^9 const int Ss = 2; const int Clk = 1; const int Mosi = 0; const int Reset = 9; //Create global variables unsigned long Time; int Factor; // Current plot position int x0; int y0; // Screen buffer - only stores 16 lines int Slice; unsigned char Buffer[12][16]; // Plot point x,y (in range -48 to 47) into buffer if in current slice void PlotPoint(int x, int y) { int row = 47 - y; int col = x + 48; // Set correct bit in slice buffer if ((row>>4) == Slice) Buffer[col>>3][row & 0x0F] |= 1<<(col & 0x07); } // Move current plot position to x1,y1 void MoveTo(int x1, int y1) { x0 = x1; y0 = y1; } // Draw a line to x1,y1 void DrawTo(int x1, int y1) { int sx, sy, e2, err; int dx = abs(x1 - x0); int dy = abs(y1 - y0); if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; err = dx - dy; for (;;) { PlotPoint(x0, y0); if (x0==x1 && y0==y1) return; e2 = err<<1; if (e2 > -dy) { err = err - dy; x0 = x0 + sx; } if (e2 < dx) { err = err + dx; y0 = y0 + sy; } } } // Draw a hand from 0,0 to x,y void DrawHand(int x, int y) { int v = x/2; int u = y/2; int w = v/5; int t = u/5; MoveTo(0, 0); DrawTo(v-t, u+w); DrawTo(x, y); DrawTo(v+t, u-w); DrawTo(0, 0); } // Draw clock void DrawClock(int hour, int minute, int second) { int x = 0; int y = 46<<9; for (int i=0; i<360; i++) { int x9 = x>>9; int y9 = y>>9; // Hour marks if (i%30 == 0) { MoveTo(x9 - (x9>>3), y9 - (y9>>3)); DrawTo(x9, y9); } // Hour hand if (i == hour * 30 + (minute>>1)) DrawHand(x9 - (x9>>2), y9 - (y9>>2)); // Minute hand if (i == minute * 6 + second/10) DrawHand(x9, y9); // Second hand if (i == second * 6) { MoveTo(0, 0); DrawTo(x9, y9); } // Border of clock MoveTo(x9, y9); if (x9 > 0) DrawTo(47, y9); else DrawTo (-47, y9); x = x + (y9 * Delta); y = y - ((x>>9) * Delta); } } const int HourNow = 6; const int MinuteNow = 16; void setup() { // Save power; switch off what we don't need // Timer/Counter0, USI, and ADC // PRR = 1<> 1; digitalWrite(Clk, LOW); } } const int BytesPerLine = 12; const int LinesPerSlice = 16; const int Slices = 96 / LinesPerSlice; // Update the display void Update (int hour, int minute, int second) { digitalWrite(Ss, HIGH); // Command - toggle COM every second SendByte(0x01 | (second & 0x01)<<1); for (Slice=0; Slice < Slices; Slice++) { // Clear buffer for (int i=0; i