diff --git a/src/button.cpp b/src/button.cpp index 1554d4b31716de01d60a5b44e6ca2fe26a6801d1..735388d02519424722509fe719b319eeb0bf782b 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 e201705605138debed592c9303d25e738077646b..d99348524c32083bc5f908949f4c8ac3e0630e13 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 0737fba858a9861ace8483ca4287c0478dfbbbee..298397db3a72f40baf98324f98f5fc583ff48a06 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 60efa3b2ec83717dc2134ea2516044995aafe6f8..24fbbbb82d69a084a257538f57c6a4072ed7b2e4 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 2abf7dc3119f545d5880cacfa8db52cd83616d5f..b8f02caaf1be16a6361b8bfe7b75e534a67a8dd7 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 9c85d37e48c63a0692e8282807d3f37bfb47c372..b824fbabaeec5f28af7f7546bacc5dc424e7d6f3 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 acdf10b9e5521da75e1af0085353d617d121ad8e..b7dbcb934cf6c6da844822e6c7aed0e44f2ec28e 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 cef47588ec30c20b4ecac22e7c58d8fa785ae4e1..e9e5778f294d6c2a59a3e7bc588e38bb659838d2 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)));