first commit

This commit is contained in:
valeh
2020-12-22 14:30:09 +02:00
commit 26b0ba5954
1832 changed files with 17777948 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
/**
* @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));
ESP_LOGE(TLS_TAG, "Connection failed...");
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;
}

View 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;
}

View File

@@ -0,0 +1,77 @@
/**
* @file ScreenModule.cpp
* @brief ScreenModule implementation file
* */
#include "module.h"
#include "freertos/timers.h"
#include "esp_sleep.h"
#include "lvgl/lvgl.h"
#include "drv/disp_spi.h"
#include "drv/ili9341.h"
static void lv_task_timercb(void *timer){
/* Periodically call this function.
* The timing is not critical but should be between 1..10 ms */
lv_task_handler();
}
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(){
lv_init();
disp_spi_init();
ili9341_init();
static lv_color_t buf1[DISP_BUF_SIZE];
static lv_color_t buf2[DISP_BUF_SIZE];
static lv_disp_buf_t disp_buf;
lv_disp_buf_init(&disp_buf, buf1, buf2, DISP_BUF_SIZE);
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.flush_cb = ili9341_flush;
disp_drv.buffer = &disp_buf;
disp_drv.rotated = 1 ;
lv_disp_drv_register(&disp_drv);
esp_timer_create_args_t lv_task_timer_conf = {
.callback = lv_task_timercb,
.name = "lv_task_timer"
};
esp_timer_handle_t lv_task_timer = NULL;
esp_timer_create(&lv_task_timer_conf, &lv_task_timer);
esp_timer_start_periodic(lv_task_timer, 5 * 1000U);
return ESP_OK;
}
esp_err_t ScreenModule::deinit(){
return ESP_OK;
}
ScreenModule& ScreenModule::Instance(){
static ScreenModule instance;
return instance;
}

View File

@@ -0,0 +1,76 @@
/**
* @file TouchModule.cpp
* @brief TouchModule implementation file
* */
#include "lvgl/lvgl.h"
#include "Arduino.h"
#include "TouchScreen.h"
#include "module.h"
#define TS_MINX 200
#define TS_MINY 150
#define TS_MAXX 940
#define TS_MAXY 950
TouchModule::TouchModule(gpio_num_t a, gpio_num_t b, gpio_num_t c, gpio_num_t d){
this->YP = a;
this->XP = b;
this->XM = c;
this->YM = d;
};
int map(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
TouchScreen* ts;
static bool touch_callback(lv_indev_drv_t * drv, lv_indev_data_t *data){
static int last_x = 0, last_y = 0;
analogReadResolution(10);
TSPoint p = ts->getPoint();
bool valid = true;
int x,y;
if(p.z > 0){
p.x = 240 - map(p.x, TS_MINX, TS_MAXX, 0, 240);
p.y = 320 - map(p.y, TS_MINY, TS_MAXY, 0, 320);
x = p.x;
y = p.y;
last_x = x;
last_y = y;
}else{
x = last_x;
y = last_y;
valid = false;
}
data->point.x = x;
data->point.y = y;
data->state = valid ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
return false;
}
esp_err_t TouchModule::init(){
ts = new TouchScreen(XP, YP, XM, YM, 0 );
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv); /*Basic initialization*/
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touch_callback;
/*Register the driver in LittlevGL and save the created input device object*/
lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv);
return ESP_OK;
};
esp_err_t TouchModule::deinit(){
return ESP_OK;
};

View File

@@ -0,0 +1,87 @@
/**
* @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");
}
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_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
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;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
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(s_retry_num >= MAXIMUM_RETRY){
throw "WiFi unable to connect";
}
}
return ESP_OK;
}
esp_err_t WiFiModule::deinit(){
return esp_wifi_deinit();
}