From f68fc7a9235a3d3bbea866aed6eedc3e1068cfde Mon Sep 17 00:00:00 2001 From: Jan Grewe <jan@faked.org> Date: Tue, 18 Jun 2024 17:01:29 +0200 Subject: [PATCH] add region switching via doubleclick --- src/button.cpp | 25 +++++++++++++++++++++++-- src/button.h | 1 + src/ir.cpp | 27 +++++++++++++++------------ src/ir.h | 2 +- src/led.cpp | 5 +++++ src/led.h | 3 +++ src/main.cpp | 6 ++++-- src/main.h | 4 ++-- 8 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/button.cpp b/src/button.cpp index 1554d4b..735388d 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -1,6 +1,7 @@ #include <Arduino.h> #include <InterruptButton.h> #include "config.h" +#include "main.h" #include "button.h" #include "utils.h" #include "led.h" @@ -11,20 +12,38 @@ void handleShortButton() { if (!sendingEnabled) { - logLine("Starting to send."); + logLine("Starting to send. (Region: "+ String(region)+")"); blink(1); digitalWrite(PIN_LED, HIGH); } else { logLine("Restarting sending."); - blink(2); + blink(1); digitalWrite(PIN_LED, HIGH); cancelSending = true; } sendingEnabled = true; } +void handleDoubleButton() { + digitalWrite(PIN_LED, LOW); + delay(500); + if (region == EU) { + region = NA; + blink(2); + } else { + region = EU; + blink(1); + } + logLine("Switching Region to: " + String(region)); + + if (sendingEnabled) { + digitalWrite(PIN_LED, HIGH); + } + cancelSending = true; +} + void handleLongButton() { if (sendingEnabled) @@ -40,5 +59,7 @@ void setupButton() { button.bind(Event_KeyPress, &handleShortButton); button.bind(Event_LongKeyPress, &handleLongButton); + button.bind(Event_DoubleClick, &handleDoubleButton); button.setLongPressInterval(1000); + button.setDoubleClickInterval(500); } diff --git a/src/button.h b/src/button.h index e201705..d993485 100644 --- a/src/button.h +++ b/src/button.h @@ -1,5 +1,6 @@ extern bool sendingEnabled; extern bool cancelSending; +extern uint8_t region; void handleShortButton(); void handleLongButton(); diff --git a/src/ir.cpp b/src/ir.cpp index 0737fba..298397d 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -19,9 +19,9 @@ extern void callHandlers(); void xmitCodeElement(uint16_t ontime, uint16_t offtime, uint8_t PWM_code); -uint8_t read_bits(uint8_t count); uint16_t rawData[300]; - +uint16_t ontime, offtime; +uint8_t i, num_codes; uint8_t bitsleft_r = 0; uint8_t bits_r = 0; uint8_t code_ptr; @@ -53,10 +53,6 @@ uint8_t read_bits(uint8_t count) return tmp; } -uint16_t ontime, offtime; -uint8_t i, num_codes; -uint8_t region; - void delay_ten_us(uint16_t us) { uint8_t timer; @@ -74,26 +70,33 @@ void delay_ten_us(uint16_t us) } } -void sendAllCodes() +void sendAllCodes(uint8_t region) { - region = EU; - num_codes = num_EUcodes; + if (region == EU) + { + num_codes = num_EUcodes; + } + else + { + num_codes = num_NAcodes; + } // for every POWER code in our collection for (i = 0; i < num_codes; i++) { + // make sure all loop() handlers are still ran while sending callHandlers(); // point to next POWER code, from the right database - if (region == NA) + if (region == EU) { - powerCode = NApowerCodes[i]; + powerCode = EUpowerCodes[i]; } else { - powerCode = EUpowerCodes[i]; + powerCode = NApowerCodes[i]; } // Read the carrier frequency from the first byte of code structure diff --git a/src/ir.h b/src/ir.h index 60efa3b..24fbbbb 100644 --- a/src/ir.h +++ b/src/ir.h @@ -4,5 +4,5 @@ uint8_t read_bits(uint8_t count); void delay_ten_us(uint16_t us); -void sendAllCodes(); +void sendAllCodes(uint8_t region); void setupIR(); diff --git a/src/led.cpp b/src/led.cpp index 2abf7dc..b8f02ca 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -14,3 +14,8 @@ void blink(int count) delay(200); } } + +void setupLED() { + pinMode(PIN_LED, OUTPUT); + blink(region + 1); +} diff --git a/src/led.h b/src/led.h index 9c85d37..b824fba 100644 --- a/src/led.h +++ b/src/led.h @@ -1,3 +1,6 @@ #include <Arduino.h> void blink(int count); +void setupLED(); + +extern uint8_t region; diff --git a/src/main.cpp b/src/main.cpp index acdf10b..b7dbcb9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ bool sendingEnabled = false; bool cancelSending = false; +uint8_t region = 0; void setup() { @@ -19,13 +20,14 @@ void setup() setupWifi(); setupOTA(); setupTelnet(); + setupLED(); setupButton(); setupIR(); logLine(""); logLine("Hool-B-Gone ready!"); logLine(""); - blink(1); + logLine("Region: " + String(region)); } void callHandlers() @@ -38,7 +40,7 @@ void loop() { if (sendingEnabled) { - sendAllCodes(); + sendAllCodes(region); } else { diff --git a/src/main.h b/src/main.h index cef4758..e9e5778 100644 --- a/src/main.h +++ b/src/main.h @@ -10,8 +10,8 @@ By Anton Grimpelhuber (anton.grimpelhuber@gmail.com) // NA is for North America, Asia, and the rest of the world not covered by EU // Two regions! -#define NA 1 // set by a HIGH on REGIONSWITCH pin -#define EU 0 // set by a LOW on REGIONSWITCH pin +#define EU 0 +#define NA 1 // Lets us calculate the size of the NA/EU databases #define NUM_ELEM(x) (sizeof(x) / sizeof(*(x))); -- GitLab