add/update interface

This commit is contained in:
Lurkars
2020-08-16 16:40:05 +02:00
parent 628ee8fe39
commit b473e88be1
46 changed files with 2785 additions and 1463 deletions
-2
View File
@@ -1,7 +1,5 @@
idf_component_register(
SRCS
"main.c"
"display-interface.c"
"wifi.c"
INCLUDE_DIRS ""
)
-20
View File
@@ -1,20 +0,0 @@
menu "Wifi Setup"
config WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
config WIFI_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
endmenu
-153
View File
@@ -1,153 +0,0 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "ds3231.h"
#include "ena-exposure.h"
#include "interface.h"
#include "interface-menu.h"
#include "interface-datetime.h"
#include "ssd1306.h"
void interface_display_time(void *pvParameter)
{
static time_t curtime;
static char *curtime_text;
static struct tm rtc_time;
static bool edit_invert = false;
while (1)
{
curtime = time(NULL);
localtime_r(&curtime, &rtc_time);
curtime_text = asctime(&rtc_time);
ssd1306_text_line(SSD1306_ADDRESS, curtime_text, 0, false);
gmtime_r(&curtime, &rtc_time);
curtime_text = asctime(&rtc_time);
ssd1306_text_line(SSD1306_ADDRESS, curtime_text, 1, false);
if (interface_get_state() == INTERFACE_STATE_SET_DATETIME)
{
edit_invert = !edit_invert;
ds3231_set_time(&rtc_time);
char edit_year[4] = "";
char edit_month[3] = "";
char edit_day[2] = "";
char edit_hour[2] = "";
char edit_minute[2] = "";
char edit_second[2] = "";
switch (interface_datetime_state())
{
case INTERFACE_DATETIME_STATE_YEAR:
memcpy(&edit_year, &curtime_text[20], 4);
ssd1306_text_line_column(SSD1306_ADDRESS, edit_year, 0, 20, edit_invert);
break;
case INTERFACE_DATETIME_STATE_MONTH:
memcpy(&edit_month, &curtime_text[4], 3);
ssd1306_text_line_column(SSD1306_ADDRESS, edit_month, 0, 4, edit_invert);
break;
case INTERFACE_DATETIME_STATE_DAY:
memcpy(&edit_day, &curtime_text[8], 2);
ssd1306_text_line_column(SSD1306_ADDRESS, edit_day, 0, 8, edit_invert);
break;
case INTERFACE_DATETIME_STATE_HOUR:
memcpy(&edit_hour, &curtime_text[11], 2);
ssd1306_text_line_column(SSD1306_ADDRESS, edit_hour, 0, 11, edit_invert);
break;
case INTERFACE_DATETIME_STATE_MINUTE:
memcpy(&edit_minute, &curtime_text[14], 2);
ssd1306_text_line_column(SSD1306_ADDRESS, edit_minute, 0, 14, edit_invert);
break;
case INTERFACE_DATETIME_STATE_SECONDS:
memcpy(&edit_second[0], &curtime_text[17], 2);
ssd1306_text_line_column(SSD1306_ADDRESS, edit_second, 0, 17, edit_invert);
break;
}
}
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
void interface_display_status(void *pvParameter)
{
static bool get_status = true;
while (1)
{
if (interface_get_state() == INTERFACE_STATE_STATUS)
{
if (get_status)
{
ena_exposure_summary_t summary;
ena_exposure_summary(ena_exposure_default_config(), &summary);
char buffer[23];
sprintf(buffer, "Days: %d", summary.days_since_last_exposure);
ssd1306_text_line(SSD1306_ADDRESS, buffer, 3, false);
sprintf(buffer, "Exposures: %d", summary.num_exposures);
ssd1306_text_line(SSD1306_ADDRESS, buffer, 4, false);
sprintf(buffer, "Score: %d, Max: %d", summary.risk_score_sum, summary.max_risk_score);
ssd1306_text_line(SSD1306_ADDRESS, buffer, 5, false);
get_status = false;
}
}
else if (!get_status)
{
ssd1306_clear_line(SSD1306_ADDRESS, 3, false);
ssd1306_clear_line(SSD1306_ADDRESS, 4, false);
ssd1306_clear_line(SSD1306_ADDRESS, 5, false);
get_status = true;
}
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
void interface_display_idle(void *pvParameter)
{
static bool set_status = true;
while (1)
{
if (interface_get_state() == INTERFACE_STATE_IDLE)
{
if (set_status)
{
ssd1306_on(SSD1306_ADDRESS, false);
set_status = false;
}
}
else if (!set_status)
{
ssd1306_on(SSD1306_ADDRESS, true);
set_status = true;
}
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
void display_interface_start(void)
{
ssd1306_start(SSD1306_ADDRESS);
ssd1306_clear(SSD1306_ADDRESS);
interface_start();
interface_menu_start();
xTaskCreate(&interface_display_time, "interface_display_time", 4096, NULL, 5, NULL);
xTaskCreate(&interface_display_status, "interface_display_status", 4096, NULL, 5, NULL);
xTaskCreate(&interface_display_idle, "interface_display_idle", 4096, NULL, 5, NULL);
}
-22
View File
@@ -1,22 +0,0 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _display_interface_H_
#define _display_interface_H_
/**
* @brief start display + interface
*/
void display_interface_start(void);
#endif
+11 -5
View File
@@ -25,16 +25,17 @@
#include "ena-exposure.h"
#include "ena-bluetooth-advertise.h"
#include "ena-bluetooth-scan.h"
#include "interface.h"
#include "ena-cwa.h"
#include "interface.h"
#include "ds3231.h"
#include "display-interface.h"
#include "wifi.h"
#include "ssd1306.h"
#include "wifi-controller.h"
#include "sdkconfig.h"
void app_main(void)
{
// debug only own LOG TAGs
esp_log_level_set("*", ESP_LOG_WARN);
esp_log_level_set(ENA_LOG, ESP_LOG_DEBUG);
@@ -47,6 +48,9 @@ void app_main(void)
esp_log_level_set(INTERFACE_LOG, ESP_LOG_DEBUG);
esp_log_level_set(WIFI_LOG, ESP_LOG_DEBUG);
// start interface
interface_start();
// set system time from DS3231
struct tm rtc_time;
ds3231_get_time(&rtc_time);
@@ -62,11 +66,13 @@ void app_main(void)
ena_start();
display_interface_start();
// start with main interface
interface_main_start();
while (1)
{
ena_run();
ena_cwa_run();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
-172
View File
@@ -1,172 +0,0 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "ena-cwa.h"
#include "wifi.h"
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static EventGroupHandle_t s_wifi_event_group;
static int s_retry_num = 0;
static bool connected = false;
void wifi_stop(void)
{
connected = false;
esp_wifi_stop();
esp_wifi_deinit();
esp_event_loop_delete_default();
}
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 < WIFI_MAXIMUM_RETRY)
{
esp_wifi_connect();
s_retry_num++;
ESP_LOGD(WIFI_LOG, "retry to connect to the AP");
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGD(WIFI_LOG, "connect to the AP fail");
connected = false;
}
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);
connected = true;
heap_caps_check_integrity_all(true);
}
else
{
ESP_LOGD(WIFI_LOG, "eventbase %s, eventid %d", event_base, event_id);
}
}
void wifi_start(void)
{
// init NVS for WIFI
esp_err_t ret;
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());
ESP_ERROR_CHECK(nvs_flash_init());
}
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_netif_init());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASSWORD,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = 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());
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGV(WIFI_LOG, "connected to ap SSID:%s",
WIFI_SSID);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(WIFI_LOG, "Failed to connect to SSID:%s",
WIFI_SSID);
}
else
{
ESP_LOGE(WIFI_LOG, "UNEXPECTED EVENT");
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
}
bool wifi_is_connected(void)
{
return connected;
}
-38
View File
@@ -1,38 +0,0 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _wifi_H_
#define _wifi_H_
#define WIFI_LOG "ESP-ENA-wifi" // TAG for Logging
#define WIFI_SSID (CONFIG_WIFI_SSID)
#define WIFI_PASSWORD (CONFIG_WIFI_PASSWORD)
#define WIFI_MAXIMUM_RETRY (CONFIG_WIFI_MAXIMUM_RETRY)
/**
* @brief start wifi connection to configured AP
*/
void wifi_start(void);
/**
* @brief stop wifi (restart does not work for now!)
*/
void wifi_stop(void);
/**
* @brief check if a wifi is connected
*/
bool wifi_is_connected(void);
#endif