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"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "driver/touch_pad.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
#include "interface.h"
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
static int current_interface_state = INTERFACE_STATE_IDLE;
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-07-14 20:54:14 +02:00
|
|
|
static int touch_mapping[TOUCH_PAD_COUNT] = {0};
|
|
|
|
static bool touch_status[TOUCH_PAD_COUNT] = {0};
|
2020-07-27 16:54:51 +02:00
|
|
|
static interface_touch_callback touch_callbacks[TOUCH_PAD_MAX];
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
void interface_register_touch_callback(int touch_pad, interface_touch_callback callback)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
|
|
|
touch_callbacks[touch_pad] = callback;
|
2020-07-13 20:32:53 +02:00
|
|
|
}
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
void interface_run(void *pvParameter)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-07-22 21:44:17 +02:00
|
|
|
static uint16_t touch_value;
|
|
|
|
static uint16_t touch_thresh;
|
|
|
|
static bool touch_status_current[TOUCH_PAD_MAX] = {0};
|
2020-07-12 14:14:06 +02:00
|
|
|
while (1)
|
|
|
|
{
|
2020-07-14 20:54:14 +02:00
|
|
|
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-07-22 21:44:17 +02:00
|
|
|
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));
|
2020-07-14 20:54:14 +02:00
|
|
|
touch_status_current[i] = touch_value < touch_thresh;
|
2020-07-12 14:14:06 +02:00
|
|
|
|
2020-07-14 20:54:14 +02:00
|
|
|
if (!touch_status[i] & touch_status_current[i])
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-07-27 16:54:51 +02:00
|
|
|
ESP_LOGD(INTERFACE_LOG, "touch %u at %d (thresh %u)", touch_value, touch_mapping[i], touch_thresh);
|
2020-07-22 21:44:17 +02:00
|
|
|
if (touch_callbacks[touch_mapping[i]] != NULL)
|
2020-07-14 20:54:14 +02:00
|
|
|
{
|
2020-07-22 21:44:17 +02:00
|
|
|
(*touch_callbacks[touch_mapping[i]])();
|
2020-07-14 20:54:14 +02:00
|
|
|
}
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
|
|
|
touch_status[i] = touch_status_current[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
void interface_start(void)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
|
|
|
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));
|
2020-07-14 20:54:14 +02:00
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
2020-07-22 21:44:17 +02:00
|
|
|
ESP_ERROR_CHECK(touch_pad_config(touch_mapping[i], 0));
|
2020-07-14 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 14:14:06 +02:00
|
|
|
ESP_ERROR_CHECK(touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD));
|
2020-07-14 20:54:14 +02:00
|
|
|
uint16_t touch_value;
|
|
|
|
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
|
|
|
|
{
|
2020-07-22 21:44:17 +02:00
|
|
|
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));
|
2020-07-27 16:54:51 +02:00
|
|
|
ESP_LOGD(INTERFACE_LOG, "calibrate %u at %u (thresh %u)", touch_mapping[i], touch_value, (touch_value * 2 / 3));
|
2020-07-14 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
xTaskCreate(&interface_run, "interface_run", 4096, NULL, 5, NULL);
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
int interface_get_state(void)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-07-27 16:54:51 +02:00
|
|
|
return current_interface_state;
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 16:54:51 +02:00
|
|
|
void interface_set_state(interface_state_t state)
|
2020-07-12 14:14:06 +02:00
|
|
|
{
|
2020-07-27 16:54:51 +02:00
|
|
|
current_interface_state = state;
|
2020-07-12 14:14:06 +02:00
|
|
|
}
|