Compare commits
	
		
			No commits in common. "development" and "8a62260dc2ae33744d45978900e854b2bd50427a" have entirely different histories.
		
	
	
		
			developmen
			...
			8a62260dc2
		
	
		
					 15 changed files with 86 additions and 395 deletions
				
			
		
							
								
								
									
										21
									
								
								.drone.yml
									
										
									
									
									
								
							
							
						
						
									
										21
									
								
								.drone.yml
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,21 +0,0 @@
 | 
			
		|||
kind: pipeline
 | 
			
		||||
type: docker
 | 
			
		||||
name: default
 | 
			
		||||
 | 
			
		||||
steps:
 | 
			
		||||
  - name: build
 | 
			
		||||
    image: espressif/idf:release-v4.1
 | 
			
		||||
    commands:
 | 
			
		||||
      - mkdir ~/.ssh
 | 
			
		||||
      - ssh-keyscan -t rsa git.oit.cloud >> ~/.ssh/known_hosts
 | 
			
		||||
      - git submodule update --init
 | 
			
		||||
      - if [ -f sdkconfig ]; then
 | 
			
		||||
      -   rm sdkconfig
 | 
			
		||||
      - fi
 | 
			
		||||
      - ls
 | 
			
		||||
      - . /opt/esp/idf/export.sh
 | 
			
		||||
      - idf.py reconfigure
 | 
			
		||||
      - echo "BB-CI-$((1 + RANDOM % 999))" > bbid
 | 
			
		||||
      - sed -i -e s/BARBACK_ID=\"Barback\"/BARBACK_ID=\"`cat bbid`\"/ sdkconfig
 | 
			
		||||
      - idf.py reconfigure
 | 
			
		||||
      - idf.py build
 | 
			
		||||
							
								
								
									
										12
									
								
								.gitmodules
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										12
									
								
								.gitmodules
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -1,9 +1,3 @@
 | 
			
		|||
[submodule "components/configulator"]
 | 
			
		||||
	path = components/configulator
 | 
			
		||||
	url = https://git.oit.cloud/morgan/configulator
 | 
			
		||||
[submodule "esp-idf"]
 | 
			
		||||
	path = esp-idf
 | 
			
		||||
	url = https://github.com/espressif/esp-idf
 | 
			
		||||
[submodule "components/BAROS32"]
 | 
			
		||||
	path = components/BAROS32
 | 
			
		||||
	url = https://git.oit.cloud/Barback/BAROS32.git
 | 
			
		||||
[submodule "components/esp32-lora"]
 | 
			
		||||
	path = components/esp32-lora
 | 
			
		||||
	url = https://gitlab.com/morganrallen/esp32-lora.git
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,27 +0,0 @@
 | 
			
		|||
steps:
 | 
			
		||||
  build:
 | 
			
		||||
    image: espressif/idf:v5.2.2
 | 
			
		||||
    when:
 | 
			
		||||
      - event: [ tag, push, manual ]
 | 
			
		||||
    commands:
 | 
			
		||||
      - git submodule update --init
 | 
			
		||||
      - if [ -f sdkconfig ]; then
 | 
			
		||||
      -   rm sdkconfig
 | 
			
		||||
      - fi
 | 
			
		||||
      - . /opt/esp/idf/export.sh
 | 
			
		||||
      - idf.py reconfigure
 | 
			
		||||
      - idf.py build
 | 
			
		||||
  release:
 | 
			
		||||
    image: woodpeckerci/plugin-release
 | 
			
		||||
    when:
 | 
			
		||||
      - event: tag
 | 
			
		||||
    settings:
 | 
			
		||||
      log_level: debug
 | 
			
		||||
      title: 'Release ${CI_COMMIT_TAG}'
 | 
			
		||||
      files:
 | 
			
		||||
        - 'build/barback.bin'
 | 
			
		||||
      api_key:
 | 
			
		||||
        from_secret: ACCESS_TOKEN
 | 
			
		||||
      generate_release_notes: true
 | 
			
		||||
      overwrite: true
 | 
			
		||||
      upload-url: https://git.oit.cloud/repos/${CI_REPO}/releases
 | 
			
		||||
| 
						 | 
				
			
			@ -3,5 +3,4 @@
 | 
			
		|||
cmake_minimum_required(VERSION 3.5)
 | 
			
		||||
 | 
			
		||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 | 
			
		||||
 | 
			
		||||
project(barback)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +0,0 @@
 | 
			
		|||
Subproject commit 54c7a2d2d65dc9159dad27d6388d8a5409b59dd0
 | 
			
		||||
| 
						 | 
				
			
			@ -1 +0,0 @@
 | 
			
		|||
Subproject commit af52863f024eeed3c4730e2a7b945a18e9bfb9fb
 | 
			
		||||
							
								
								
									
										1
									
								
								esp-idf
									
										
									
									
									
								
							
							
						
						
									
										1
									
								
								esp-idf
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1 +0,0 @@
 | 
			
		|||
Subproject commit 3b8741b172dc951e18509698dee938304bcf1523
 | 
			
		||||
| 
						 | 
				
			
			@ -1,14 +1,12 @@
 | 
			
		|||
#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;
 | 
			
		||||
extern nvs_handle_t config_handle;
 | 
			
		||||
static xQueueHandle gpio_evt_queue = NULL;
 | 
			
		||||
nvs_handle_t config_handle;
 | 
			
		||||
 | 
			
		||||
extern uint8_t mac[6];
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,12 +7,10 @@
 | 
			
		|||
#define PUMPS_ERR_OUT_IDX   (1)
 | 
			
		||||
#define GPIO_PUMP_PIN_SEL   ((1ULL<<pump_gpio_map[0]) | (1ULL<<pump_gpio_map[1]) |  (1ULL<<pump_gpio_map[2]) | (1ULL<<pump_gpio_map[3]));
 | 
			
		||||
 | 
			
		||||
static uint8_t pump_gpio_map[] = { 12, 13, 2, 4 };
 | 
			
		||||
extern uint8_t running;
 | 
			
		||||
static uint8_t pump_gpio_map[] = { 27, 26, 25, 33 };
 | 
			
		||||
 | 
			
		||||
void pumps_run();
 | 
			
		||||
void pumps_stop();
 | 
			
		||||
uint8_t pumps_io_init();
 | 
			
		||||
uint8_t pumps_init();
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_set_duration(uint8_t idx, uint8_t time);
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +19,6 @@ uint8_t pumps_get_duration(uint8_t idx);
 | 
			
		|||
uint8_t pumps_set_enabled(uint8_t idx, uint8_t value);
 | 
			
		||||
uint8_t pumps_get_enabled(uint8_t idx);
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_get_running(uint8_t idx);
 | 
			
		||||
uint8_t pumps_get_state(uint8_t idx);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,6 +6,5 @@ set(COMPONENT_SRCS "\
 | 
			
		|||
  user_button.c\
 | 
			
		||||
")
 | 
			
		||||
set(COMPONENT_ADD_INCLUDEDIRS ". ../include")
 | 
			
		||||
set(REQUIRES configulato BAROS32)
 | 
			
		||||
 | 
			
		||||
register_component()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										85
									
								
								main/ble.c
									
										
									
									
									
								
							
							
						
						
									
										85
									
								
								main/ble.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -16,7 +16,6 @@
 | 
			
		|||
#include "driver/gpio.h"
 | 
			
		||||
 | 
			
		||||
#include "barback.h"
 | 
			
		||||
#include "baros.h"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "ble.h"
 | 
			
		||||
#include "pumps.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -47,13 +46,21 @@ 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);
 | 
			
		||||
 | 
			
		||||
#define BLE_SERVICE_PUMP_ENABLED        (0x4200)
 | 
			
		||||
#define BLE_SERVICE_PUMP_STATE          (0x4300)
 | 
			
		||||
#define BLE_SERVICE_PUMP_DURATION       (0x4350)
 | 
			
		||||
 | 
			
		||||
#define BLE_SERVICE_BARBACK             (0x4400)
 | 
			
		||||
#define CHAR_POUR                       (BLE_SERVICE_BARBACK + 1)
 | 
			
		||||
#define CHAR_BUTTON                     (BLE_SERVICE_BARBACK + 2)
 | 
			
		||||
 | 
			
		||||
static barback_ble_access_t barback_ble_pump_access_enabled = {
 | 
			
		||||
  .read = pumps_get_enabled,
 | 
			
		||||
  .write = pumps_set_enabled
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#define BLE_CHAR_PUMP_ENABLED(idx) {\
 | 
			
		||||
  .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_PUMP_ENABLED + idx),\
 | 
			
		||||
  .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED + idx),\
 | 
			
		||||
  .access_cb = barback_ble_char_access,\
 | 
			
		||||
  .arg = &barback_ble_pump_access_enabled,\
 | 
			
		||||
  .val_handle = &ble_svc_handle,\
 | 
			
		||||
| 
						 | 
				
			
			@ -61,11 +68,11 @@ static barback_ble_access_t barback_ble_pump_access_enabled = {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
static barback_ble_access_t barback_ble_pump_access_state = {
 | 
			
		||||
  .read = pumps_get_running,
 | 
			
		||||
  .read = pumps_get_state,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#define BLE_CHAR_PUMP_STATE(idx) {\
 | 
			
		||||
  .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_PUMP_STATE + idx),\
 | 
			
		||||
  .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE + idx),\
 | 
			
		||||
  .access_cb = barback_ble_char_access,\
 | 
			
		||||
  .arg = &barback_ble_pump_access_state,\
 | 
			
		||||
  .val_handle = &ble_svc_handle,\
 | 
			
		||||
| 
						 | 
				
			
			@ -78,7 +85,7 @@ static barback_ble_access_t barback_ble_pump_access_duration = {
 | 
			
		|||
};
 | 
			
		||||
 | 
			
		||||
#define BLE_CHAR_PUMP_DURATION(idx) {\
 | 
			
		||||
  .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_PUMP_DURATION + idx),\
 | 
			
		||||
  .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
 | 
			
		||||
  .access_cb = barback_ble_char_access,\
 | 
			
		||||
  .arg = &barback_ble_pump_access_duration,\
 | 
			
		||||
  .val_handle = &pump_duration_svc_val_handles[idx - 1],\
 | 
			
		||||
| 
						 | 
				
			
			@ -88,7 +95,7 @@ static barback_ble_access_t barback_ble_pump_access_duration = {
 | 
			
		|||
static const struct ble_gatt_svc_def service_defs[] = {
 | 
			
		||||
  {
 | 
			
		||||
    .type = BLE_GATT_SVC_TYPE_PRIMARY,
 | 
			
		||||
    .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_PUMP_ENABLED),
 | 
			
		||||
    .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED),
 | 
			
		||||
 | 
			
		||||
    .characteristics = (struct ble_gatt_chr_def[]) {
 | 
			
		||||
      BLE_CHAR_PUMP_ENABLED(1),
 | 
			
		||||
| 
						 | 
				
			
			@ -99,7 +106,7 @@ static const struct ble_gatt_svc_def service_defs[] = {
 | 
			
		|||
    },
 | 
			
		||||
  }, {
 | 
			
		||||
    .type = BLE_GATT_SVC_TYPE_PRIMARY,
 | 
			
		||||
    .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_PUMP_STATE),
 | 
			
		||||
    .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE),
 | 
			
		||||
    .characteristics = (struct ble_gatt_chr_def[]) {
 | 
			
		||||
      BLE_CHAR_PUMP_STATE(1),
 | 
			
		||||
      BLE_CHAR_PUMP_STATE(2),
 | 
			
		||||
| 
						 | 
				
			
			@ -109,7 +116,7 @@ static const struct ble_gatt_svc_def service_defs[] = {
 | 
			
		|||
    },
 | 
			
		||||
  }, {
 | 
			
		||||
    .type = BLE_GATT_SVC_TYPE_PRIMARY,
 | 
			
		||||
      .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_PUMP_DURATION),
 | 
			
		||||
      .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION),
 | 
			
		||||
      .characteristics = (struct ble_gatt_chr_def[]) {
 | 
			
		||||
        BLE_CHAR_PUMP_DURATION(1),
 | 
			
		||||
        BLE_CHAR_PUMP_DURATION(2),
 | 
			
		||||
| 
						 | 
				
			
			@ -119,15 +126,15 @@ static const struct ble_gatt_svc_def service_defs[] = {
 | 
			
		|||
      },
 | 
			
		||||
  }, {
 | 
			
		||||
    .type = BLE_GATT_SVC_TYPE_PRIMARY,
 | 
			
		||||
      .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_BARBACK),
 | 
			
		||||
      .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_BARBACK),
 | 
			
		||||
      .characteristics = (struct ble_gatt_chr_def[]) {
 | 
			
		||||
        {
 | 
			
		||||
          .uuid = BLE_UUID16_DECLARE(BAROS_CHAR_POUR),
 | 
			
		||||
          .uuid = BLE_UUID16_DECLARE(CHAR_POUR),
 | 
			
		||||
          .access_cb = svc_access_system,
 | 
			
		||||
          .val_handle = &ble_svc_handle,
 | 
			
		||||
          .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
 | 
			
		||||
        }, {
 | 
			
		||||
          .uuid = BLE_UUID16_DECLARE(BAROS_CHAR_BUTTON),
 | 
			
		||||
          .uuid = BLE_UUID16_DECLARE(CHAR_BUTTON),
 | 
			
		||||
          .access_cb = svc_access_system,
 | 
			
		||||
          .val_handle = &svc_handle_button,
 | 
			
		||||
          .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY
 | 
			
		||||
| 
						 | 
				
			
			@ -138,7 +145,8 @@ static const struct ble_gatt_svc_def service_defs[] = {
 | 
			
		|||
  { 0 } // no more services
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len) {
 | 
			
		||||
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
 | 
			
		||||
    void *dst, uint16_t *len) {
 | 
			
		||||
  uint16_t om_len;
 | 
			
		||||
  int rc;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -158,7 +166,6 @@ static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max
 | 
			
		|||
static int svc_access_system(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 value = 0;
 | 
			
		||||
  uint16_t vlen = 0;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -168,23 +175,20 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
 | 
			
		|||
        ctxt->om,
 | 
			
		||||
        sizeof value,
 | 
			
		||||
        sizeof value,
 | 
			
		||||
        &value, &vlen
 | 
			
		||||
        &value, NULL
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "value (%d): %d", vlen, value);
 | 
			
		||||
 | 
			
		||||
    switch(uuid16) {
 | 
			
		||||
      case BAROS_CHAR_POUR:
 | 
			
		||||
      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;
 | 
			
		||||
 | 
			
		||||
      case BAROS_CHAR_BUTTON:
 | 
			
		||||
      case CHAR_BUTTON:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -193,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(
 | 
			
		||||
| 
						 | 
				
			
			@ -213,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;
 | 
			
		||||
| 
						 | 
				
			
			@ -385,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,
 | 
			
		||||
| 
						 | 
				
			
			@ -461,7 +444,7 @@ void ble_init(char *name) {
 | 
			
		|||
 | 
			
		||||
  memset(&callback_handlers, 0, sizeof(event_callback_handle_t) * CONFIG_CACO_MAX_SERVICES);
 | 
			
		||||
 | 
			
		||||
  //ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
 | 
			
		||||
  ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
 | 
			
		||||
 | 
			
		||||
  nimble_port_init();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,6 +25,7 @@ void console_task(void *args) {
 | 
			
		|||
    .data_bits = UART_DATA_8_BITS,
 | 
			
		||||
    .parity = UART_PARITY_DISABLE,
 | 
			
		||||
    .stop_bits = UART_STOP_BITS_1,
 | 
			
		||||
    .use_ref_tick = true
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										74
									
								
								main/main.c
									
										
									
									
									
								
							
							
						
						
									
										74
									
								
								main/main.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -5,58 +5,21 @@
 | 
			
		|||
#include "freertos/queue.h"
 | 
			
		||||
#include "driver/uart.h"
 | 
			
		||||
#include "esp_log.h"
 | 
			
		||||
#include "esp_mac.h"
 | 
			
		||||
#include "esp_debug_helpers.h"
 | 
			
		||||
#include "nvs_flash.h"
 | 
			
		||||
#include "esp_vfs_dev.h"
 | 
			
		||||
 | 
			
		||||
#include "led_strip.h"
 | 
			
		||||
 | 
			
		||||
#include "configulator.h"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "console.h"
 | 
			
		||||
#include "ble.h"
 | 
			
		||||
#include "user_button.h"
 | 
			
		||||
#include "pumps.h"
 | 
			
		||||
 | 
			
		||||
#define LED_GPIO 14
 | 
			
		||||
#define LED_COUNT 5
 | 
			
		||||
 | 
			
		||||
static const char *TAG = "BARBACK";
 | 
			
		||||
 | 
			
		||||
uint8_t mac[6];
 | 
			
		||||
nvs_handle_t config_handle = {};
 | 
			
		||||
led_strip_handle_t led_strip;
 | 
			
		||||
 | 
			
		||||
void led_init() {
 | 
			
		||||
  led_strip_config_t strip_config = {
 | 
			
		||||
      .strip_gpio_num = LED_GPIO,  // The GPIO that connected to the LED strip's data line
 | 
			
		||||
      .max_leds = LED_COUNT,                 // The number of LEDs in the strip,
 | 
			
		||||
      .led_model = LED_MODEL_WS2812, // LED strip model, it determines the bit timing
 | 
			
		||||
      .color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB, // The color component format is G-R-B
 | 
			
		||||
      .flags = {
 | 
			
		||||
          .invert_out = false, // don't invert the output signal
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  /// RMT backend specific configuration
 | 
			
		||||
  led_strip_rmt_config_t rmt_config = {
 | 
			
		||||
      .clk_src = RMT_CLK_SRC_DEFAULT,    // different clock source can lead to different power consumption
 | 
			
		||||
      .resolution_hz = 10 * 1000 * 1000, // RMT counter clock frequency: 10MHz
 | 
			
		||||
      .mem_block_symbols = 64,           // the memory size of each RMT channel, in words (4 bytes)
 | 
			
		||||
      .flags = {
 | 
			
		||||
          .with_dma = false, // DMA feature is available on chips like ESP32-S3/P4
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  /// Create the LED strip object
 | 
			
		||||
  ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void app_main(void) {
 | 
			
		||||
  // init pump IO immediately to limit run away pumps in case power is on
 | 
			
		||||
  pumps_io_init();
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = nvs_flash_init();
 | 
			
		||||
 | 
			
		||||
  if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
 | 
			
		||||
| 
						 | 
				
			
			@ -69,7 +32,7 @@ void app_main(void) {
 | 
			
		|||
  if (err != ESP_OK) {
 | 
			
		||||
    printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "config_handle: %d", (int)config_handle);
 | 
			
		||||
    ESP_LOGI(TAG, "config_handle: %d", config_handle);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  esp_efuse_mac_get_default((uint8_t*)&mac);
 | 
			
		||||
| 
						 | 
				
			
			@ -79,46 +42,13 @@ void app_main(void) {
 | 
			
		|||
  char *name = malloc(strlen(TAG) + 4);
 | 
			
		||||
  sprintf(name, "%s-%02X", TAG, mac[5]);
 | 
			
		||||
 | 
			
		||||
  uint8_t tank_levels[] = { 100, 100, 100, 100 };
 | 
			
		||||
  uint16_t pump_ramp_ms[] = { 500, 500, 500, 500 };
 | 
			
		||||
 | 
			
		||||
  cfglr_handle_t cfglr_handle = {
 | 
			
		||||
    .namespace = "configulator",
 | 
			
		||||
    .backend = CFGLR_BACKEND_NVS(),
 | 
			
		||||
    .elements = {
 | 
			
		||||
      CFGLR_ELEMENT_U8("armed", 1, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      CFGLR_ELEMENT_U8("monitor", 0, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      CFGLR_ELEMENT_U8("pour_button", 19, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      CFGLR_ELEMENT_STR("device_name", 32, name, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      CFGLR_ELEMENT_BIN("tank_levels", 4, tank_levels, 4, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      CFGLR_ELEMENT_BIN("pump_pins", 4, pump_gpio_map, 4, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      CFGLR_ELEMENT_BIN("pump_ramp_ms", sizeof(uint16_t) * 4, pump_ramp_ms, sizeof(uint16_t) * 4, CFGLR_SIGNALER_IDF_EVENT()),
 | 
			
		||||
      { NULL },
 | 
			
		||||
    },
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  uint8_t ret = cfglr_init(&cfglr_handle);
 | 
			
		||||
 | 
			
		||||
  vTaskDelay(1000 / portTICK_PERIOD_MS);
 | 
			
		||||
 | 
			
		||||
  user_button_init();
 | 
			
		||||
  pumps_init();
 | 
			
		||||
  ble_init(name);
 | 
			
		||||
 | 
			
		||||
  xTaskCreate(console_task, "console", 4048, NULL, tskIDLE_PRIORITY + 3, NULL);
 | 
			
		||||
 | 
			
		||||
  led_init();
 | 
			
		||||
 | 
			
		||||
  uint16_t tick = 0;
 | 
			
		||||
 | 
			
		||||
  while(1) {
 | 
			
		||||
    uint16_t delay = running > 0 ? 500 / running : 500;
 | 
			
		||||
    vTaskDelay(delay / portTICK_PERIOD_MS);
 | 
			
		||||
 | 
			
		||||
    uint8_t color = tick % (LED_COUNT * 2) > LED_COUNT ? 0 : 100;
 | 
			
		||||
 | 
			
		||||
    ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, tick % LED_COUNT, 5, 5, color));
 | 
			
		||||
    ESP_ERROR_CHECK(led_strip_refresh(led_strip));
 | 
			
		||||
    tick++;
 | 
			
		||||
    vTaskDelay(1000 / portTICK_PERIOD_MS);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										212
									
								
								main/pumps.c
									
										
									
									
									
								
							
							
						
						
									
										212
									
								
								main/pumps.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,63 +1,22 @@
 | 
			
		|||
#include "string.h"
 | 
			
		||||
#include "freertos/FreeRTOS.h"
 | 
			
		||||
#include "freertos/timers.h"
 | 
			
		||||
#include "freertos/queue.h"
 | 
			
		||||
#include "esp_log.h"
 | 
			
		||||
#include "driver/gpio.h"
 | 
			
		||||
#include "driver/ledc.h"
 | 
			
		||||
#include "nvs_flash.h"
 | 
			
		||||
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "pumps.h"
 | 
			
		||||
 | 
			
		||||
#define TAG     "PUMP!"
 | 
			
		||||
 | 
			
		||||
uint8_t running = 0;
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
  PUMP_STATE_IDLE = 0,
 | 
			
		||||
  PUMP_STATE_RAMPING_UP,
 | 
			
		||||
  PUMP_STATE_RUNNING,
 | 
			
		||||
  PUMP_STATE_RAMPING_DOWN,
 | 
			
		||||
} pump_states_e;
 | 
			
		||||
 | 
			
		||||
TimerHandle_t pump_timers[PUMPS];
 | 
			
		||||
QueueHandle_t pump_step_queue;
 | 
			
		||||
 | 
			
		||||
static pump_states_e pump_states[PUMPS] = { PUMP_STATE_IDLE };
 | 
			
		||||
static uint8_t pumps_duration[PUMPS] = {0};
 | 
			
		||||
static uint8_t pumps_enabled[PUMPS] = {0};
 | 
			
		||||
static uint8_t pumps_running[PUMPS] = {0};
 | 
			
		||||
static uint8_t pumps_state[PUMPS] = {0};
 | 
			
		||||
static uint8_t safety = 1;
 | 
			
		||||
 | 
			
		||||
#define RAMP_UP_TIME            (500)
 | 
			
		||||
#define RAMP_DOWN_TIME          (250)
 | 
			
		||||
 | 
			
		||||
#define LEDC_HS_TIMER           LEDC_TIMER_0
 | 
			
		||||
#define LEDC_HS_MODE            LEDC_HIGH_SPEED_MODE
 | 
			
		||||
#define LEDC_LS_TIMER           LEDC_TIMER_1
 | 
			
		||||
#define LEDC_LS_MODE            LEDC_LOW_SPEED_MODE
 | 
			
		||||
 | 
			
		||||
#define LEDC_CONFIG(CHANNEL_NO, GPIO) (ledc_channel_config_t){\
 | 
			
		||||
  .channel = CHANNEL_NO,\
 | 
			
		||||
  .duty = 0,\
 | 
			
		||||
  .gpio_num = GPIO,\
 | 
			
		||||
  .speed_mode = LEDC_LOW_SPEED_MODE,\
 | 
			
		||||
  .hpoint = 0,\
 | 
			
		||||
  .timer_sel = LEDC_TIMER_1,\
 | 
			
		||||
  .flags.output_invert = 0,\
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ledc_channel_config_t ledc_motor_channels[4] = {};
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -65,21 +24,15 @@ uint8_t pumps_set_duration(uint8_t idx, uint8_t time) {
 | 
			
		|||
 | 
			
		||||
  pumps_duration[idx] = time;
 | 
			
		||||
 | 
			
		||||
  pumps_update_config();
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_get_duration(uint8_t idx) {
 | 
			
		||||
  return pumps_duration[idx];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_set_enabled(uint8_t idx, uint8_t value) {
 | 
			
		||||
  uint8_t io = pump_gpio_map[idx];
 | 
			
		||||
 | 
			
		||||
  value = (value ? 1 : 0);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "gpio: %d value: %d", io, value);
 | 
			
		||||
  ESP_LOGD(TAG, "gpio: %d value: %d", io, value);
 | 
			
		||||
  // invert IO
 | 
			
		||||
  if(safety)
 | 
			
		||||
    gpio_set_level(io, value);
 | 
			
		||||
| 
						 | 
				
			
			@ -91,186 +44,79 @@ uint8_t pumps_get_enabled(uint8_t idx) {
 | 
			
		|||
  return pumps_enabled[idx] ? 1 : 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_get_running(uint8_t idx) {
 | 
			
		||||
  return pumps_running[idx];
 | 
			
		||||
uint8_t pumps_get_duration(uint8_t idx) {
 | 
			
		||||
  return pumps_duration[idx];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_get_state(uint8_t idx) {
 | 
			
		||||
  return pumps_state[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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pump_timer_done(TimerHandle_t timer) {
 | 
			
		||||
  uint8_t idx = (pcTimerGetName(timer) - 0x48);
 | 
			
		||||
  uint8_t idx = (pcTimerGetTimerName(timer) - 0x48);
 | 
			
		||||
  ESP_LOGD(TAG, "pump done: %d", idx);
 | 
			
		||||
 | 
			
		||||
  xQueueSend(pump_step_queue, &idx, portMAX_DELAY);
 | 
			
		||||
  running--;
 | 
			
		||||
 | 
			
		||||
  pumps_set_enabled(idx, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pumps_stop() {
 | 
			
		||||
  for(uint8_t i = 0; i < PUMPS; i++) {
 | 
			
		||||
    if(pump_timers[i] != NULL) {
 | 
			
		||||
      pump_disable(i);
 | 
			
		||||
    if(pump_timers[i] != NULL)
 | 
			
		||||
      xTimerStop(pump_timers[i], 0);
 | 
			
		||||
  }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pumps_run() {
 | 
			
		||||
  if(running > 0) return;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "Running pumps");
 | 
			
		||||
 | 
			
		||||
  for(uint8_t i = 0; i < PUMPS; i++) {
 | 
			
		||||
    //if(pumps_duration[i] == 0 || pumps_enabled[i] != 1) continue;
 | 
			
		||||
 | 
			
		||||
    if(pump_states[i] != PUMP_STATE_IDLE) {
 | 
			
		||||
      ESP_LOGW(TAG, "Attempting to start pump in non-IDLE state. idx: %d state: %d", i, pump_states[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ledc_set_fade_with_time(ledc_motor_channels[i].speed_mode, ledc_motor_channels[i].channel, 8192, RAMP_UP_TIME);
 | 
			
		||||
    ledc_fade_start(ledc_motor_channels[i].speed_mode, ledc_motor_channels[i].channel, LEDC_FADE_NO_WAIT);
 | 
			
		||||
 | 
			
		||||
    pump_states[i] = PUMP_STATE_RAMPING_UP;
 | 
			
		||||
    if(pumps_duration[i] == 0) continue;
 | 
			
		||||
 | 
			
		||||
    running++;
 | 
			
		||||
    ESP_LOGI(TAG, "Pump %d started", i);
 | 
			
		||||
 | 
			
		||||
    pump_timers[i] = xTimerCreate((const char *)(0x48 + i), (pumps_duration[i] * 1000 / portTICK_PERIOD_MS), pdFALSE, (void*)0, pump_timer_done);
 | 
			
		||||
 | 
			
		||||
    xTimerStart(pump_timers[i], 0);
 | 
			
		||||
 | 
			
		||||
    pumps_set_enabled(i, 1);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pump_step_task() {
 | 
			
		||||
  uint8_t idx;
 | 
			
		||||
  BaseType_t awoke = pdFALSE;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "Starting pump stepping task");
 | 
			
		||||
 | 
			
		||||
  while(xQueueReceive(pump_step_queue, &idx, &awoke)) {
 | 
			
		||||
    ESP_LOGI(TAG, "step %d complete for %d", pump_states[idx], idx);
 | 
			
		||||
 | 
			
		||||
    if(pump_states[idx] == PUMP_STATE_RAMPING_UP) {
 | 
			
		||||
      uint16_t pump_time = pumps_duration[idx] * 250 - (RAMP_UP_TIME + RAMP_DOWN_TIME) / 2;
 | 
			
		||||
 | 
			
		||||
      if(RAMP_UP_TIME + RAMP_DOWN_TIME / 2 > pumps_duration[idx] * 250) {
 | 
			
		||||
        ESP_LOGW(TAG, "Pump %d total ramp time great that duration, running for 1 Tick", idx);
 | 
			
		||||
 | 
			
		||||
        pump_time = 10;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      ESP_LOGI(TAG, "Running pump %d for %dms", idx, pump_time);
 | 
			
		||||
 | 
			
		||||
      xTimerChangePeriod(pump_timers[idx], pump_time / portTICK_PERIOD_MS, portMAX_DELAY);
 | 
			
		||||
      xTimerStart(pump_timers[idx], portMAX_DELAY);
 | 
			
		||||
 | 
			
		||||
      pump_states[idx] = PUMP_STATE_RUNNING;
 | 
			
		||||
 | 
			
		||||
      continue;
 | 
			
		||||
    } else if(pump_states[idx] == PUMP_STATE_RUNNING) {
 | 
			
		||||
      ledc_set_fade_with_time(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, 0, RAMP_UP_TIME);
 | 
			
		||||
      ledc_fade_start(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, LEDC_FADE_NO_WAIT);
 | 
			
		||||
 | 
			
		||||
      pump_states[idx] = PUMP_STATE_RAMPING_DOWN;
 | 
			
		||||
      continue;
 | 
			
		||||
    } else if(pump_states[idx] == PUMP_STATE_RAMPING_DOWN) {
 | 
			
		||||
      ESP_LOGI(TAG, "Pump cycles complete: %d", idx);
 | 
			
		||||
      pump_states[idx] = PUMP_STATE_IDLE;
 | 
			
		||||
 | 
			
		||||
      running--;
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGE(TAG, "Pump step fired with unknown state: %d", pump_states[idx]);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static IRAM_ATTR bool ledc_fade_end_event(const ledc_cb_param_t *param, void *parg) {
 | 
			
		||||
  BaseType_t awoke = pdFALSE;
 | 
			
		||||
 | 
			
		||||
  xQueueSendFromISR(pump_step_queue, parg, &awoke);
 | 
			
		||||
 | 
			
		||||
  return (awoke == pdTRUE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_io_init() {
 | 
			
		||||
uint8_t pumps_init() {
 | 
			
		||||
  gpio_config_t io_conf;
 | 
			
		||||
 | 
			
		||||
  for(uint8_t i = 0 ; i < PUMPS; i++) {
 | 
			
		||||
  // put the output high before initializing
 | 
			
		||||
  // to prevent relays from clacking
 | 
			
		||||
  for(uint8_t i = 0 ; i < 4; i++) {
 | 
			
		||||
    gpio_set_level(pump_gpio_map[i], 0);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  io_conf.intr_type = GPIO_INTR_DISABLE;
 | 
			
		||||
  io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
 | 
			
		||||
  io_conf.mode = GPIO_MODE_OUTPUT;
 | 
			
		||||
  io_conf.pin_bit_mask = GPIO_PUMP_PIN_SEL;
 | 
			
		||||
  io_conf.pull_down_en = 1;
 | 
			
		||||
  io_conf.pull_up_en = 0;
 | 
			
		||||
  gpio_config(&io_conf);
 | 
			
		||||
 | 
			
		||||
  return gpio_config(&io_conf);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t pumps_init() {
 | 
			
		||||
  // zero out pump enabled, duration
 | 
			
		||||
  memset(&pumps_enabled, 0, sizeof(uint8_t) * PUMPS);
 | 
			
		||||
  memset(&pumps_duration, 5, sizeof(uint8_t) * PUMPS);
 | 
			
		||||
  memset(&pumps_duration, 10, sizeof(uint8_t) * PUMPS);
 | 
			
		||||
 | 
			
		||||
  // this should neve go above 4, but just in case?
 | 
			
		||||
  pump_step_queue = xQueueCreate(PUMPS * 4, sizeof(uint8_t));
 | 
			
		||||
 | 
			
		||||
  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);
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  ledc_timer_config_t ledc_timer = {
 | 
			
		||||
    .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
 | 
			
		||||
    .freq_hz = 4000,                      // frequency of PWM signal
 | 
			
		||||
    .speed_mode = LEDC_LS_MODE,           // timer mode
 | 
			
		||||
    .timer_num = LEDC_LS_TIMER,            // timer index
 | 
			
		||||
    .clk_cfg = LEDC_AUTO_CLK,              // Auto select the source clock
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  ledc_timer_config(&ledc_timer);
 | 
			
		||||
 | 
			
		||||
  ledc_timer.speed_mode = LEDC_HS_MODE;
 | 
			
		||||
  ledc_timer.timer_num = LEDC_HS_TIMER;
 | 
			
		||||
 | 
			
		||||
  ledc_timer_config(&ledc_timer);
 | 
			
		||||
 | 
			
		||||
  for(uint8_t i = 0; i < PUMPS; i++) {
 | 
			
		||||
    gpio_set_level(pump_gpio_map[i], 0);
 | 
			
		||||
 | 
			
		||||
    pump_timers[i] = xTimerCreate((const char *)(0x48 + i), 1, pdFALSE, (void*)0, pump_timer_done);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for(uint8_t i = 0; i < PUMPS; i++) {
 | 
			
		||||
    ledc_motor_channels[i] = LEDC_CONFIG(i, pump_gpio_map[i]);
 | 
			
		||||
    ledc_channel_config(&ledc_motor_channels[i]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ledc_cbs_t callbacks = {
 | 
			
		||||
    .fade_cb = ledc_fade_end_event
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  ledc_fade_func_install(0);
 | 
			
		||||
 | 
			
		||||
  uint8_t *idx;
 | 
			
		||||
 | 
			
		||||
  for(uint8_t i = 0; i < PUMPS; i++) {
 | 
			
		||||
    idx = malloc(1);
 | 
			
		||||
    memcpy(idx, &i, 1);
 | 
			
		||||
    ledc_cb_register(ledc_motor_channels[i].speed_mode, ledc_motor_channels[i].channel, &callbacks, idx);
 | 
			
		||||
  }
 | 
			
		||||
  //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]);
 | 
			
		||||
 | 
			
		||||
  xTaskCreate(pump_step_task, "ramp", 4024, NULL, tskIDLE_PRIORITY + 4, NULL);
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,14 +2,12 @@
 | 
			
		|||
#include "freertos/task.h"
 | 
			
		||||
#include "freertos/queue.h"
 | 
			
		||||
#include "driver/gpio.h"
 | 
			
		||||
#include "esp_log.h"
 | 
			
		||||
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "pumps.h"
 | 
			
		||||
#include "ble.h"
 | 
			
		||||
 | 
			
		||||
#define GPIO_USER_BUTTON      (19)
 | 
			
		||||
#define TAG "user_btn"
 | 
			
		||||
 | 
			
		||||
static void IRAM_ATTR gpio_isr_handler(void* arg) {
 | 
			
		||||
  uint32_t gpio_num = (uint32_t) arg;
 | 
			
		||||
| 
						 | 
				
			
			@ -19,36 +17,32 @@ 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;
 | 
			
		||||
 | 
			
		||||
      ESP_LOGI(TAG, "GPIO[%ld] intr, val: %d\n", io_num, level);
 | 
			
		||||
 | 
			
		||||
      if(state == 0)
 | 
			
		||||
        vTaskDelay(10 / portTICK_PERIOD_MS);
 | 
			
		||||
      printf("GPIO[%d] intr, val: %d\n", io_num, level);
 | 
			
		||||
 | 
			
		||||
      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);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void user_button_init() {
 | 
			
		||||
  ESP_LOGI(TAG, "user_button_init");
 | 
			
		||||
 | 
			
		||||
  gpio_config_t io_conf;
 | 
			
		||||
 | 
			
		||||
  io_conf.intr_type = GPIO_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;
 | 
			
		||||
| 
						 | 
				
			
			@ -60,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…
	
	Add table
		
		Reference in a new issue