rename interface component, update docs, added cleanup of old beacons

This commit is contained in:
Lurkars
2020-07-27 16:54:51 +02:00
parent 7dacb93369
commit 4e696f2fb4
30 changed files with 484 additions and 333 deletions
+10
View File
@@ -0,0 +1,10 @@
idf_component_register(
SRCS
"interface.c"
"interface-datetime.c"
"interface-menu.c"
"interface-status.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
ena
)
@@ -0,0 +1,37 @@
// 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.
/**
* @file
*
* @brief interface for changing current date and time
*
*/
#ifndef _interface__DATETIME_H_
#define _interface__DATETIME_H_
typedef enum
{
INTERFACE_DATETIME_STATE_YEAR = 0,
INTERFACE_DATETIME_STATE_MONTH,
INTERFACE_DATETIME_STATE_DAY,
INTERFACE_DATETIME_STATE_HOUR,
INTERFACE_DATETIME_STATE_MINUTE,
INTERFACE_DATETIME_STATE_SECONDS,
} interface_datetime_state_t;
void interface_datetime_start(void);
int interface_datetime_state(void);
#endif
@@ -0,0 +1,35 @@
// 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.
/**
* @file
*
* @brief interface menu to navigate through interface
*
*/
#ifndef _interface__MENU_H_
#define _interface__MENU_H_
typedef enum
{
INTERFACE_MENU_STATE_IDLE = 0,
INTERFACE_MENU_STATE_SELECT_TIME,
INTERFACE_MENU_STATE_SELECT_DEBUG,
INTERFACE_MENU_STATE_SELECT_STATUS,
} interface_menu_state_t;
void interface_menu_start(void);
int interface_menu_get_state(void);
#endif
@@ -0,0 +1,25 @@
// 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.
/**
* @file
*
* @brief interface to show current ENA status (infection risk etc.)
*
*/
#ifndef _interface__STATUS_H_
#define _interface__STATUS_H_
void interface_status_start(void);
#endif
+79
View File
@@ -0,0 +1,79 @@
// 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.
/**
* @file
*
* @brief interface functionality via touch pads for control and setup
*
*/
#ifndef _interface__H_
#define _interface__H_
#define INTERFACE_LOG "INTERFACE" // TAG for Logging
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
#define TOUCH_PAD_COUNT (4)
#define TOUCH_PAD_ESC (TOUCH_PAD_NUM0)
#define TOUCH_PAD_OK (TOUCH_PAD_NUM6)
#define TOUCH_PAD_UP (TOUCH_PAD_NUM4)
#define TOUCH_PAD_DOWN (TOUCH_PAD_NUM3)
/**
* @brief different interface states
*/
typedef enum
{
INTERFACE_STATE_IDLE = 0, // ilde state, do nothing
INTERFACE_STATE_MENU, // main menu
INTERFACE_STATE_SET_DATETIME, // set current date and time
INTERFACE_STATE_STATUS, // current status
} interface_state_t;
/**
* @brief callback function on touch event
*/
typedef void (*interface_touch_callback)(void);
/**
* @brief register a callback function for touch event
*
* @param[in] touch_pad id of the touchpad to listen touch
* @param[in] callback callback function
*/
void interface_register_touch_callback(int touch_pad, interface_touch_callback callback);
/**
* @brief get current interface state
*
* @return
* current state the interface is in
*/
int interface_get_state(void);
/**
* @brief set current interface state
*
* @param[in] state new state to set
*/
void interface_set_state(interface_state_t state);
/**
* @brief start interface logic
*
* This will initialize the touch controls and start a task to listen to touch
* inputs and calling the callbacks
*/
void interface_start(void);
#endif
+78
View File
@@ -0,0 +1,78 @@
// 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 <stdio.h>
#include <time.h>
#include <sys/time.h>
#include "esp_log.h"
#include "interface.h"
#include "driver/touch_pad.h"
#include "interface-menu.h"
#include "interface-datetime.h"
static int current_interface_datetime_state = INTERFACE_DATETIME_STATE_YEAR;
const uint32_t interface_datetime_steps[6] = {
31557600, 2629800, 86400, 3600, 60, 1};
void interface_datetime_esc(void)
{
interface_menu_start();
}
void interface_datetime_ok(void)
{
current_interface_datetime_state++;
if (current_interface_datetime_state > INTERFACE_DATETIME_STATE_SECONDS)
{
current_interface_datetime_state = INTERFACE_DATETIME_STATE_YEAR;
}
ESP_LOGD(INTERFACE_LOG, "datetime to %d", current_interface_datetime_state);
}
void interface_datetime_up(void)
{
time_t curtime = time(NULL);
curtime += interface_datetime_steps[current_interface_datetime_state];
struct timeval tv = {0};
tv.tv_sec = curtime;
settimeofday(&tv, NULL);
ESP_LOGD(INTERFACE_LOG, "increment %d about %u %s", current_interface_datetime_state, interface_datetime_steps[current_interface_datetime_state], ctime(&curtime));
}
void interface_datetime_down(void)
{
time_t curtime = time(NULL);
curtime -= interface_datetime_steps[current_interface_datetime_state];
struct timeval tv = {0};
tv.tv_sec = curtime;
settimeofday(&tv, NULL);
ESP_LOGD(INTERFACE_LOG, "decrement %d about %u %s", current_interface_datetime_state, interface_datetime_steps[current_interface_datetime_state], ctime(&curtime));
}
void interface_datetime_start(void)
{
interface_set_state(INTERFACE_STATE_SET_DATETIME);
interface_register_touch_callback(TOUCH_PAD_ESC, &interface_datetime_esc);
interface_register_touch_callback(TOUCH_PAD_OK, &interface_datetime_ok);
interface_register_touch_callback(TOUCH_PAD_UP, &interface_datetime_up);
interface_register_touch_callback(TOUCH_PAD_DOWN, &interface_datetime_down);
ESP_LOGD(INTERFACE_LOG, "start datetime interface");
}
int interface_datetime_state(void)
{
return current_interface_datetime_state;
}
+88
View File
@@ -0,0 +1,88 @@
// 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 <stdio.h>
#include "esp_log.h"
#include "driver/touch_pad.h"
#include "interface.h"
#include "interface-datetime.h"
#include "interface-status.h"
#include "interface-menu.h"
static int current_interface_menu_state = INTERFACE_MENU_STATE_IDLE;
void interface_menu_ok(void)
{
if (current_interface_menu_state == INTERFACE_MENU_STATE_SELECT_TIME)
{
interface_datetime_start();
}
else if (current_interface_menu_state == INTERFACE_MENU_STATE_SELECT_STATUS)
{
interface_status_start();
}
else if (current_interface_menu_state == INTERFACE_MENU_STATE_SELECT_DEBUG)
{
}
else if (current_interface_menu_state == INTERFACE_MENU_STATE_IDLE)
{
if (interface_get_state() == INTERFACE_STATE_MENU)
{
interface_set_state(INTERFACE_STATE_IDLE);
interface_register_touch_callback(TOUCH_PAD_UP, NULL);
interface_register_touch_callback(TOUCH_PAD_DOWN, NULL);
}
else
{
interface_menu_start();
}
}
}
void interface_menu_up(void)
{
current_interface_menu_state--;
if (current_interface_menu_state < INTERFACE_MENU_STATE_IDLE)
{
current_interface_menu_state = INTERFACE_MENU_STATE_SELECT_STATUS;
}
ESP_LOGD(INTERFACE_LOG, "menu up to %d", current_interface_menu_state);
}
void interface_menu_down(void)
{
current_interface_menu_state++;
if (current_interface_menu_state > INTERFACE_MENU_STATE_SELECT_STATUS)
{
current_interface_menu_state = INTERFACE_MENU_STATE_IDLE;
}
ESP_LOGD(INTERFACE_LOG, "menu down to %d", current_interface_menu_state);
}
void interface_menu_start(void)
{
interface_set_state(INTERFACE_STATE_MENU);
interface_register_touch_callback(TOUCH_PAD_ESC, NULL);
interface_register_touch_callback(TOUCH_PAD_OK, &interface_menu_ok);
interface_register_touch_callback(TOUCH_PAD_UP, &interface_menu_up);
interface_register_touch_callback(TOUCH_PAD_DOWN, &interface_menu_down);
ESP_LOGD(INTERFACE_LOG, "start menu interface");
}
int interface_menu_get_state(void)
{
return current_interface_menu_state;
}
+37
View File
@@ -0,0 +1,37 @@
// 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 <stdio.h>
#include "esp_log.h"
#include "driver/touch_pad.h"
#include "interface.h"
#include "interface-menu.h"
#include "interface-status.h"
void interface_status_esc(void)
{
interface_menu_start();
}
void interface_status_start(void)
{
interface_set_state(INTERFACE_STATE_STATUS);
interface_register_touch_callback(TOUCH_PAD_ESC, &interface_status_esc);
interface_register_touch_callback(TOUCH_PAD_OK, NULL);
interface_register_touch_callback(TOUCH_PAD_UP, NULL);
interface_register_touch_callback(TOUCH_PAD_DOWN, NULL);
ESP_LOGD(INTERFACE_LOG, "start status interface");
}
+98
View File
@@ -0,0 +1,98 @@
// 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 <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/touch_pad.h"
#include "esp_log.h"
#include "interface.h"
static int current_interface_state = INTERFACE_STATE_IDLE;
static int touch_mapping[TOUCH_PAD_COUNT] = {0};
static bool touch_status[TOUCH_PAD_COUNT] = {0};
static interface_touch_callback touch_callbacks[TOUCH_PAD_MAX];
void interface_register_touch_callback(int touch_pad, interface_touch_callback callback)
{
touch_callbacks[touch_pad] = callback;
}
void interface_run(void *pvParameter)
{
static uint16_t touch_value;
static uint16_t touch_thresh;
static bool touch_status_current[TOUCH_PAD_MAX] = {0};
while (1)
{
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
ESP_ERROR_CHECK_WITHOUT_ABORT(touch_pad_read_filtered(touch_mapping[i], &touch_value));
ESP_ERROR_CHECK_WITHOUT_ABORT(touch_pad_get_thresh(touch_mapping[i], &touch_thresh));
touch_status_current[i] = touch_value < touch_thresh;
if (!touch_status[i] & touch_status_current[i])
{
ESP_LOGD(INTERFACE_LOG, "touch %u at %d (thresh %u)", touch_value, touch_mapping[i], touch_thresh);
if (touch_callbacks[touch_mapping[i]] != NULL)
{
(*touch_callbacks[touch_mapping[i]])();
}
}
touch_status[i] = touch_status_current[i];
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void interface_start(void)
{
ESP_ERROR_CHECK(touch_pad_init());
ESP_ERROR_CHECK(touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V));
ESP_ERROR_CHECK(touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW));
touch_mapping[0] = TOUCH_PAD_ESC;
touch_mapping[1] = TOUCH_PAD_OK;
touch_mapping[2] = TOUCH_PAD_UP;
touch_mapping[3] = TOUCH_PAD_DOWN;
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
ESP_ERROR_CHECK(touch_pad_config(touch_mapping[i], 0));
}
ESP_ERROR_CHECK(touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD));
uint16_t touch_value;
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
ESP_ERROR_CHECK(touch_pad_read_filtered(touch_mapping[i], &touch_value));
ESP_ERROR_CHECK(touch_pad_set_thresh(touch_mapping[i], touch_value * 2 / 3));
ESP_LOGD(INTERFACE_LOG, "calibrate %u at %u (thresh %u)", touch_mapping[i], touch_value, (touch_value * 2 / 3));
}
xTaskCreate(&interface_run, "interface_run", 4096, NULL, 5, NULL);
}
int interface_get_state(void)
{
return current_interface_state;
}
void interface_set_state(interface_state_t state)
{
current_interface_state = state;
}