Skip to content

Commit

Permalink
gen2: basic touch sensor support (#17)
Browse files Browse the repository at this point in the history
Play audio sample on touch event.
  • Loading branch information
matslina authored Aug 28, 2024
1 parent a9fe99f commit e0297de
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
#include "gfx.h"
#include "remote.h"
#include "sdkconfig.h"
#include "touch.h"
#include "wifi.h"

static const char* TAG = "main";

void _on_touch() {
ESP_LOGI(TAG, "Touch detected");
audio_play(ASSET_LAZY_DADDY_MP3, ASSET_LAZY_DADDY_MP3_LEN);
}

void app_main(void) {
ESP_LOGI(TAG, "Hello world!");

Expand Down Expand Up @@ -45,6 +51,12 @@ void app_main(void) {
return;
}

// Setup touch.
if (touch_initialize(_on_touch) != ESP_OK) {
ESP_LOGE(TAG, "failed to initialize touch");
return;
}

uint8_t mac[6];
if (!wifi_get_mac(mac)) {
ESP_LOGI(TAG, "WiFi MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1],
Expand Down
101 changes: 101 additions & 0 deletions src/touch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "touch.h"

#ifdef TIDBYT_GEN2

#include <driver/gpio.h>
#include <driver/touch_pad.h>
#include <esp_err.h>
#include <esp_log.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdint.h>

#define TOUCH_PAD_NUM TOUCH_PAD_NUM8
#define DEBOUNCE_TIME_MICROS (250 * 1000)
#define TOUCH_FILTER_PERIOD_MILLIS 10

const char *TAG = "touch";

touch_callback_t _callback = NULL;
uint16_t _threshold = 0;
uint64_t _lastTouch = 0;

static void _filterCallback(uint16_t *raw, uint16_t *filtered) {
uint16_t val = filtered[TOUCH_PAD_NUM];

uint64_t now = esp_timer_get_time();
if (val < _threshold && now - _lastTouch > DEBOUNCE_TIME_MICROS) {
_lastTouch = now;
if (_callback != NULL) {
_callback();
}
}
}

esp_err_t touch_initialize(touch_callback_t callback) {
_callback = callback;

esp_err_t err = touch_pad_init();
if (err != ESP_OK) {
return err;
}

touch_pad_set_measurement_interval(TOUCH_PAD_SLEEP_CYCLE_DEFAULT / 4);
touch_pad_set_measurement_clock_cycles(TOUCH_PAD_MEASURE_CYCLE_DEFAULT / 2);

err = touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
if (err != ESP_OK) {
return err;
}

err = touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5,
TOUCH_HVOLT_ATTEN_1V);
if (err != ESP_OK) {
return err;
}

err = touch_pad_filter_start(TOUCH_FILTER_PERIOD_MILLIS);
if (err != ESP_OK) {
return err;
}

err = touch_pad_config(TOUCH_PAD_NUM, 0);
if (err != ESP_OK) {
return err;
}

ESP_LOGI(TAG, "Reading current touch pad values");
uint16_t thresh = 0;
for (int i = 0; i < 3; i++) {
vTaskDelay(pdMS_TO_TICKS(100));
uint16_t x;
touch_pad_read_filtered(TOUCH_PAD_NUM, &x);

if (x > thresh) {
thresh = x;
}
}

_threshold = (thresh * 95) / 100;

ESP_LOGI(TAG, "Touch threshold: %d", _threshold);

err = touch_pad_config(TOUCH_PAD_NUM, 0);
if (err != ESP_OK) {
return err;
}

err = touch_pad_set_filter_read_cb(_filterCallback);
if (err != ESP_OK) {
return err;
}

return ESP_OK;
}

#else

esp_err_t touch_initialize(touch_callback_t callback) { return ESP_OK; }

#endif
7 changes: 7 additions & 0 deletions src/touch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <esp_err.h>

typedef void (*touch_callback_t)();

esp_err_t touch_initialize(touch_callback_t callback);

0 comments on commit e0297de

Please sign in to comment.