volatile void Button::poll()
{
    uint8_t i;
    char p;

    for(i = 0; i < NUM_KEYS; i++)
    {
        p = pgm_read_byte(&button_pins[i]);
        
        if(i < 2) 
            p = (getBit(p, FB_PIN) == LOW);
        else 
            p = (getBit(p, B_PIN) == LOW);
        
        if(p)  // key is pressed
        {
            if(button_count[i] < DEBOUNCE_REPEAT_DELAY)
            {
                button_count[i]++;
                
                if(button_count[i] > DEBOUNCE_ON)
                {
                    if(button_status[i] == 0)
                    {
                        button_flag[i] = 1;
                        button_status[i] = 1; //button debounced to 'pressed' status
                        clock.awake(); // keep from sleeping since a button was pressed
                    }

                }
            }
            else
            {
                if(i + 1 == UP_KEY || i + 1 == DOWN_KEY || (verticalRepeat && (i + 1 == LEFT_KEY || i + 1 == RIGHT_KEY)))
                {
                    button_flag[i] = 1;
                    button_status[i] = 1; //button debounced to 'pressed' status
                    button_count[i] = DEBOUNCE_REPEAT_DELAY - DEBOUNCE_REPEAT_SPEED;
                }
                else if(i + 1 == FL_KEY)
                {
                    off_count++;
                    if(off_count > POWER_OFF_TIME)
                    {
                        menu.message(TEXT("Power Off"));
                        menu.task();
                        cli();
                        while(getBit(p, FB_PIN) == LOW) wdt_reset();
                        hardware_off();
                    }
                }
            }

        } 
        else // not pressed
        {
            if(i + 1 == FL_KEY) off_count = 0;
            if(button_count[i] > 0)
            {
                //button_flag[i] = 0;
                if(button_count[i] > DEBOUNCE_MAX) button_count[i] = DEBOUNCE_MAX;
                button_count[i]--;
                
                if(button_count[i] < DEBOUNCE_OFF)
                {
                    button_status[i] = 0;   //button debounced to 'released' status
                }
            }
        }
    }
}
volatile char batteryStatus(char key, char first)
{
//	uint16_t batt_high = 645;
//	uint16_t batt_low = 540;
	static uint8_t charging;
	char stat = battery_status();

	if(first)
	{
		charging = (stat > 0);
	}

//	unsigned int batt_level = battery_read_raw();

#define BATT_LINES 36

//	uint8_t lines = ((batt_level - batt_low) * BATT_LINES) / (batt_high - batt_low);
	uint8_t lines = (uint8_t)((uint16_t)battery_percent * BATT_LINES / 100);

	if(lines > BATT_LINES - 1 && stat == 1)
		lines = BATT_LINES - 1;

	if(lines > BATT_LINES || stat == 2)
		lines = BATT_LINES;

	lcd.cls();

	char* text;

	text = getChargingStatus();

	char l = lcd.measureStringTiny(text) / 2;

	if(battery_status())
		lcd.writeStringTiny(41 - l, 31, text);

	// Draw Battery Outline //
	lcd.drawLine(20, 15, 60, 15);
	lcd.drawLine(20, 16, 20, 27);
	lcd.drawLine(21, 27, 60, 27);
	lcd.drawLine(60, 16, 60, 19);
	lcd.drawLine(60, 23, 60, 26);
	lcd.drawLine(61, 19, 61, 23);

	// Draw Battery Charge //
	for(uint8_t i = 0; i <= lines; i++)
	{
		lcd.drawLine(22 + i, 17, 22 + i, 25);
	}

	menu.setTitle(TEXT("Battery Status"));
	menu.setBar(TEXT("RETURN"), BLANK_STR);
	lcd.update();

	if(stat == 0 && charging)
	{
		clock.awake();
		return FN_CANCEL; // unplugged
	}
	if(key == FL_KEY)
		return FN_CANCEL;

	return FN_CONTINUE;
}