Skip to content
Snippets Groups Projects
Commit 07a0b224 authored by koalo's avatar koalo
Browse files

epicardium: BLE opt in

parent 154cc573
No related branches found
No related tags found
1 merge request!127epicardium: BLE opt in
Pipeline #2460 passed
#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 */
......
......@@ -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.
......
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)
......@@ -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) },
......
......@@ -97,6 +97,7 @@ Q(write)
/* os */
Q(os)
Q(exit)
Q(reset)
Q(exec)
Q(listdir)
Q(unlink)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment