#include "common.h" #include #include #include #include #include #include #include #include "i2c.h" #include "uart.h" #include "timer.h" #include "gyro.h" #include "accel.h" #include "speaker.h" #include "pwm.h" static void cpuInit () { /* enter change prescaler mode */ CLKPR = (1 << CLKPCE); /* write new prescaler */ #if F_CPU == 1000000 CLKPR = 0b00000011; #elif F_CPU == 4000000 CLKPR = 0b00000001; #elif F_CPU == 8000000 CLKPR = 0b00000000; #else #error "cpu speed not supported" #endif } int main () { cpuInit (); twInit (); uartInit (); gyroInit (); accelInit (); speakerInit (); pwmInit (); set_sleep_mode (SLEEP_MODE_IDLE); printf ("initialization done\n"); /* global interrupt enable */ sei (); gyroStart (); accelStart (); pwmStart (); #if 0 speakerStart (); _delay_ms (200); speakerStop (); #endif timerStart (); bool checkGyro = false; while (1) { while (!timerHit ()) { for (uint8_t i = 0; i < 6; i++) { pwmSetBrightness (i, abs (gyroGetZTicks ())); } /* round-robin to prevent starvation */ if (checkGyro) { gyroProcess (); accelProcess(); } else { accelProcess (); gyroProcess (); } checkGyro = !checkGyro; sleep_enable (); sleep_cpu (); sleep_disable (); } printf ("t=%i, h=%i, s=%i\n", gyroGetZTicks (), accelGetHorizon (), accelGetShakeCount ()); #if 0 volatile const int32_t *gyroval = gyroGetAccum (); volatile const int16_t *gyroraw = gyroGetRaw (); volatile const int8_t *accelval = accelGet (); printf ("%li/%li/%li - %i/%i/%i - %i/%i/%i\n", gyroval[0], gyroval[1], gyroval[2], gyroraw[0], gyroraw[1], gyroraw[2], accelval[1], accelval[3], accelval[5]); #endif } timerStop (); pwmStop (); printf ("stopped\n"); /* global interrupt disable */ cli (); while (1); }