2020-07-14 20:54:14 +02:00
|
|
|
// 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
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-07-14 20:54:14 +02:00
|
|
|
// 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.
|
2020-07-12 14:14:06 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2020-12-06 12:48:12 +01:00
|
|
|
#include <freertos/timers.h>
|
2020-08-16 16:40:05 +02:00
|
|
|
#include "driver/gpio.h"
|
2020-07-12 14:14:06 +02:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2020-09-29 19:58:01 +02:00
|
|
|
#include "display.h"
|
|
|
|
#include "display-gfx.h"
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-08-16 16:40:05 +02:00
|
|
|
#include "interface.h"
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-09-29 19:58:01 +02:00
|
|
|
static interface_command_callback command_callbacks[INTERFACE_COMMANDS_SIZE];
|
2020-12-12 22:26:37 +01:00
|
|
|
static bool command_callback_trigger[INTERFACE_COMMANDS_SIZE];
|
2020-08-16 16:40:05 +02:00
|
|
|
static interface_display_function current_display_function;
|
2020-12-06 12:48:12 +01:00
|
|
|
static interface_display_function current_display_refresh_function;
|
|
|
|
|
|
|
|
static TimerHandle_t interface_idle_timer;
|
|
|
|
static bool interface_idle = false;
|
2020-12-12 14:26:08 +01:00
|
|
|
static bool busy = false;
|
|
|
|
|
|
|
|
bool interface_is_idle(void)
|
|
|
|
{
|
|
|
|
return interface_idle;
|
|
|
|
}
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-09-29 19:58:01 +02:00
|
|
|
void interface_register_command_callback(interface_command_t command, interface_command_callback callback)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-09-29 19:58:01 +02:00
|
|
|
command_callbacks[command] = callback;
|
2020-12-12 22:26:37 +01:00
|
|
|
command_callback_trigger[command] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void interface_command_callback_set_trigger(interface_command_t command)
|
|
|
|
{
|
|
|
|
command_callback_trigger[command] = true;
|
2020-07-13 20:32:53 +02:00
|
|
|
}
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-08-16 16:40:05 +02:00
|
|
|
void interface_set_display_function(interface_display_function display_function)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-12-12 14:26:08 +01:00
|
|
|
busy = true;
|
2020-09-29 19:58:01 +02:00
|
|
|
display_clear();
|
2020-12-12 14:26:08 +01:00
|
|
|
current_display_refresh_function = NULL;
|
2020-08-16 16:40:05 +02:00
|
|
|
current_display_function = display_function;
|
2020-12-12 14:26:08 +01:00
|
|
|
if (current_display_function != NULL)
|
|
|
|
{
|
|
|
|
(*current_display_function)();
|
|
|
|
}
|
|
|
|
busy = false;
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-06 12:48:12 +01:00
|
|
|
void interface_set_display_refresh_function(interface_display_function display_function)
|
|
|
|
{
|
2020-12-12 14:26:08 +01:00
|
|
|
busy = true;
|
2020-12-06 12:48:12 +01:00
|
|
|
current_display_refresh_function = display_function;
|
2020-12-12 14:26:08 +01:00
|
|
|
if (current_display_function != NULL)
|
|
|
|
{
|
|
|
|
(*current_display_function)();
|
|
|
|
}
|
|
|
|
busy = false;
|
2020-12-06 12:48:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 19:58:01 +02:00
|
|
|
void interface_execute_command(interface_command_t command)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-12-06 12:48:12 +01:00
|
|
|
if (!interface_idle && command_callbacks[command] != NULL)
|
2020-08-16 16:40:05 +02:00
|
|
|
{
|
2020-12-06 12:48:12 +01:00
|
|
|
xTimerReset(interface_idle_timer, 0);
|
2020-09-29 19:58:01 +02:00
|
|
|
(*command_callbacks[command])();
|
2020-12-12 14:26:08 +01:00
|
|
|
if (!busy)
|
2020-12-06 12:48:12 +01:00
|
|
|
{
|
2020-12-12 14:26:08 +01:00
|
|
|
busy = true;
|
2020-12-06 12:48:12 +01:00
|
|
|
if (current_display_function != NULL)
|
|
|
|
{
|
|
|
|
(*current_display_function)();
|
|
|
|
}
|
2020-12-12 14:26:08 +01:00
|
|
|
busy = false;
|
2020-12-06 12:48:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (interface_idle && command == INTERFACE_COMMAND_SET)
|
|
|
|
{
|
|
|
|
xTimerReset(interface_idle_timer, 0);
|
|
|
|
interface_idle = false;
|
|
|
|
display_on(true);
|
2020-08-16 16:40:05 +02:00
|
|
|
}
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 22:26:37 +01:00
|
|
|
void interface_execute_command_trigger(interface_command_t command)
|
|
|
|
{
|
|
|
|
if (command_callback_trigger[command])
|
|
|
|
{
|
|
|
|
interface_execute_command(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 16:40:05 +02:00
|
|
|
void interface_display_task(void *pvParameter)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-12-06 12:48:12 +01:00
|
|
|
xTimerStart(interface_idle_timer, 0);
|
|
|
|
|
2020-08-16 16:40:05 +02:00
|
|
|
while (1)
|
|
|
|
{
|
2020-12-12 14:26:08 +01:00
|
|
|
if (!interface_idle && !busy && current_display_refresh_function != NULL)
|
2020-08-16 16:40:05 +02:00
|
|
|
{
|
2020-12-06 12:48:12 +01:00
|
|
|
(*current_display_refresh_function)();
|
2020-12-12 14:26:08 +01:00
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
2020-08-16 16:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-06 12:48:12 +01:00
|
|
|
void interface_idle_callback(TimerHandle_t timer)
|
|
|
|
{
|
|
|
|
display_on(false);
|
|
|
|
interface_idle = true;
|
|
|
|
}
|
|
|
|
|
2020-08-16 16:40:05 +02:00
|
|
|
void interface_start(void)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-12-06 12:48:12 +01:00
|
|
|
|
|
|
|
interface_idle_timer = xTimerCreate(
|
|
|
|
"interface_idle",
|
2020-12-12 14:26:08 +01:00
|
|
|
(INTERFACE_IDLE_SECONDS * 1000) / portTICK_PERIOD_MS,
|
2020-12-06 12:48:12 +01:00
|
|
|
false,
|
|
|
|
NULL,
|
|
|
|
interface_idle_callback);
|
2020-08-16 16:40:05 +02:00
|
|
|
|
|
|
|
// init label
|
|
|
|
interface_init_label();
|
|
|
|
|
2020-09-29 19:58:01 +02:00
|
|
|
display_start();
|
|
|
|
display_clear();
|
2020-08-16 16:40:05 +02:00
|
|
|
|
2020-12-06 12:48:12 +01:00
|
|
|
xTaskCreate(&interface_display_task, "interface_display_task", 4096, NULL, 5, NULL);
|
2020-12-17 10:21:10 +01:00
|
|
|
|
|
|
|
interface_input_start();
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
2020-12-12 14:26:08 +01:00
|
|
|
|
|
|
|
void interface_flipped(bool flipped)
|
|
|
|
{
|
|
|
|
busy = true;
|
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
display_clear();
|
|
|
|
display_flipped(flipped);
|
|
|
|
if (current_display_function != NULL)
|
|
|
|
{
|
|
|
|
(*current_display_function)();
|
|
|
|
}
|
|
|
|
busy = false;
|
|
|
|
}
|