]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Implement battery level indicator
authorPriyadi Iman Nurcahyo <priyadi@priyadi.net>
Fri, 10 Feb 2017 14:28:46 +0000 (21:28 +0700)
committerPriyadi Iman Nurcahyo <priyadi@priyadi.net>
Fri, 10 Feb 2017 14:28:46 +0000 (21:28 +0700)
keyboards/handwired/promethium/keymaps/priyadi/keymap.c
keyboards/handwired/promethium/promethium.c
keyboards/handwired/promethium/promethium.h
keyboards/handwired/promethium/rgbsps.c
keyboards/handwired/promethium/rgbsps.h
keyboards/handwired/promethium/rules.mk

index 65fa14dd3556636aed0f279624a371059992dfc5..469d7a5d80477fabbd36f68568a92fdd51a6eec5 100644 (file)
@@ -25,6 +25,7 @@ enum glow_modes {
   GLOW_MIN,
   GLOW_FULL
 };
+
 uint8_t glow_mode = GLOW_MIN;
 
 extern keymap_config_t keymap_config;
@@ -505,11 +506,6 @@ void led_init(void) {
   rgbsps_set(LED_TRACKPOINT2, 0, 0, 15);
   rgbsps_set(LED_TRACKPOINT3, 15, 0, 0);
 
-  // // hardcode indicator for now
-  rgbsps_set(LED_IND_BLUETOOTH, 0, 0, 15);
-  rgbsps_set(LED_IND_USB, 15, 15, 15);
-  rgbsps_set(LED_IND_BATTERY, 0, 15, 0);
-
   led_layer_normal();
 }
 
@@ -996,6 +992,11 @@ void matrix_init_user(void) {
   }
 }
 
+void battery_poll(float percentage) {
+  rgbsps_sethsv(LED_IND_BATTERY, percentage*120/100, 255, 15);
+  rgbsps_send();
+}
+
 void ps2_mouse_init_user() {
     uint8_t rcv;
 
index a0035cce1aa2ffa695ed3cc92c5456500deb135d..7f876c7562881b11f39efab8c5e36e1d005c99f9 100644 (file)
@@ -1,6 +1,36 @@
 #include "promethium.h"
+#include "analog.h"
+#include "timer.h"
+#include "matrix.h"
 
-void matrix_init_kb(void) {
+float battery_percentage(void) {
+    float voltage = analogRead(BATTERY_PIN) * 2 * 3.3 / 1024;
+    float percentage = (voltage - 3.5) * 143;
+    if (percentage > 100) {
+        return 100;
+    } else if (percentage < 0) {
+        return 0;
+    } else {
+        return percentage;
+    }
+}
+
+__attribute__ ((weak))
+void battery_poll(float percentage) {
+}
 
+void matrix_init_kb(void) {
        matrix_init_user();
-}
\ No newline at end of file
+}
+
+void matrix_scan_kb(void) {
+    static uint16_t counter = BATTERY_POLL;
+    counter++;
+
+    if (counter > BATTERY_POLL) {
+        counter = 0;
+        battery_poll(battery_percentage());
+    }
+}
+
+
index a2572db2afafa146910b6e57749855fd4ffd2a6c..6d51f81ca6529b72cb49fc808572bb1035729643 100644 (file)
@@ -5,6 +5,8 @@
 
 #define PS2_INIT_DELAY 2000
 #define UNICODE_TYPE_DELAY 0
+#define BATTERY_PIN 9
+#define BATTERY_POLL 30000
 
 #define KEYMAP( \
     k11, k12, k13, k14, k15, k16, k17, k18, k19, k1a, k1b, k1c, \
@@ -23,6 +25,8 @@
     {k47, k48, k49, k4a, k4b, k4c} \
 }
 
+
+
 enum led_sequence {
   LED_IND_BLUETOOTH,
   LED_IND_USB,
@@ -99,4 +103,4 @@ enum led_sequence {
 
 #endif
 
-
+void battery_poll(float percentage);
\ No newline at end of file
index ea922ec3fde71af55635a962f0f62b9ac81081d7..f30badd3568cc222a2aaf9f7d2962c9384ffbad4 100644 (file)
@@ -21,4 +21,53 @@ void rgbsps_turnoff(void) {
 
 void rgbsps_send(void) {
   ws2812_setleds(led, RGBSPS_NUM);
-}
\ No newline at end of file
+}
+
+void rgbsps_sethsv(uint8_t index, uint16_t hue, uint8_t sat, uint8_t val) {
+  uint8_t r = 0, g = 0, b = 0, base, color;
+
+  if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
+    r = val;
+    g = val;
+    b = val;
+  } else {
+    base = ((255 - sat) * val) >> 8;
+    color = (val - base) * (hue % 60) / 60;
+
+    switch (hue / 60) {
+      case 0:
+        r = val;
+        g = base + color;
+        b = base;
+        break;
+      case 1:
+        r = val - color;
+        g = val;
+        b = base;
+        break;
+      case 2:
+        r = base;
+        g = val;
+        b = base + color;
+        break;
+      case 3:
+        r = base;
+        g = val - color;
+        b = val;
+        break;
+      case 4:
+        r = base + color;
+        g = base;
+        b = val;
+        break;
+      case 5:
+        r = val;
+        g = base;
+        b = val - color;
+        break;
+    }
+  }
+
+  rgbsps_set(index, r, g, b);
+}
+
index 6da197f75dd57d71a3620dc2c485abfeb10836bb..72612a7a82c3925f0d64de728fc384fb37ec8a09 100644 (file)
@@ -1,4 +1,5 @@
 void rgbsps_set(uint8_t index, uint8_t r, uint8_t g, uint8_t b);
 void rgbsps_setall(uint8_t r, uint8_t g, uint8_t b);
 void rgbsps_turnoff(void);
-void rgbsps_send(void);
\ No newline at end of file
+void rgbsps_send(void);
+void rgbsps_sethsv(uint8_t index, uint16_t hue, uint8_t sat, uint8_t val);
\ No newline at end of file
index 465ef3359a2f96806114498d7913dcacdc1e306d..e75cf4dde430e30f01f8fc09598d0e57e63bbaa3 100644 (file)
@@ -72,4 +72,5 @@ API_SYSEX_ENABLE ?= no
 SLEEP_LED_ENABLE ?= no    # Breathing sleep LED during USB suspend
 
 SRC += $(QUANTUM_DIR)/light_ws2812.c
-SRC += rgbsps.c
\ No newline at end of file
+SRC += rgbsps.c
+SRC += $(QUANTUM_DIR)/analog.c
\ No newline at end of file