Compare commits
8 commits
b113a70577
...
e9eb537eee
Author | SHA1 | Date | |
---|---|---|---|
|
e9eb537eee | ||
|
88f4c96247 | ||
|
afd99d2586 | ||
|
41da70278f | ||
|
e3f35e4389 | ||
|
e2f7b6a391 | ||
|
7be60e623a | ||
|
3d9a9c530c |
4 changed files with 70 additions and 24 deletions
|
@ -1,11 +1,13 @@
|
|||
#ifndef __MAIN_H_
|
||||
#define __MAIN_H_
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#define PUMP_COUNT (4)
|
||||
|
||||
static xQueueHandle gpio_evt_queue = NULL;
|
||||
static QueueHandle_t gpio_evt_queue = NULL;
|
||||
nvs_handle_t config_handle;
|
||||
|
||||
extern uint8_t mac[6];
|
||||
|
|
42
main/ble.c
42
main/ble.c
|
@ -181,9 +181,10 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
|
|||
switch(uuid16) {
|
||||
case CHAR_POUR:
|
||||
if(value == 1) {
|
||||
ESP_LOGI(TAG, "starting pump");
|
||||
ESP_LOGI(TAG, "starting pumps");
|
||||
pumps_run();
|
||||
} else {
|
||||
ESP_LOGI(TAG, "stoping pumps");
|
||||
pumps_stop();
|
||||
}
|
||||
break;
|
||||
|
@ -197,13 +198,15 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
|
|||
}
|
||||
|
||||
static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
|
||||
uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
|
||||
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1);
|
||||
uint16_t chr_uuid = ble_uuid_u16(ctxt->chr->uuid);
|
||||
|
||||
// match the UUID against 0b111
|
||||
uint8_t idx = (chr_uuid & 0x07) - 1;
|
||||
uint8_t value = 0;
|
||||
|
||||
barback_ble_access_t *access = (barback_ble_access_t *)arg;
|
||||
|
||||
ESP_LOGI(TAG, "conn_handle: %d attr_handle: %d char: 0x%02X op: %s", conn_handle, attr_handle, uuid16, (ctxt->op == 0 ? "read" : ctxt->op == 1 ? "write" : "unknown"));
|
||||
ESP_LOGI(TAG, "conn_handle: %d attr_handle: %d char: 0x%02X op: %s", conn_handle, attr_handle, chr_uuid, (ctxt->op == 0 ? "read" : ctxt->op == 1 ? "write" : "unknown"));
|
||||
|
||||
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
||||
gatt_svr_chr_write(
|
||||
|
@ -215,24 +218,35 @@ static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, s
|
|||
|
||||
struct ble_gap_conn_desc notifiee;
|
||||
|
||||
// TODO
|
||||
// this could needs to be verified, it might actually be needed
|
||||
// NiBLE could be passing notifications itself on chr_handler changes
|
||||
for (uint16_t i = 0; i < CONFIG_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||
if(ble_gap_conn_find(i, ¬ifiee) == ESP_OK) {
|
||||
ble_gattc_notify_custom(i, attr_handle, ctxt->om);
|
||||
}
|
||||
}
|
||||
|
||||
if(access->write != NULL)
|
||||
if(access == NULL) {
|
||||
ESP_LOGI(TAG, "no read/write functions defined");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(access->write != NULL) {
|
||||
ESP_LOGI(TAG, "calling BLE write function: %d %d", idx, value);
|
||||
|
||||
access->write(idx, value);
|
||||
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
||||
if(access->read != NULL) {
|
||||
}
|
||||
} else if(access != NULL && ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
||||
if(access->read == NULL) {
|
||||
ESP_LOGW(TAG, "Attempted read without defining access.read");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t enabled = access->read(idx);
|
||||
uint8_t value = access->read(idx);
|
||||
|
||||
ESP_LOGI(TAG, "pumps_enabled[%d] = %d", idx, enabled);
|
||||
os_mbuf_append(ctxt->om, &enabled, sizeof enabled);
|
||||
ESP_LOGI(TAG, "returning read idx: %d value: %d", idx, value);
|
||||
os_mbuf_append(ctxt->om, &value, sizeof value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -376,6 +390,14 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
|||
event->mtu.value);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_NOTIFY_RX:
|
||||
ESP_LOGI(TAG, "BLE_GAP_EVENT_NOTIFY_RX; conn_handle=%d mtu=%d attr_handle=%d\n",
|
||||
event->mtu.conn_handle,
|
||||
event->mtu.value,
|
||||
event->notify_rx.attr_handle);
|
||||
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_NOTIFY_TX:
|
||||
ESP_LOGI(TAG, "BLE_GAP_EVENT_NOTIFY_TX; conn_handle=%d mtu=%d\n",
|
||||
event->mtu.conn_handle,
|
||||
|
|
24
main/pumps.c
24
main/pumps.c
|
@ -5,6 +5,7 @@
|
|||
#include "driver/gpio.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "pumps.h"
|
||||
|
||||
#define TAG "PUMP!"
|
||||
|
@ -17,6 +18,14 @@ static uint8_t pumps_enabled[PUMPS] = {0};
|
|||
static uint8_t pumps_state[PUMPS] = {0};
|
||||
static uint8_t safety = 1;
|
||||
|
||||
void pumps_update_config() {
|
||||
ESP_LOGI(TAG, "writing config");
|
||||
|
||||
size_t size = sizeof(uint8_t) * PUMPS;
|
||||
nvs_set_blob(config_handle, "pumps_duration", &pumps_duration, size);
|
||||
nvs_commit(config_handle);
|
||||
}
|
||||
|
||||
uint8_t pumps_set_duration(uint8_t idx, uint8_t time) {
|
||||
if(idx > PUMPS) return PUMPS_ERR_OUT_IDX;
|
||||
|
||||
|
@ -24,6 +33,8 @@ uint8_t pumps_set_duration(uint8_t idx, uint8_t time) {
|
|||
|
||||
pumps_duration[idx] = time;
|
||||
|
||||
pumps_update_config();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -53,10 +64,14 @@ uint8_t pumps_get_state(uint8_t idx) {
|
|||
}
|
||||
|
||||
uint8_t pump_enable(int8_t i) {
|
||||
ESP_LOGI(TAG, "pump: %d enabled", i);
|
||||
|
||||
return gpio_set_level(pump_gpio_map[i], 0);
|
||||
}
|
||||
|
||||
uint8_t pump_disable(int8_t i) {
|
||||
ESP_LOGI(TAG, "pump: %d disabled", i);
|
||||
|
||||
return gpio_set_level(pump_gpio_map[i], 0);
|
||||
}
|
||||
|
||||
|
@ -72,6 +87,7 @@ void pump_timer_done(TimerHandle_t timer) {
|
|||
void pumps_stop() {
|
||||
for(uint8_t i = 0; i < PUMPS; i++) {
|
||||
if(pump_timers[i] != NULL)
|
||||
pump_disable(i);
|
||||
xTimerStop(pump_timers[i], 0);
|
||||
}
|
||||
}
|
||||
|
@ -112,8 +128,12 @@ uint8_t pumps_init() {
|
|||
memset(&pumps_enabled, 0, sizeof(uint8_t) * PUMPS);
|
||||
memset(&pumps_duration, 10, sizeof(uint8_t) * PUMPS);
|
||||
|
||||
//uint32_t durations;
|
||||
//esp_err_t err = nvs_get_i32(config_handle, "durations", &durations);
|
||||
size_t size = sizeof(uint8_t) * PUMPS;
|
||||
if(!nvs_get_blob(config_handle, "pumps_duration", &pumps_duration, &size)) {
|
||||
ESP_LOGI(TAG, "Initializing pumps config");
|
||||
|
||||
nvs_set_blob(config_handle, "pumps_duration", &pumps_duration, size);
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "pumps_enabled: %d %d %d %d", pumps_enabled[0], pumps_enabled[1], pumps_enabled[2], pumps_enabled[3]);
|
||||
ESP_LOGI(TAG, "pumps_duration: %d %d %d %d", pumps_duration[0], pumps_duration[1], pumps_duration[2], pumps_duration[3]);
|
||||
|
|
|
@ -17,24 +17,26 @@ static void IRAM_ATTR gpio_isr_handler(void* arg) {
|
|||
|
||||
static void gpio_task(void* arg) {
|
||||
uint32_t io_num;
|
||||
uint8_t on = 1;
|
||||
uint8_t off = 0;
|
||||
uint8_t state = 0;
|
||||
int8_t last_level = -1;
|
||||
|
||||
for(;;) {
|
||||
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
||||
uint8_t level = gpio_get_level(io_num);
|
||||
|
||||
if(level == last_level) continue;
|
||||
last_level = level;
|
||||
|
||||
printf("GPIO[%d] intr, val: %d\n", io_num, level);
|
||||
|
||||
if(state == 0)
|
||||
vTaskDelay(10 / portTICK_RATE_MS);
|
||||
|
||||
pumps_run();
|
||||
|
||||
ble_send_notification((void*)&on, 1);
|
||||
state = !state;
|
||||
|
||||
// just a wee debounce
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
|
||||
ble_send_notification((void*)&off, 1);
|
||||
|
||||
xQueueReset(gpio_evt_queue);
|
||||
ble_send_notification((void*)&state, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +44,7 @@ static void gpio_task(void* arg) {
|
|||
void user_button_init() {
|
||||
gpio_config_t io_conf;
|
||||
|
||||
io_conf.intr_type = GPIO_PIN_INTR_NEGEDGE;
|
||||
io_conf.intr_type = GPIO_PIN_INTR_ANYEDGE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << GPIO_USER_BUTTON);
|
||||
io_conf.pull_down_en = 0;
|
||||
|
@ -54,5 +56,5 @@ void user_button_init() {
|
|||
xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 10, NULL);
|
||||
|
||||
gpio_install_isr_service(0);
|
||||
gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*) 0);
|
||||
gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*)GPIO_USER_BUTTON);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue