From 07a0b22424cd10bf5113ac08ac48b5ad94f8b888 Mon Sep 17 00:00:00 2001
From: koalo <koalo@koalo.de>
Date: Tue, 20 Aug 2019 12:07:12 +0200
Subject: [PATCH] epicardium: BLE opt in
---
epicardium/main.c | 62 ++++++++++++++++++++++++-----
epicardium/modules/buttons.c | 2 +-
preload/ble.py | 77 ++++++++++++++++++++++++++++++++++++
pycardium/modules/os.c | 10 +++++
pycardium/modules/qstrdefs.h | 1 +
5 files changed, 142 insertions(+), 10 deletions(-)
create mode 100644 preload/ble.py
diff --git a/epicardium/main.c b/epicardium/main.c
index d0e15ff9..016f3095 100644
--- a/epicardium/main.c
+++ b/epicardium/main.c
@@ -1,16 +1,58 @@
#include "modules/modules.h"
#include "modules/log.h"
+#include "modules/filesystem.h"
#include "card10-version.h"
#include "FreeRTOS.h"
#include "task.h"
#include <stdlib.h>
+#include <string.h>
TaskHandle_t dispatcher_task_id;
void vBleTask(void *pvParameters);
+#define BLEMAXCFGBYTES 100
+int bleShallStart(void)
+{
+ int bleConfigFile = epic_file_open("ble.txt", "r");
+ if (bleConfigFile < 0) {
+ LOG_INFO(
+ "startup",
+ "can not open ble.txt -> BLE is not started"
+ );
+ epic_file_close(bleConfigFile);
+ return 0;
+ }
+
+ char cfgBuf[BLEMAXCFGBYTES + 1];
+ int readNum = epic_file_read(bleConfigFile, cfgBuf, BLEMAXCFGBYTES);
+ epic_file_close(bleConfigFile);
+ if (readNum < 0) {
+ LOG_INFO(
+ "startup",
+ "can not read ble.txt -> BLE is not started"
+ );
+ return 0;
+ }
+ cfgBuf[readNum] = '\0';
+
+ char bleActiveStr[] = "active=true";
+ cfgBuf[sizeof(bleActiveStr) - 1] = '\0';
+
+ if (strcmp(cfgBuf, "active=true") != 0) {
+ LOG_INFO(
+ "startup",
+ "ble.txt is not \"active=true\" -> BLE is not started"
+ );
+ return 0;
+ }
+
+ LOG_INFO("startup", "ble.txt is \"active=true\" -> BLE is starting");
+ return 1;
+}
+
int main(void)
{
LOG_INFO("startup", "Epicardium startup ...");
@@ -62,15 +104,17 @@ int main(void)
}
/* BLE */
- if (xTaskCreate(
- vBleTask,
- (const char *)"BLE",
- configMINIMAL_STACK_SIZE * 10,
- NULL,
- tskIDLE_PRIORITY + 1,
- NULL) != pdPASS) {
- LOG_CRIT("startup", "Failed to create %s task!", "BLE");
- abort();
+ if (bleShallStart()) {
+ if (xTaskCreate(
+ vBleTask,
+ (const char *)"BLE",
+ configMINIMAL_STACK_SIZE * 10,
+ NULL,
+ tskIDLE_PRIORITY + 1,
+ NULL) != pdPASS) {
+ LOG_CRIT("startup", "Failed to create %s task!", "BLE");
+ abort();
+ }
}
/* Lifecycle */
diff --git a/epicardium/modules/buttons.c b/epicardium/modules/buttons.c
index 8364e725..539d6e5c 100644
--- a/epicardium/modules/buttons.c
+++ b/epicardium/modules/buttons.c
@@ -14,7 +14,7 @@ static const uint8_t pin_mask[] = {
uint8_t epic_buttons_read(uint8_t mask)
{
uint8_t ret = 0;
- if (portexpander_detected() && (mask & 0x3)) {
+ if (portexpander_detected() && (mask & 0x7)) {
/*
* Not using PB_Get() here as that performs one I2C transcation
* per button.
diff --git a/preload/ble.py b/preload/ble.py
new file mode 100644
index 00000000..f12562e3
--- /dev/null
+++ b/preload/ble.py
@@ -0,0 +1,77 @@
+import os
+import display
+import utime
+import buttons
+
+CONFIG_NAME = "ble.txt"
+ACTIVE_STRING = "active=true"
+INACTIVE_STRING = "active=false"
+
+
+def init():
+ if CONFIG_NAME not in os.listdir("."):
+ with open(CONFIG_NAME, "w") as f:
+ f.write(INACTIVE_STRING)
+
+
+def triangle(disp, x, y, left):
+ yf = 1 if left else -1
+ scale = 6
+ disp.line(x - scale * yf, int(y + scale / 2), x, y)
+ disp.line(x, y, x, y + scale)
+ disp.line(x, y + scale, x - scale * yf, y + int(scale / 2))
+
+
+def toggle():
+ content = INACTIVE_STRING if is_active() else ACTIVE_STRING
+ with open(CONFIG_NAME, "w") as f:
+ f.write(content)
+
+ disp.clear()
+ disp.print("resetting", posy=0, fg=[0, 255, 255])
+ disp.print("to toggle", posy=20, fg=[0, 255, 255])
+ disp.print("BLE state", posy=40, fg=[0, 255, 255])
+ disp.update()
+ os.reset()
+
+
+def is_active():
+ with open(CONFIG_NAME, "r") as f:
+ state = f.readlines()[0]
+ if len(state) < len(ACTIVE_STRING):
+ return False
+ state = state[0 : len(ACTIVE_STRING)]
+ return state == ACTIVE_STRING
+
+
+def headline():
+ disp.print("BLE", posy=0, fg=[0, 255, 255])
+ if is_active():
+ disp.print("active", posy=20, fg=[0, 255, 255])
+ else:
+ disp.print("inactive", posy=20, fg=[0, 255, 255])
+
+
+def selector():
+ triangle(disp, 148, 46, False)
+ disp.print("toggle", posx=25, posy=40, fg=[0, 255, 0])
+
+
+disp = display.open()
+button_pressed = True
+init()
+
+while True:
+ disp.clear()
+ headline()
+ v = buttons.read(buttons.TOP_RIGHT)
+ if v == 0:
+ button_pressed = False
+
+ if not button_pressed and v & buttons.TOP_RIGHT != 0:
+ button_pressed = True
+ toggle()
+
+ selector()
+ disp.update()
+ utime.sleep(0.1)
diff --git a/pycardium/modules/os.c b/pycardium/modules/os.c
index 99af67cb..d2a4ec55 100644
--- a/pycardium/modules/os.c
+++ b/pycardium/modules/os.c
@@ -19,6 +19,15 @@ static mp_obj_t mp_os_exit(size_t n_args, const mp_obj_t *args)
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exit_obj, 0, 1, mp_os_exit);
+static mp_obj_t mp_os_reset(void)
+{
+ epic_system_reset();
+
+ /* unreachable */
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_0(reset_obj, mp_os_reset);
+
static mp_obj_t mp_os_exec(mp_obj_t name_in)
{
const char *name_ptr;
@@ -117,6 +126,7 @@ static MP_DEFINE_CONST_FUN_OBJ_2(rename_obj, mp_os_rename);
static const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&exit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_exec), MP_ROM_PTR(&exec_obj) },
{ MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&listdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&unlink_obj) },
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index 4315a839..33009070 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -97,6 +97,7 @@ Q(write)
/* os */
Q(os)
Q(exit)
+Q(reset)
Q(exec)
Q(listdir)
Q(unlink)
--
GitLab