mirror of
https://github.com/Valeh2012/PersonalVotingMachine
synced 2025-12-10 18:45:17 +02:00
first commit
This commit is contained in:
345
basic-setup/main/impl/modules/BLEModule.cpp
Normal file
345
basic-setup/main/impl/modules/BLEModule.cpp
Normal file
@@ -0,0 +1,345 @@
|
||||
/**
|
||||
* @file BLEModule.cpp
|
||||
* @brief BLEModule implementation file
|
||||
* */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
#include "esp_gatts_api.h"
|
||||
#include "esp_bt_defs.h"
|
||||
#include "esp_bt_main.h"
|
||||
#include "esp_gatt_common_api.h"
|
||||
#include "module.h"
|
||||
|
||||
static uint8_t adv_config_done = 0;
|
||||
#define adv_config_flag (1 << 0)
|
||||
#define scan_rsp_config_flag (1 << 1)
|
||||
|
||||
#define GATTS_TAG "GATTS"
|
||||
|
||||
static esp_ble_adv_params_t adv_params = {
|
||||
.adv_int_min = 0x20,
|
||||
.adv_int_max = 0x40,
|
||||
.adv_type = ADV_TYPE_IND,
|
||||
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
|
||||
.peer_addr = {},
|
||||
.peer_addr_type = {},
|
||||
.channel_map = ADV_CHNL_ALL,
|
||||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
||||
};
|
||||
|
||||
static uint8_t adv_service_uuid128[32] = {
|
||||
/* LSB <--------------------------------------------------------------------------------> MSB */
|
||||
//first uuid, 16bit, [12],[13] is the value
|
||||
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xEE, 0x00, 0x00, 0x00,
|
||||
//second uuid, 32bit, [12], [13], [14], [15] is the value
|
||||
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
// The length of adv data must be less than 31 bytes
|
||||
//static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x12, 0x23, 0x45, 0x56};
|
||||
//adv data
|
||||
static esp_ble_adv_data_t adv_data = {
|
||||
.set_scan_rsp = false,
|
||||
.include_name = true,
|
||||
.include_txpower = true,
|
||||
.min_interval = 0x0006, //slave connection min interval, Time = min_interval * 1.25 msec
|
||||
.max_interval = 0x0010, //slave connection max interval, Time = max_interval * 1.25 msec
|
||||
.appearance = 0x00,
|
||||
.manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
|
||||
.p_manufacturer_data = NULL, //&test_manufacturer[0],
|
||||
.service_data_len = 0,
|
||||
.p_service_data = NULL,
|
||||
.service_uuid_len = sizeof(adv_service_uuid128),
|
||||
.p_service_uuid = adv_service_uuid128,
|
||||
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
||||
};
|
||||
// scan response data
|
||||
static esp_ble_adv_data_t scan_rsp_data = {
|
||||
.set_scan_rsp = true,
|
||||
.include_name = true,
|
||||
.include_txpower = true,
|
||||
.min_interval = 0x0006,
|
||||
.max_interval = 0x0010,
|
||||
.appearance = 0x00,
|
||||
.manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
|
||||
.p_manufacturer_data = NULL, //&test_manufacturer[0],
|
||||
.service_data_len = 0,
|
||||
.p_service_data = NULL,
|
||||
.service_uuid_len = sizeof(adv_service_uuid128),
|
||||
.p_service_uuid = adv_service_uuid128,
|
||||
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
||||
};
|
||||
|
||||
#define adv_config_flag (1 << 0)
|
||||
#define scan_rsp_config_flag (1 << 1)
|
||||
|
||||
static char *esp_key_type_to_str(esp_ble_key_type_t key_type){
|
||||
|
||||
char *key_str = NULL;
|
||||
switch(key_type) {
|
||||
case ESP_LE_KEY_NONE:
|
||||
key_str = "ESP_LE_KEY_NONE";
|
||||
break;
|
||||
case ESP_LE_KEY_PENC:
|
||||
key_str = "ESP_LE_KEY_PENC";
|
||||
break;
|
||||
case ESP_LE_KEY_PID:
|
||||
key_str = "ESP_LE_KEY_PID";
|
||||
break;
|
||||
case ESP_LE_KEY_PCSRK:
|
||||
key_str = "ESP_LE_KEY_PCSRK";
|
||||
break;
|
||||
case ESP_LE_KEY_PLK:
|
||||
key_str = "ESP_LE_KEY_PLK";
|
||||
break;
|
||||
case ESP_LE_KEY_LLK:
|
||||
key_str = "ESP_LE_KEY_LLK";
|
||||
break;
|
||||
case ESP_LE_KEY_LENC:
|
||||
key_str = "ESP_LE_KEY_LENC";
|
||||
break;
|
||||
case ESP_LE_KEY_LID:
|
||||
key_str = "ESP_LE_KEY_LID";
|
||||
break;
|
||||
case ESP_LE_KEY_LCSRK:
|
||||
key_str = "ESP_LE_KEY_LCSRK";
|
||||
break;
|
||||
default:
|
||||
key_str = "INVALID BLE KEY TYPE";
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return key_str;
|
||||
}
|
||||
|
||||
static char *esp_auth_req_to_str(esp_ble_auth_req_t auth_req){
|
||||
|
||||
char *auth_str = NULL;
|
||||
switch(auth_req) {
|
||||
case ESP_LE_AUTH_NO_BOND:
|
||||
auth_str = "ESP_LE_AUTH_NO_BOND";
|
||||
break;
|
||||
case ESP_LE_AUTH_BOND:
|
||||
auth_str = "ESP_LE_AUTH_BOND";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_MITM:
|
||||
auth_str = "ESP_LE_AUTH_REQ_MITM";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_BOND_MITM:
|
||||
auth_str = "ESP_LE_AUTH_REQ_BOND_MITM";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_ONLY:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_ONLY";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_BOND:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_BOND";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_MITM:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_MITM";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_MITM_BOND:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_MITM_BOND";
|
||||
break;
|
||||
default:
|
||||
auth_str = "INVALID BLE AUTH REQ";
|
||||
break;
|
||||
}
|
||||
|
||||
return auth_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief BLE GAP server event handler
|
||||
*
|
||||
* Allows to connect/disconnect bluetooth devices
|
||||
*
|
||||
* @param event gap event fired when connection state changes
|
||||
* @param param event parameters
|
||||
* */
|
||||
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param){
|
||||
|
||||
switch (event) { //! * look for event type
|
||||
#ifdef CONFIG_SET_RAW_ADV_DATA
|
||||
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
|
||||
adv_config_done &= (~adv_config_flag);
|
||||
if (adv_config_done==0){
|
||||
esp_ble_gap_start_advertising(BLEModule::adv_params);
|
||||
}
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
|
||||
adv_config_done &= (~scan_rsp_config_flag);
|
||||
if (adv_config_done==0){
|
||||
esp_ble_gap_start_advertising(BLEModule::adv_params);
|
||||
}
|
||||
break;
|
||||
#else
|
||||
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
|
||||
adv_config_done &= (~adv_config_flag);
|
||||
if (adv_config_done == 0){
|
||||
esp_ble_gap_start_advertising(&adv_params); //! * start advertising bluetooth profile to nearby devices after advertisement parameters set
|
||||
}
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
|
||||
adv_config_done &= (~scan_rsp_config_flag);
|
||||
if (adv_config_done == 0){
|
||||
esp_ble_gap_start_advertising(&adv_params); //! * start advertising bluetooth profile to nearby devices after nearby devices scanned
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
|
||||
//! - advertising start complete event to indicate advertising start successfully or failed
|
||||
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
|
||||
ESP_LOGE(GATTS_TAG, "Advertising start failed\n");
|
||||
}
|
||||
break;
|
||||
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
|
||||
//! - advertising stop complete event to indicate advertising stop successfully or failed
|
||||
if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS) {
|
||||
ESP_LOGE(GATTS_TAG, "Advertising stop failed\n");
|
||||
} else {
|
||||
ESP_LOGI(GATTS_TAG, "Stop adv successfully\n");
|
||||
}
|
||||
break;
|
||||
case ESP_GAP_BLE_PASSKEY_REQ_EVT: /* passkey request event */
|
||||
ESP_LOGI(GATTS_TAG, "ESP_GAP_BLE_PASSKEY_REQ_EVT");
|
||||
/* Call the following function to input the passkey which is displayed on the remote device */
|
||||
esp_ble_passkey_reply(param->ble_security.ble_req.bd_addr, true, 0x00);
|
||||
break;
|
||||
case ESP_GAP_BLE_NC_REQ_EVT:
|
||||
/* The app will receive this evt when the IO has DisplayYesNO capability and the peer device IO also has DisplayYesNo capability.
|
||||
show the passkey number to the user to confirm it with the number displayed by peer device. */
|
||||
esp_ble_confirm_reply(param->ble_security.ble_req.bd_addr, true);
|
||||
ESP_LOGI(GATTS_TAG, "ESP_GAP_BLE_NC_REQ_EVT, the passkey Notify number:%d", param->ble_security.key_notif.passkey);
|
||||
break;
|
||||
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: //! * handle event fired when connection params change
|
||||
ESP_LOGI(GATTS_TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d",
|
||||
param->update_conn_params.status,
|
||||
param->update_conn_params.min_int,
|
||||
param->update_conn_params.max_int,
|
||||
param->update_conn_params.conn_int,
|
||||
param->update_conn_params.latency,
|
||||
param->update_conn_params.timeout);
|
||||
break;
|
||||
case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: ///the app will receive this evt when the IO has Output capability and the peer device IO has Input capability.
|
||||
///show the passkey number to the user to input it in the peer device.
|
||||
ESP_LOGI(GATTS_TAG, "The passkey Notify number:%06d", param->ble_security.key_notif.passkey);
|
||||
break;
|
||||
case ESP_GAP_BLE_KEY_EVT:
|
||||
//shows the ble key info share with peer device to the user.
|
||||
ESP_LOGI(GATTS_TAG, "key type = %s", esp_key_type_to_str(param->ble_security.ble_key.key_type));
|
||||
break;
|
||||
case ESP_GAP_BLE_SEC_REQ_EVT:
|
||||
/* send the positive (true) security response to the peer device to accept the security request.
|
||||
If not accept the security request, should send the security response with negative(false) accept value*/
|
||||
esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
|
||||
break;
|
||||
case ESP_GAP_BLE_AUTH_CMPL_EVT: {
|
||||
esp_bd_addr_t bd_addr;
|
||||
memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t));
|
||||
ESP_LOGI(GATTS_TAG, "remote BD_ADDR: %08x%04x",\
|
||||
(bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
|
||||
(bd_addr[4] << 8) + bd_addr[5]);
|
||||
ESP_LOGI(GATTS_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type);
|
||||
ESP_LOGI(GATTS_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail");
|
||||
if(!param->ble_security.auth_cmpl.success) {
|
||||
ESP_LOGI(GATTS_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason);
|
||||
} else {
|
||||
ESP_LOGI(GATTS_TAG, "auth mode = %s",esp_auth_req_to_str(param->ble_security.auth_cmpl.auth_mode));
|
||||
}
|
||||
// show_bonded_devices();
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT: {
|
||||
ESP_LOGD(GATTS_TAG, "ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT status = %d", param->remove_bond_dev_cmpl.status);
|
||||
ESP_LOGI(GATTS_TAG, "ESP_GAP_BLE_REMOVE_BOND_DEV");
|
||||
ESP_LOGI(GATTS_TAG, "-----ESP_GAP_BLE_REMOVE_BOND_DEV----");
|
||||
esp_log_buffer_hex(GATTS_TAG, (void *)param->remove_bond_dev_cmpl.bd_addr, sizeof(esp_bd_addr_t));
|
||||
ESP_LOGI(GATTS_TAG, "------------------------------------");
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT:
|
||||
if (param->local_privacy_cmpl.status != ESP_BT_STATUS_SUCCESS){
|
||||
ESP_LOGE(GATTS_TAG, "config local privacy failed, error status = %x", param->local_privacy_cmpl.status);
|
||||
break;
|
||||
}
|
||||
//config adv data
|
||||
esp_err_t ret;
|
||||
ret = esp_ble_gap_config_adv_data(&adv_data);
|
||||
if (ret){
|
||||
ESP_LOGE(GATTS_TAG, "config adv data failed, error code = %x", ret);
|
||||
}
|
||||
adv_config_done |= adv_config_flag;
|
||||
//config scan response data
|
||||
ret = esp_ble_gap_config_adv_data(&scan_rsp_data);
|
||||
if (ret){
|
||||
ESP_LOGE(GATTS_TAG, "config scan response data failed, error code = %x", ret);
|
||||
}
|
||||
adv_config_done |= scan_rsp_config_flag;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
BLEModule::BLEModule(){
|
||||
|
||||
this->adv = &adv_params;
|
||||
}
|
||||
|
||||
esp_err_t BLEModule::init(){
|
||||
|
||||
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); //! * start Bluetooth Mode (turn on bluetooth)
|
||||
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t ret = esp_bt_controller_init(&bt_cfg); //! * initialize low-level Bluetooth controller
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); //! * enable Bluetooth Low Energy
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
ret = esp_bluedroid_init(); //! * initialize Bluedroid api
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
ret = esp_bluedroid_enable(); //! * enable Bluedroid api
|
||||
if (ret) {
|
||||
ESP_LOGE(TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_ble_gap_register_callback(gap_event_handler); //! * register GAP event handler
|
||||
if (ret){
|
||||
ESP_LOGE(TAG, "gap register error, error code = %x", ret);
|
||||
return ret;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t BLEModule::deinit(){
|
||||
|
||||
esp_err_t ret;
|
||||
ret = esp_bluedroid_disable(); //! * disable Bluedroid api
|
||||
if(ret) return ret;
|
||||
ret = esp_bluedroid_deinit(); //! * deinitialize Bluedroid api
|
||||
if(ret) return ret;
|
||||
ret = esp_bt_controller_disable(); //! * disable bluetooth controller
|
||||
if(ret) return ret;
|
||||
ret = esp_bt_controller_deinit(); //! * deinitialize bluetooth controller (turn off)
|
||||
if(ret) return ret;
|
||||
return ESP_OK;
|
||||
}
|
||||
23
basic-setup/main/impl/modules/NVSModule.cpp
Normal file
23
basic-setup/main/impl/modules/NVSModule.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @file NVSModule.cpp
|
||||
* @brief NVSModule implementation file
|
||||
* */
|
||||
|
||||
#include "nvs_flash.h"
|
||||
#include "module.h"
|
||||
|
||||
esp_err_t NVSModule::init(){
|
||||
|
||||
ESP_LOGI(TAG, "initializing nvs");
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t NVSModule::deinit(){
|
||||
|
||||
return nvs_flash_deinit();
|
||||
}
|
||||
92
basic-setup/main/impl/modules/RPCModule.cpp
Normal file
92
basic-setup/main/impl/modules/RPCModule.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* @file RPCModule.cpp
|
||||
* @brief RPC implementation file
|
||||
* */
|
||||
|
||||
#include "string.h"
|
||||
#include "module.h"
|
||||
|
||||
RPC::RPC(){
|
||||
|
||||
this->cfg = new esp_tls_cfg_t();
|
||||
this->server = NULL;
|
||||
this->port = 443;
|
||||
};
|
||||
|
||||
cJSON * RPC::send_json_rpc(const char* sni, char* req){
|
||||
|
||||
static char buf[512]; // tls connection buffer to read response
|
||||
int length = 1024;
|
||||
char *res; // buffer to store whole response body
|
||||
res = (char *) malloc(length);
|
||||
memset(res, 0, length);
|
||||
int ret, len;
|
||||
|
||||
/* tls connection config. */
|
||||
cfg->common_name = sni; //! set SNI extension
|
||||
|
||||
struct esp_tls *tls = esp_tls_conn_new(server, strlen(server),port, cfg); //! connect to defined address
|
||||
|
||||
if(tls != NULL) {
|
||||
ESP_LOGI(TLS_TAG, "Connection established with %s", sni);
|
||||
} else {
|
||||
printf("available memory: %d\n", (int) heap_caps_get_free_size(MALLOC_CAP_32BIT | MALLOC_CAP_8BIT));
|
||||
throw "Connection failed";
|
||||
}
|
||||
|
||||
size_t written_bytes = 0;
|
||||
do {
|
||||
ret = esp_tls_conn_write(tls, req + written_bytes, strlen(req) - written_bytes); //! send rpc data
|
||||
|
||||
if (ret >= 0) {
|
||||
written_bytes += ret;
|
||||
} else if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
ESP_LOGE(TLS_TAG, "esp_tls_conn_write returned 0x%x", ret);
|
||||
esp_tls_conn_delete(tls);
|
||||
}
|
||||
} while(written_bytes < strlen(req));
|
||||
|
||||
do
|
||||
{
|
||||
len = sizeof(buf) - 1;
|
||||
bzero(buf, sizeof(buf));
|
||||
ret = esp_tls_conn_read(tls, (char *)buf, len); //! recieve response in chunks
|
||||
if(ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ)
|
||||
continue;
|
||||
|
||||
if(ret < 0)
|
||||
{
|
||||
ESP_LOGE(TLS_TAG, "esp_tls_conn_read returned -0x%x", -ret);
|
||||
break;
|
||||
}
|
||||
|
||||
if(ret == 0)
|
||||
{
|
||||
ESP_LOGI(TLS_TAG, "connection closed");
|
||||
break;
|
||||
}
|
||||
|
||||
len = ret;
|
||||
ESP_LOGD(TLS_TAG, "%d bytes read", len);
|
||||
|
||||
if(strlen(res) + len + 1 >= length){ //! In case no enough memory, extend res buffer
|
||||
length += len; //! * increase by amount required
|
||||
res = (char*) realloc(res, length); //! * reallocate memory
|
||||
memset(res+length-len,0,len); //! * set new memory blocks 0 to assure the result is null-terminate string
|
||||
}
|
||||
memcpy(res + strlen(res), buf, len); //! write chunked response to buffer
|
||||
|
||||
} while(1);
|
||||
|
||||
cJSON *tmp = cJSON_Parse(res); //! parse response JSON into cJSON struct
|
||||
free(res);
|
||||
free(req);
|
||||
esp_tls_conn_delete(tls);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
RPC& RPC::Instance(){
|
||||
|
||||
static RPC instance;
|
||||
return instance;
|
||||
}
|
||||
52
basic-setup/main/impl/modules/RotaryEncodeModule.cpp
Normal file
52
basic-setup/main/impl/modules/RotaryEncodeModule.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @file RotaryEncodeModule.cpp
|
||||
* @brief RotaryEncoderModule implementation file
|
||||
* */
|
||||
|
||||
#include "module.h"
|
||||
|
||||
RotaryEncoderModule::RotaryEncoderModule(){};
|
||||
|
||||
void RotaryEncoderModule::setPins(gpio_num_t a, gpio_num_t b, gpio_num_t c){
|
||||
|
||||
ROT_ENC_A_GPIO = a;
|
||||
ROT_ENC_B_GPIO = b;
|
||||
ROT_ENC_C_GPIO = c;
|
||||
ENABLE_HALF_STEPS = false;
|
||||
FLIP_DIRECTION = false;
|
||||
this->info = new rotary_encoder_info_t();
|
||||
}
|
||||
|
||||
rotary_encoder_info_t* RotaryEncoderModule::getInfo() const{
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
esp_err_t RotaryEncoderModule::init(){
|
||||
|
||||
// Initialise the rotary encoder device with the GPIOs for A and B signals
|
||||
ESP_ERROR_CHECK(rotary_encoder_init(info, ROT_ENC_A_GPIO, ROT_ENC_B_GPIO, ROT_ENC_C_GPIO));
|
||||
ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(info, ENABLE_HALF_STEPS));
|
||||
if( FLIP_DIRECTION )
|
||||
ESP_ERROR_CHECK(rotary_encoder_flip_direction(info));
|
||||
|
||||
// Create a queue for events from the rotary encoder driver.
|
||||
// Tasks can read from this queue to receive up to date position information.
|
||||
QueueHandle_t event_queue = rotary_encoder_create_queue();
|
||||
ESP_ERROR_CHECK(rotary_encoder_set_queue(info, event_queue));
|
||||
ESP_LOGI(TAG, "rotary encoder inited");
|
||||
return ESP_OK;
|
||||
};
|
||||
|
||||
esp_err_t RotaryEncoderModule::deinit(){
|
||||
|
||||
ESP_ERROR_CHECK(rotary_encoder_uninit(info));
|
||||
ESP_LOGI(TAG, "deinited");
|
||||
return ESP_OK;
|
||||
};
|
||||
|
||||
RotaryEncoderModule& RotaryEncoderModule::Instance(){
|
||||
|
||||
static RotaryEncoderModule instance;
|
||||
return instance;
|
||||
}
|
||||
59
basic-setup/main/impl/modules/SNTPModule.cpp
Normal file
59
basic-setup/main/impl/modules/SNTPModule.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* @file SNTPModule.cpp
|
||||
* @brief SNTPModule implementation file
|
||||
* */
|
||||
|
||||
#include "lwip/apps/sntp.h"
|
||||
#include "sntp/sntp.h"
|
||||
#include "module.h"
|
||||
|
||||
void time_sync_notification_cb(struct timeval *tv){
|
||||
|
||||
ESP_LOGI("Global","Notification of a time synchronization event");
|
||||
}
|
||||
|
||||
/** Sample code to sync time with NTP server from ESP-IDF
|
||||
* https://github.com/espressif/esp-idf/tree/master/examples/protocols/sntp
|
||||
**/
|
||||
void SNTPModule::initialize_sntp(void){
|
||||
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
sntp_setservername(0, (char*) "pool.ntp.org");
|
||||
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
|
||||
sntp_init();
|
||||
}
|
||||
|
||||
esp_err_t SNTPModule::init(){
|
||||
|
||||
// Sync time
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
// Is time set? If not, tm_year will be (1970 - 1900).
|
||||
if (timeinfo.tm_year < (2019 - 1900)) {
|
||||
ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
|
||||
ESP_LOGI(TAG, "Initializing SNTP");
|
||||
initialize_sntp();
|
||||
int retry = 0;
|
||||
const int retry_count = 10;
|
||||
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
|
||||
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
// update 'now' variable with current time
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
char curr[20];
|
||||
strftime(curr, 20,"%Y-%m-%dT%XZ", localtime(&now));
|
||||
ESP_LOGI(TAG, "Time is set: %s", curr);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t SNTPModule::deinit(){
|
||||
|
||||
sntp_stop();
|
||||
ESP_LOGI(TAG, "deinited");
|
||||
return ESP_OK;
|
||||
}
|
||||
100
basic-setup/main/impl/modules/ScreenModule.cpp
Normal file
100
basic-setup/main/impl/modules/ScreenModule.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* @file ScreenModule.cpp
|
||||
* @brief ScreenModule implementation file
|
||||
* */
|
||||
|
||||
#include "u8g2.h"
|
||||
#include "u8g2_esp32_hal.h"
|
||||
#include "module.h"
|
||||
|
||||
u8g2_t u8g2; // a structure which will contain all the data for one display
|
||||
|
||||
ScreenModule::ScreenModule(){};
|
||||
|
||||
void ScreenModule::setSCL(gpio_num_t scl_pin){
|
||||
|
||||
this->SCL_PIN = scl_pin;
|
||||
}
|
||||
|
||||
void ScreenModule::setSDA(gpio_num_t sda_pin){
|
||||
|
||||
this->SDA_PIN = sda_pin;
|
||||
}
|
||||
|
||||
void ScreenModule::setRST(gpio_num_t rst_pin){
|
||||
|
||||
this->RESET_PIN = rst_pin;
|
||||
}
|
||||
|
||||
esp_err_t ScreenModule::init(){
|
||||
|
||||
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
|
||||
u8g2_esp32_hal.sda = SDA_PIN;
|
||||
u8g2_esp32_hal.scl = SCL_PIN;
|
||||
|
||||
if(RESET_PIN != GPIO_NUM_NC) u8g2_esp32_hal.reset = RESET_PIN;
|
||||
u8g2_esp32_hal_init( u8g2_esp32_hal );
|
||||
|
||||
u8g2_Setup_ssd1306_i2c_128x64_noname_f(
|
||||
&u8g2,
|
||||
U8G2_R0,
|
||||
//u8x8_byte_sw_i2c,
|
||||
u8g2_esp32_i2c_byte_cb,
|
||||
u8g2_esp32_gpio_and_delay_cb); // init u8g2 structure
|
||||
try{
|
||||
u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
|
||||
ESP_LOGI(TAG, "u8g2_InitDisplay");
|
||||
u8g2_InitDisplay(&u8g2);
|
||||
ESP_LOGI(TAG, "u8g2_SetPowerSave");
|
||||
u8g2_SetPowerSave(&u8g2, 0); // wake up display
|
||||
ESP_LOGI(TAG, "u8g2_ClearBuffer");
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
}catch(const char* msg){
|
||||
throw msg;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ScreenModule::deinit(){
|
||||
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* UI method to write strings to screen */
|
||||
void ScreenModule::print_screen(char *buff){
|
||||
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
u8g2_SetFont(&u8g2, u8g2_font_Georgia7px_tf);
|
||||
u8g2_DrawStr(&u8g2, 0,10, buff);
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
}
|
||||
|
||||
void ScreenModule::clear(){
|
||||
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
lineOff = 0;
|
||||
}
|
||||
|
||||
void ScreenModule::refresh(){
|
||||
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
}
|
||||
|
||||
|
||||
void ScreenModule::writeLine(char *str, uint8_t pl){
|
||||
|
||||
u8g2_SetFont(&u8g2, u8g2_font_4x6_tf);
|
||||
u8g2_DrawStr(&u8g2, pl, lineOff*10+10, str);
|
||||
lineOff++;
|
||||
}
|
||||
|
||||
void ScreenModule::FillCircle(uint8_t x, uint8_t y, uint8_t r){
|
||||
u8g2_DrawDisc(&u8g2, x, y, r, U8G2_DRAW_ALL);
|
||||
}
|
||||
|
||||
ScreenModule& ScreenModule::Instance(){
|
||||
|
||||
static ScreenModule instance;
|
||||
return instance;
|
||||
}
|
||||
95
basic-setup/main/impl/modules/WiFiModule.cpp
Normal file
95
basic-setup/main/impl/modules/WiFiModule.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* @file WiFiModule.cpp
|
||||
* @brief WiFiModule implementation file
|
||||
**/
|
||||
|
||||
#include "string.h"
|
||||
#include "esp_event.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "module.h"
|
||||
|
||||
static const int WIFI_CONNECTED_BIT = BIT0;
|
||||
static int MAXIMUM_RETRY = 5;
|
||||
static int wifi_status = 0;
|
||||
|
||||
static int s_retry_num = 0;
|
||||
/* FreeRTOS event group to signal when we are connected*/
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
/** Sample code to connect wifi from ESP-IDF*/
|
||||
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if (s_retry_num < MAXIMUM_RETRY) {
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
s_retry_num++;
|
||||
ESP_LOGI("wi-fi", "retry to connect to the AP");
|
||||
}
|
||||
else{
|
||||
wifi_status = -1;
|
||||
}
|
||||
ESP_LOGI("wi-fi","connect to the AP fail");
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
wifi_status = 1;
|
||||
ESP_LOGI("wi-fi","connected");
|
||||
}
|
||||
}
|
||||
|
||||
WiFiModule::WiFiModule(char *ssid, char* pass){
|
||||
|
||||
this->WIFI_SSID = ssid;
|
||||
this->WIFI_PASS = pass;
|
||||
}
|
||||
|
||||
esp_err_t WiFiModule::init(){
|
||||
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
tcpip_adapter_init();
|
||||
|
||||
esp_err_t err;
|
||||
err = esp_event_loop_create_default();
|
||||
if(err != ESP_OK) throw "Event loop error";
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
err = esp_wifi_init(&cfg);
|
||||
if(err != ESP_OK) throw "Wifi init error";
|
||||
|
||||
err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);
|
||||
if(err != ESP_OK) throw "Event handler error";
|
||||
|
||||
err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL);
|
||||
if(err != ESP_OK) throw "Event handler error";
|
||||
|
||||
wifi_config_t wifi_config = {};
|
||||
memset(&wifi_config,0, sizeof(wifi_config));
|
||||
strcpy((char*)(wifi_config.sta.ssid), WIFI_SSID );
|
||||
strcpy((char*)(wifi_config.sta.password), WIFI_PASS);
|
||||
wifi_config.sta.bssid_set = false;
|
||||
|
||||
err = esp_wifi_set_mode(WIFI_MODE_STA); if(err != ESP_OK) throw "Wifi mode error";
|
||||
err = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); if(err != ESP_OK) throw "Wifi config error";
|
||||
err = esp_wifi_start(); if(err != ESP_OK) throw "Can't start wifi";
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",WIFI_SSID, WIFI_PASS);
|
||||
|
||||
while(!wifi_status){
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
}
|
||||
if(wifi_status == -1){
|
||||
throw "Couldn't connect to wifi";
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t WiFiModule::deinit(){
|
||||
|
||||
return esp_wifi_deinit();
|
||||
}
|
||||
Reference in New Issue
Block a user