// reading twice costs us 28 bytes, but improves reliability. // The root problem is when the charge value goes from <128 to >768 (or the // reverse, from topping off), it passes through the middle range. If we // read at the wrong time, we can get a BATTERY value while we are still // plugged in. // Reading twice with a sufficient delay, we can guarantee that our state is correct. unsigned char hexbright::get_definite_charge_state() { unsigned char val1 = get_charge_state(); delayMicroseconds(50); // wait a little in case the value was changing unsigned char val2 = get_charge_state(); // BATTERY & CHARGING = CHARGING, BATTERY & CHARGED = CHARGED, CHARGED & CHARGING = CHARGING // In essence, only return the middle value (BATTERY) if two reads report the same thing. return val1 & val2; }
void hexbright::print_charge(unsigned char led) { unsigned char charge_state = get_charge_state(); if(charge_state == CHARGING && get_led_state(led) == LED_OFF) { set_led(led, 350, 350); } else if (charge_state == CHARGED) { set_led(led,50); } }
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::read_avr_voltage() { band_gap_reading = read_adc(APIN_BAND_GAP); if(get_charge_state()==BATTERY) lowest_band_gap_reading = band_gap_reading < lowest_band_gap_reading ? band_gap_reading : lowest_band_gap_reading; }