Compare commits
No commits in common. "e9eb537eee6fe6ad939e193e340568e0fc3443f7" and "b113a70577c0775458b666b8c05e90dbb46e3d1f" have entirely different histories.
e9eb537eee
...
b113a70577
4 changed files with 24 additions and 70 deletions
|
@ -1,13 +1,11 @@
|
|||
#ifndef __MAIN_H_
|
||||
#define __MAIN_H_
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#define PUMP_COUNT (4)
|
||||
|
||||
static QueueHandle_t gpio_evt_queue = NULL;
|
||||
static xQueueHandle gpio_evt_queue = NULL;
|
||||
nvs_handle_t config_handle;
|
||||
|
||||
extern uint8_t mac[6];
|
||||
|
|
42
main/ble.c
42
main/ble.c
|
@ -181,10 +181,9 @@ 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 pumps");
|
||||
ESP_LOGI(TAG, "starting pump");
|
||||
pumps_run();
|
||||
} else {
|
||||
ESP_LOGI(TAG, "stoping pumps");
|
||||
pumps_stop();
|
||||
}
|
||||
break;
|
||||
|
@ -198,15 +197,13 @@ 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 chr_uuid = ble_uuid_u16(ctxt->chr->uuid);
|
||||
|
||||
// match the UUID against 0b111
|
||||
uint8_t idx = (chr_uuid & 0x07) - 1;
|
||||
uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
|
||||
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 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, chr_uuid, (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, uuid16, (ctxt->op == 0 ? "read" : ctxt->op == 1 ? "write" : "unknown"));
|
||||
|
||||
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
||||
gatt_svr_chr_write(
|
||||
|
@ -218,35 +215,24 @@ 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 == 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);
|
||||
|
||||
if(access->write != NULL)
|
||||
access->write(idx, value);
|
||||
}
|
||||
} else if(access != NULL && ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
||||
if(access->read == NULL) {
|
||||
} else if(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 value = access->read(idx);
|
||||
uint8_t enabled = access->read(idx);
|
||||
|
||||
ESP_LOGI(TAG, "returning read idx: %d value: %d", idx, value);
|
||||
os_mbuf_append(ctxt->om, &value, sizeof value);
|
||||
ESP_LOGI(TAG, "pumps_enabled[%d] = %d", idx, enabled);
|
||||
os_mbuf_append(ctxt->om, &enabled, sizeof enabled);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -390,14 +376,6 @@ 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,7 +5,6 @@
|
|||
#include "driver/gpio.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "pumps.h"
|
||||
|
||||
#define TAG "PUMP!"
|
||||
|
@ -18,14 +17,6 @@ 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;
|
||||
|
||||
|
@ -33,8 +24,6 @@ uint8_t pumps_set_duration(uint8_t idx, uint8_t time) {
|
|||
|
||||
pumps_duration[idx] = time;
|
||||
|
||||
pumps_update_config();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -64,14 +53,10 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -87,7 +72,6 @@ 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);
|
||||
}
|
||||
}
|
||||
|
@ -128,12 +112,8 @@ uint8_t pumps_init() {
|
|||
memset(&pumps_enabled, 0, sizeof(uint8_t) * PUMPS);
|
||||
memset(&pumps_duration, 10, sizeof(uint8_t) * PUMPS);
|
||||
|
||||
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);
|
||||
};
|
||||
//uint32_t durations;
|
||||
//esp_err_t err = nvs_get_i32(config_handle, "durations", &durations);
|
||||
|
||||
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,26 +17,24 @@ static void IRAM_ATTR gpio_isr_handler(void* arg) {
|
|||
|
||||
static void gpio_task(void* arg) {
|
||||
uint32_t io_num;
|
||||
uint8_t state = 0;
|
||||
int8_t last_level = -1;
|
||||
uint8_t on = 1;
|
||||
uint8_t off = 0;
|
||||
|
||||
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();
|
||||
|
||||
state = !state;
|
||||
ble_send_notification((void*)&on, 1);
|
||||
|
||||
ble_send_notification((void*)&state, 1);
|
||||
// just a wee debounce
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
|
||||
ble_send_notification((void*)&off, 1);
|
||||
|
||||
xQueueReset(gpio_evt_queue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +42,7 @@ static void gpio_task(void* arg) {
|
|||
void user_button_init() {
|
||||
gpio_config_t io_conf;
|
||||
|
||||
io_conf.intr_type = GPIO_PIN_INTR_ANYEDGE;
|
||||
io_conf.intr_type = GPIO_PIN_INTR_NEGEDGE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << GPIO_USER_BUTTON);
|
||||
io_conf.pull_down_en = 0;
|
||||
|
@ -56,5 +54,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*)GPIO_USER_BUTTON);
|
||||
gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*) 0);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue