aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2020-04-26 12:56:11 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2020-04-26 12:56:11 +0200
commitcbdf9d9c9d9541a16223118664a2590cabee7a76 (patch)
tree8392c6eb85884bd3709fa8a895136293e6f9d1a6
parentc056e476cfd51ee9ce5679ba06b51a48a1b07a1f (diff)
downloadglutte-o-matic-cbdf9d9c9d9541a16223118664a2590cabee7a76.tar.gz
glutte-o-matic-cbdf9d9c9d9541a16223118664a2590cabee7a76.tar.bz2
glutte-o-matic-cbdf9d9c9d9541a16223118664a2590cabee7a76.zip
Add missing files
-rw-r--r--src/common/GPIO/batterycharge.c87
-rw-r--r--src/common/GPIO/batterycharge.h35
2 files changed, 122 insertions, 0 deletions
diff --git a/src/common/GPIO/batterycharge.c b/src/common/GPIO/batterycharge.c
new file mode 100644
index 0000000..95176cd
--- /dev/null
+++ b/src/common/GPIO/batterycharge.c
@@ -0,0 +1,87 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+#include "GPIO/batterycharge.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+static int charge_valid;
+static uint32_t last_charge;
+
+void batterycharge_init()
+{
+ /* No need to protect the variables, this is called at init before the
+ * tasks get created.
+ */
+ charge_valid = 0;
+ last_charge = 0;
+}
+
+void batterycharge_push_message(const char *ccounter_msg)
+{
+ /* We ignore TEXT, ERROR, VBAT+ and VBAT- for the moment, and only parse CAPA.
+ * The \r\n has been trimmed off the message already, therefore the value
+ * should extend until the end of string.
+ */
+ if (strncmp(ccounter_msg, "CAPA", 4) == 0) {
+ size_t ix_value = 0;
+ int second_found = 0;
+
+ for (size_t ix = 0; ccounter_msg[ix] != '\0'; ix++) {
+ if (ccounter_msg[ix] == ',') {
+ if (! second_found) {
+ second_found = 1;
+ }
+ else {
+ ix_value = ix + 1;
+ }
+ }
+ }
+
+ const char *valuestr = ccounter_msg + ix_value;
+ char *endptr;
+ errno = 0;
+ const uint32_t value = strtol(valuestr, &endptr, 10);
+ if (errno == 0 && endptr != valuestr) {
+ taskDISABLE_INTERRUPTS();
+ last_charge = value;
+ charge_valid = 1;
+ taskENABLE_INTERRUPTS();
+ }
+ }
+}
+
+uint32_t batterycharge_retrieve_last_capacity()
+{
+ taskDISABLE_INTERRUPTS();
+ uint32_t ret = 0;
+ if (charge_valid) {
+ ret = last_charge;
+ }
+ taskENABLE_INTERRUPTS();
+ return ret;
+}
diff --git a/src/common/GPIO/batterycharge.h b/src/common/GPIO/batterycharge.h
new file mode 100644
index 0000000..2d1e198
--- /dev/null
+++ b/src/common/GPIO/batterycharge.h
@@ -0,0 +1,35 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+#pragma once
+#include <stdint.h>
+
+void batterycharge_init(void);
+
+// Parse a message received from the glutte coulomb counter and update
+// internal state accordingly
+void batterycharge_push_message(const char *ccounter_msg);
+
+// Get the last received battery capacity in mAh, or 0 if not valid
+uint32_t batterycharge_retrieve_last_capacity(void);