void hexbright::init_hardware() { // These next 8 commands are for reference and cost nothing, // as we are initializing the values to their default state. pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, LOW); pinModeFast(DPIN_RLED_SW, INPUT); pinModeFast(DPIN_GLED, OUTPUT); pinModeFast(DPIN_DRV_MODE, OUTPUT); pinModeFast(DPIN_DRV_EN, OUTPUT); digitalWriteFast(DPIN_DRV_MODE, LOW); digitalWriteFast(DPIN_DRV_EN, LOW); #if (DEBUG!=DEBUG_OFF) // Initialize serial busses Serial.begin(9600); Wire.begin(); Serial.println("DEBUG MODE ON"); #endif #if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) if(DEBUG==DEBUG_LIGHT) { // do a full light range sweep, (printing all light intensity info) set_light(0,1000,update_delay*1002); } else if (DEBUG==DEBUG_TEMP) { set_light(0, MAX_LEVEL, NOW); } else if (DEBUG==DEBUG_LOOP) { // note the use of TIME_MS/update_delay. set_light(0, MAX_LEVEL, 2500/update_delay); } #ifdef FREE_RAM Serial.print("Ram available: "); Serial.print(freeRam()); Serial.println("/1024 bytes"); #endif #ifdef FLASH_CHECKSUM Serial.print("Flash checksum: "); Serial.println(flash_checksum()); #endif #endif // DEBUG!=DEBUG_OFF #ifdef ACCELEROMETER enable_accelerometer(); #endif // was this power on from battery? if so, it was a button press, even if it was too fast to register. read_charge_state(); if(get_charge_state()==BATTERY) press_button(); continue_time = micros(); }
void hexbright::init_hardware() { // These next 8 commands are for reference and cost nothing, // as we are initializing the values to their default state. pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, LOW); pinModeFast(DPIN_RLED_SW, INPUT); pinModeFast(DPIN_GLED, OUTPUT); pinModeFast(DPIN_DRV_MODE, OUTPUT); pinModeFast(DPIN_DRV_EN, OUTPUT); digitalWriteFast(DPIN_DRV_MODE, LOW); digitalWriteFast(DPIN_DRV_EN, LOW); #if (DEBUG!=DEBUG_OFF) // Initialize serial busses Serial.begin(9600); Wire.begin(); Serial.println("DEBUG MODE ON"); #endif #if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) #ifdef FREE_RAM Serial.print("Ram available: "); Serial.print(freeRam()); Serial.println("/1024 bytes"); #endif #ifdef FLASH_CHECKSUM Serial.print("Flash checksum: "); Serial.println(flash_checksum()); #endif #endif //(DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) #ifdef ACCELEROMETER enable_accelerometer(); #endif // was this power on from battery? if so, it was a button press, even if it was too fast to register. read_charge_state(); if(get_charge_state()==BATTERY) press_button(); continue_time = micros(); }
void hexbright::update() { unsigned long now; #if (DEBUG==DEBUG_LOOP) unsigned long start_time=micros(); #endif #ifdef STROBE while (true) { do { now = micros(); } while (next_strobe > now && // not ready for strobe continue_time > now); // not ready for update if (next_strobe <= now) { if (now - next_strobe <26) { digitalWriteFast(DPIN_DRV_EN, HIGH); delayMicroseconds(strobe_duration); digitalWriteFast(DPIN_DRV_EN, LOW); } next_strobe += strobe_delay; } if(continue_time <= now) { if(strobe_delay>update_delay && // we strobe less than once every 8333 microseconds next_strobe-continue_time < 4000) // and the next strobe is within 4000 microseconds (may occur before we return) continue; else break; } } // do nothing... (will short circuit once every 70 minutes (micros maxint)) #else do { now = micros(); } while (continue_time > now); // not ready for update #endif // if we're in debug mode, let us know if our loops are too large #if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) static int i=0; #if (DEBUG==DEBUG_LOOP) static unsigned long last_time = 0; if(!i) { Serial.print("Time used: "); Serial.print(start_time-last_time); Serial.println("/8333"); } last_time = now; #endif if(now-continue_time>5000 && !i) { // This may be caused by too much processing for our update_delay, or by too many print statements) // If you're triggering this, your button and light will react more slowly, and some accelerometer // data is being missed. Serial.println("WARNING: code is too slow"); } if (!i) i=1000/update_delay; // display loop output every second else i--; #endif // power saving modes described here: http://www.atmel.com/Images/2545s.pdf //run overheat protection, time display, track battery usage #ifdef LED // regardless of desired led state, turn it off so we can read the button _led_off(RLED); delayMicroseconds(50); // let the light stabilize... read_button(); // turn on (or off) the leds, if appropriate adjust_leds(); #ifdef PRINT_NUMBER update_number(); #endif #else read_button(); #endif read_thermal_sensor(); // takes about .2 ms to execute (fairly long, relative to the other steps) read_charge_state(); read_avr_voltage(); #ifdef ACCELEROMETER read_accelerometer(); find_down(); #endif detect_overheating(); detect_low_battery(); apply_max_light_level(); // change light levels as requested adjust_light(); // advance time at the same rate as values are changed in the accelerometer. // advance continue_time here, so the first run through short-circuits, // meaning we will read hardware immediately after power on. continue_time = continue_time+(1000*update_delay); }