/* * moon.c * Created: 10/22/2016 1:14:19 PM * Author: jumbleview */ #include #include #include "./pt-1.4/pt.h" // http://dunkels.com/adam/pt/ typedef enum {OUT_LOW, OUT_HIGH} OutputLevel; volatile uint16_t pulses; #define pinOut(port, bit, outLevel) \ (DDR##port |= (1 << ( DD##port##bit)));\ switch(outLevel) \ {\ case OUT_LOW: (PORT##port &= ~(1 << (PORT##port##bit))); break;\ case OUT_HIGH: (PORT##port |= (1 << (PORT##port##bit))); break;\ } void clearAll() { // set all outputs low DDRB = 0xFF; PORTB = 0; DDRC = 0xFF; PORTC = 0; DDRD = 0xFF; PORTD = 0; } void activateTimer0() { cli(); pulses = 0; TCCR0A=0; // Normal operation TCCR0B=2; // f divider 64 : about 2 ms for interrupt ... TIMSK0 = 1; // for system clock f= 1Mhz (CKDIV8 set) sei(); } struct pt wpt; // protothread descriptor int moonlight(struct pt* mlpt) { PT_BEGIN(mlpt); // New Moon PT_YIELD(mlpt); pinOut(C,5,OUT_HIGH); pinOut(B,1,OUT_HIGH); PT_YIELD(mlpt); pinOut(D,0,OUT_HIGH); pinOut(B,2,OUT_HIGH); PT_YIELD(mlpt); pinOut(D,1,OUT_HIGH); pinOut(B,0,OUT_HIGH); PT_YIELD(mlpt); // First Quarter pinOut(D,2,OUT_HIGH); pinOut(D,7,OUT_HIGH); PT_YIELD(mlpt); pinOut(D,3,OUT_HIGH); pinOut(D,6,OUT_HIGH); PT_YIELD(mlpt); pinOut(D,4,OUT_HIGH); pinOut(D,5,OUT_HIGH); pinOut(C,0,OUT_HIGH); pinOut(C,1,OUT_HIGH); PT_YIELD(mlpt); // Full Moon + Red Eyes PT_YIELD(mlpt); pinOut(C,0,OUT_LOW); pinOut(C,1,OUT_LOW); pinOut(C,5,OUT_LOW); pinOut(B,1,OUT_LOW); PT_YIELD(mlpt); pinOut(D,0,OUT_LOW); pinOut(B,2,OUT_LOW); PT_YIELD(mlpt); pinOut(D,1,OUT_LOW); pinOut(B,0,OUT_LOW); PT_YIELD(mlpt); // Third Quarter pinOut(D,2,OUT_LOW); pinOut(D,7,OUT_LOW); PT_YIELD(mlpt); pinOut(D,3,OUT_LOW); pinOut(D,6,OUT_LOW); PT_YIELD(mlpt); pinOut(D,4,OUT_LOW); pinOut(D,5,OUT_LOW); PT_RESTART(mlpt); // New Moon PT_END(mlpt); } ISR(TIMER0_OVF_vect) { pulses++; uint16_t mod =pulses%750; if (mod == 0){ moonlight(&wpt); } } int main(void) { PT_INIT(&wpt); // initiate protothread structure... clearAll(); activateTimer0(); while(1) { } }