diff options
| author | tmk <nobody@nowhere> | 2014-09-04 17:08:23 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2014-09-04 17:08:23 +0900 |
| commit | 3b81ffc16c8cdf260c25f0778d32c721af4f105c (patch) | |
| tree | 4d9ef22491bfc77a0956d87718b715742e07497f /keyboard/hhkb_rn42 | |
| parent | 02939ab1d831ab7bb02edb28cb0b21fb61bced56 (diff) | |
| download | qmk_firmware-3b81ffc16c8cdf260c25f0778d32c721af4f105c.tar.gz qmk_firmware-3b81ffc16c8cdf260c25f0778d32c721af4f105c.zip | |
Monitor battery and alert low voltage
Diffstat (limited to 'keyboard/hhkb_rn42')
| -rw-r--r-- | keyboard/hhkb_rn42/rn42/battery.c | 13 | ||||
| -rw-r--r-- | keyboard/hhkb_rn42/rn42/battery.h | 1 | ||||
| -rw-r--r-- | keyboard/hhkb_rn42/rn42/rn42_task.c | 47 |
3 files changed, 52 insertions, 9 deletions
diff --git a/keyboard/hhkb_rn42/rn42/battery.c b/keyboard/hhkb_rn42/rn42/battery.c index 32de86448..c6988fe33 100644 --- a/keyboard/hhkb_rn42/rn42/battery.c +++ b/keyboard/hhkb_rn42/rn42/battery.c | |||
| @@ -49,10 +49,11 @@ bool battery_charging(void) | |||
| 49 | { | 49 | { |
| 50 | if (!(USBSTA&(1<<VBUS))) return false; | 50 | if (!(USBSTA&(1<<VBUS))) return false; |
| 51 | 51 | ||
| 52 | // MCP73831:STAT | 52 | // Charger Status: |
| 53 | // HiZ: Shutdown/No Battery | 53 | // MCP73831 MCP73832 LTC4054 Status |
| 54 | // Low: Charging | 54 | // Hi-Z Hi-Z Hi-Z Shutdown/No Battery |
| 55 | // Hi: Charged | 55 | // Low Low Low Charging |
| 56 | // Hi Hi-Z Hi-Z Charged | ||
| 56 | 57 | ||
| 57 | // preserve last register status | 58 | // preserve last register status |
| 58 | uint8_t ddrf_prev = DDRF; | 59 | uint8_t ddrf_prev = DDRF; |
| @@ -68,6 +69,10 @@ bool battery_charging(void) | |||
| 68 | DDRF = (DDRF&~(1<<5)) | (ddrf_prev&(1<<5)); | 69 | DDRF = (DDRF&~(1<<5)) | (ddrf_prev&(1<<5)); |
| 69 | PORTF = (PORTF&~(1<<5)) | (portf_prev&(1<<5)); | 70 | PORTF = (PORTF&~(1<<5)) | (portf_prev&(1<<5)); |
| 70 | 71 | ||
| 72 | // TODO: With MCP73831 this can not get stable status when charging. | ||
| 73 | // LED is powered from PSEL line(USB or Lipo) | ||
| 74 | // due to weak low output of STAT pin? | ||
| 75 | // due to pull-up'd via resitor and LED? | ||
| 71 | return charging; | 76 | return charging; |
| 72 | } | 77 | } |
| 73 | 78 | ||
diff --git a/keyboard/hhkb_rn42/rn42/battery.h b/keyboard/hhkb_rn42/rn42/battery.h index 60fc8adfc..180d4dcfa 100644 --- a/keyboard/hhkb_rn42/rn42/battery.h +++ b/keyboard/hhkb_rn42/rn42/battery.h | |||
| @@ -9,6 +9,7 @@ typedef enum { | |||
| 9 | CHARGING, | 9 | CHARGING, |
| 10 | DISCHARGING, | 10 | DISCHARGING, |
| 11 | LOW_VOLTAGE, | 11 | LOW_VOLTAGE, |
| 12 | UNKNOWN, | ||
| 12 | } battery_status_t; | 13 | } battery_status_t; |
| 13 | 14 | ||
| 14 | typedef enum { | 15 | typedef enum { |
diff --git a/keyboard/hhkb_rn42/rn42/rn42_task.c b/keyboard/hhkb_rn42/rn42/rn42_task.c index 07b34e111..30914452e 100644 --- a/keyboard/hhkb_rn42/rn42/rn42_task.c +++ b/keyboard/hhkb_rn42/rn42/rn42_task.c | |||
| @@ -81,13 +81,50 @@ void rn42_task(void) | |||
| 81 | } | 81 | } |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | /* Low voltage alert */ | 84 | |
| 85 | if (battery_status() == LOW_VOLTAGE) { | 85 | static uint16_t prev_timer = 0; |
| 86 | battery_led(LED_ON); | 86 | static uint8_t sec = 0; |
| 87 | } else { | 87 | // NOTE: not exact 1 sec |
| 88 | battery_led(LED_CHARGER); | 88 | if (timer_elapsed(prev_timer) > 1000) { |
| 89 | /* every second */ | ||
| 90 | prev_timer = timer_read(); | ||
| 91 | |||
| 92 | /* Low voltage alert */ | ||
| 93 | uint8_t bs = battery_status(); | ||
| 94 | if (bs == LOW_VOLTAGE) { | ||
| 95 | battery_led(LED_ON); | ||
| 96 | } else { | ||
| 97 | battery_led(LED_CHARGER); | ||
| 98 | } | ||
| 99 | |||
| 100 | static uint8_t prev_status = UNKNOWN; | ||
| 101 | if (bs != prev_status) { | ||
| 102 | prev_status = bs; | ||
| 103 | switch (bs) { | ||
| 104 | case FULL_CHARGED: xprintf("FULL_CHARGED\n"); break; | ||
| 105 | case CHARGING: xprintf("CHARGING\n"); break; | ||
| 106 | case DISCHARGING: xprintf("DISCHARGING\n"); break; | ||
| 107 | case LOW_VOLTAGE: xprintf("LOW_VOLTAGE\n"); break; | ||
| 108 | default: xprintf("UNKNOWN STATUS\n"); break; | ||
| 109 | }; | ||
| 110 | } | ||
| 111 | |||
| 112 | /* every minute */ | ||
| 113 | if (sec == 0) { | ||
| 114 | uint32_t t = timer_read32()/1000; | ||
| 115 | uint16_t v = battery_voltage(); | ||
| 116 | uint8_t h = t/3600; | ||
| 117 | uint8_t m = t%3600/60; | ||
| 118 | uint8_t s = t%60; | ||
| 119 | xprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v); | ||
| 120 | /* TODO: xprintf doesn't work for this. | ||
| 121 | xprintf("%02u:%02u:%02u\t%umV\n", (t/3600), (t%3600/60), (t%60), v); | ||
| 122 | */ | ||
| 123 | } | ||
| 124 | sec++; sec = sec%60; | ||
| 89 | } | 125 | } |
| 90 | 126 | ||
| 127 | |||
| 91 | /* Connection monitor */ | 128 | /* Connection monitor */ |
| 92 | if (rn42_linked()) { | 129 | if (rn42_linked()) { |
| 93 | status_led(true); | 130 | status_led(true); |
