/* Tiny Face Watch David Johnson-Davies - www.technoblogy.com - 7th June 2017 ATtiny85 @ 8 MHz (internal oscillator; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include #include // Constants const int SleepTime = 30000; // Time display stays on // Pins const int clk = 0; const int data = 1; const int dc = 2; const int cs = 3; // One Wire Protocol ********************************************** // Buffer to read data or ROM code static union { uint8_t DataBytes[5]; struct { uint8_t control; long seconds; } rtc; }; const int OneWirePin = 4; const int SkipROM = 0xCC; const int WriteClock = 0x99; const int ReadClock = 0x66; inline void PinLow () { DDRB = DDRB | 1<>OneWirePin & 1; } void DelayMicros (int micro) { TCNT1 = 0; TIFR = 1<>1) - 1; while ((TIFR & 1<> 1; } } uint8_t OneWireRead () { uint8_t data = 0; for (int i = 0; i<8; i++) { LowRelease(6, 9); data = data | PinRead()<>1); PORTB = PORTB & ~(1<>= 1) { PINB = 1<>3; int bit = row & 0x07; // Set correct bit in slice buffer Buffer[page*64 + col] |= 1< -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 = 23<<9; for (int i=0; i<360; i++) { int x9 = x>>9; int y9 = y>>9; DrawTo(x9, y9); // 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(23, y9); else DrawTo (-23, y9); x = x + (y9 * Delta); y = y - ((x>>9) * Delta); } } // Setup ********************************************** void SetTime () { unsigned long Offset = millis(); unsigned long secs = 0; for (;;) { int Mins = (unsigned long)(secs / 60) % 60; int Hours = (unsigned long)(secs / 3600) % 12; // Write time to RTC rtc.control = 0x0C; rtc.seconds = secs + ((millis()-Offset)/1000); OneWireReset(); OneWireWrite(SkipROM); OneWireWrite(WriteClock); OneWireWriteBytes(5); ClearBuffer(); DrawClock(Hours, Mins, -1); DisplayBuffer(); unsigned long Start = millis(); while (millis()-Start < 500); secs = secs + 60; } } void setup() { // Define pins pinMode(dc, OUTPUT); digitalWrite(dc,HIGH); pinMode(clk, OUTPUT); digitalWrite(clk,HIGH); pinMode(data, OUTPUT); pinMode(cs, OUTPUT); digitalWrite(cs,HIGH); InitDisplay(); DisplayOn(); OneWireSetup(); // Disable what we don't need to save power ADCSRA &= ~(1<