controls werken, wifi ook

This commit is contained in:
Daan Meijer 2025-04-21 14:40:20 +02:00
parent 5c8f5c894c
commit 067f76f71f
6 changed files with 41 additions and 17 deletions

View File

@ -41,7 +41,10 @@ board_build.f_cpu = 80000000L
board = dfrobot_beetle_esp32c3
platform = espressif32
framework = arduino
monitor_speed = 460800
monitor_speed = 115200
monitor_filters =
esp32_exception_decoder
time
lib_deps =
fastled/FastLED@^3.7.0
knolleary/PubSubClient@^2.8

View File

@ -7,10 +7,16 @@ uint8_t controlPins[] = {
PIN_KEY,
};
uint16_t thresholds[] = {
2048,
1000,
3072,
};
uint16_t prevState[3];
uint8_t controlPinCount = sizeof(controlPins) / sizeof(controlPins[0]);
auto lastChange = micros();
uint8_t pinChanges[128];
uint8_t pinChangeIndex = 0;
@ -39,28 +45,31 @@ void Controls_loop(void *pvParameters){
lastChange = 0;
while(true){
for(uint8_t a=0; a<sizeof(controlPins); a++){
for(uint8_t a=0; a<controlPinCount; a++){
auto pin = controlPins[a];
auto currentValueAnalog = analogRead(pin);
auto currentValue = currentValueAnalog > 3072;
auto currentValue = currentValueAnalog > thresholds[a];
auto prevValue = prevState[a];
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 200){
Serial.printf("Pin %d: %d->%d (%d)\n", pin, prevValue, currentValue, currentValueAnalog);
}
// if(abs(currentValue - prevValue) > 100){
if(currentValue != prevValue){
if(CONTROLS_DEBUG > 100){
Serial.printf("Change on pin %d: %d -> %d (%d)\n", pin, prevValue, currentValue, currentValueAnalog);
}
if(pin == PIN_KEY){
if(currentValue){
auto duration = micros() - lastKey;
if(duration > KEY_DURATION_MS * 1000){
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 10){
Serial.printf("Key: %d\n", duration);
}
Controls_send_event({Key, (int)duration});
}else{
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 20){
Serial.println("Too short");
}
}
@ -89,7 +98,7 @@ void Controls_loop(void *pvParameters){
pinChanges[base + 3] == PIN_B
){
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 10){
Serial.println("Dir A");
}
Controls_send_event({Counterclockwise, NULL});
@ -100,13 +109,14 @@ void Controls_loop(void *pvParameters){
pinChanges[base + 3] == PIN_A
){
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 10){
Serial.println("Dir B");
}
Controls_send_event({Clockwise, NULL});
}else{
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 20){
Serial.println("Unknown direction");
Serial.printf("[%d, %d, %d, %d]\n", pinChanges[base + 0], pinChanges[base + 1], pinChanges[base + 2], pinChanges[base + 3]);
}
}
}
@ -116,14 +126,14 @@ void Controls_loop(void *pvParameters){
if(lastChange){
if(micros() - lastChange > 100 * 1000){
if(CONTROLS_DEBUG){
if(CONTROLS_DEBUG > 100){
Serial.println("changes expired");
}
Controls_direction_reset();
}
}
vTaskDelay(1/portTICK_PERIOD_MS);
vTaskDelay(2/portTICK_PERIOD_MS);
}
}

View File

@ -2,13 +2,13 @@
#include <Arduino.h>
#define PIN_A 2
#define PIN_B 1
#define PIN_KEY 0
#define PIN_A 1
#define PIN_B 0
#define PIN_KEY 2
#define KEY_DURATION_MS 150
#define CONTROLS_DEBUG false
#define CONTROLS_DEBUG 1
enum ControlEventType {

View File

@ -8,6 +8,12 @@ CRGB colorTable[] = {
// CRGB(255, 100, 32),
// CRGB(255, 128, 32),
CRGB::White,
CRGB::Red,
CRGB::Green,
CRGB::Blue,
CRGB::Yellow,
CRGB::Turquoise,
CRGB::Purple,
CRGB(255, 142, 32), // "warm wit"
};

View File

@ -127,6 +127,8 @@ void Led_setup(){
FastLED.addLeds<WS2812B, PIN_STRIP2, BRG>(strip2, NUM_LEDS).setRgbw(RgbwDefault());
strip1[0] = CRGB::Black;
strip2[0] = CRGB::Black;
FastLED.show();
#endif
#ifdef LED_NEOPIXEL_BUS

View File

@ -30,8 +30,11 @@ void Wifi_loop(void* params){
{
while (WiFi.status() != WL_CONNECTED)
{
vTaskDelay(500 / portTICK_PERIOD_MS);
Serial.println("Wifi waiting...");
vTaskDelay(5000 / portTICK_PERIOD_MS);
if(WiFi.status() != WL_CONNECTED){
WiFi.reconnect();
}
}
Serial.println("");