Last active
February 23, 2024 12:35
-
-
Save schreibfaul1/5f7ea39bf90156e82e0c5dc7e81329e6 to your computer and use it in GitHub Desktop.
ESP32_ Hob2Hood
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#define VENTILATION_OFF 0x00D8D9DA | |
#define VENTILATION_LEVEL1 0x006C6D6E | |
#define VENTILATION_LEVEL2 0x0037B838 | |
#define VENTILATION_LEVEL3 0x003878B8 | |
#define VENTILATION_LEVEL4 0x00727374 // not used, have only three | |
#define LIGHT_ON 0x00D2D3D4 | |
#define LIGHT_OFF 0x001ABADA | |
#define PIN_LIGHT 5 | |
#define PIN_SPEED_1 23 | |
#define PIN_SPEED_2 19 | |
#define PIN_SPEED_3 18 | |
#define PIN_SPEED_4 26 | |
#define PIN_IR 36 | |
DRAM_ATTR uint32_t ir_result = 0; | |
void IRAM_ATTR isr_IR(); // prototypes | |
uint32_t g_time = 0; | |
void setup(){ | |
Serial.begin(115200); | |
pinMode(PIN_LIGHT, OUTPUT_OPEN_DRAIN); | |
pinMode(PIN_SPEED_1, OUTPUT_OPEN_DRAIN); | |
pinMode(PIN_SPEED_2, OUTPUT_OPEN_DRAIN); | |
pinMode(PIN_SPEED_3, OUTPUT_OPEN_DRAIN); | |
pinMode(PIN_SPEED_4, OUTPUT_OPEN_DRAIN); | |
pinMode(PIN_IR, INPUT_PULLUP); | |
digitalWrite(PIN_SPEED_1, HIGH); | |
digitalWrite(PIN_SPEED_2, HIGH); | |
digitalWrite(PIN_SPEED_3, HIGH); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
digitalWrite(PIN_LIGHT, HIGH); | |
attachInterrupt(PIN_IR, isr_IR, CHANGE); // Interrupts will be handle by isr_IR | |
} | |
void loop() { | |
static char res[10]; | |
static uint32_t result = 0; | |
if(ir_result) { | |
if(result != ir_result) { g_time = millis(); } | |
result = ir_result; | |
if(g_time + 30 < millis()) { // 30ms no PIN_IR level change? | |
sprintf(res, "%08x", ir_result); | |
if(ir_result == VENTILATION_OFF) { | |
digitalWrite(PIN_SPEED_1, HIGH); | |
digitalWrite(PIN_SPEED_2, HIGH); | |
digitalWrite(PIN_SPEED_3, HIGH); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
} | |
if(ir_result == VENTILATION_LEVEL1) { | |
digitalWrite(PIN_SPEED_1, LOW); | |
digitalWrite(PIN_SPEED_2, HIGH); | |
digitalWrite(PIN_SPEED_3, HIGH); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
} | |
if(ir_result == VENTILATION_LEVEL2) { | |
digitalWrite(PIN_SPEED_1, HIGH); | |
digitalWrite(PIN_SPEED_2, LOW); | |
digitalWrite(PIN_SPEED_3, HIGH); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
} | |
if(ir_result == VENTILATION_LEVEL3) { | |
digitalWrite(PIN_SPEED_1, HIGH); | |
digitalWrite(PIN_SPEED_2, HIGH); | |
digitalWrite(PIN_SPEED_3, LOW); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
} | |
if(ir_result == VENTILATION_LEVEL4) { | |
digitalWrite(PIN_SPEED_1, HIGH); | |
digitalWrite(PIN_SPEED_2, HIGH); | |
digitalWrite(PIN_SPEED_3, LOW); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
} | |
if(ir_result == LIGHT_OFF) { | |
digitalWrite(PIN_LIGHT, HIGH); // all off | |
digitalWrite(PIN_SPEED_1, HIGH); | |
digitalWrite(PIN_SPEED_2, HIGH); | |
digitalWrite(PIN_SPEED_3, HIGH); | |
digitalWrite(PIN_SPEED_4, HIGH); | |
} | |
if(ir_result == LIGHT_ON) { digitalWrite(PIN_LIGHT, LOW); } | |
ir_result = 0; | |
} | |
} | |
} | |
//**************************************************************************************************************************************************** | |
// I R S T U F F * | |
//**************************************************************************************************************************************************** | |
// Each time unit is 700ms long. A level change takes place after 700ms or a multiple of this. The entire sequence is about 17 ms long. | |
// ___________ ___ ___ ___ ___ ___ ___ _______________________ | |
// |___ ___| |___| |___ ___| |___| | |
// 2 2 1 3 2 1 1 | |
void IRAM_ATTR isr_IR() { | |
int32_t t1 = 0, intval_h = 0, intval_l = 0; // Current time and interval since last change | |
static uint8_t pulsecounter = 0; // Counts the pulse | |
static uint32_t t0 = 0; // To get the interval | |
static uint32_t result = 0; | |
t1 = micros(); // Get current time | |
if(!digitalRead(PIN_IR)) { | |
intval_h = t1 - t0; // Compute interval, only high | |
uint8_t times = 0; | |
if(intval_h > 700 - 150 && intval_h < 700 + 150) { times = 1; } // 1x | |
else if(intval_h > 2 * 700 - 150 && intval_h < 2 * 700 + 150) { times = 2; } // 2x | |
else if(intval_h > 3 * 700 - 150 && intval_h < 3 * 700 + 150) { times = 3; } // 3x | |
else if(intval_h > 4 * 700 - 150 && intval_h < 4 * 700 + 150) { times = 4; } // 4x | |
else if(intval_h > 5 * 700 - 150 && intval_h < 5 * 700 + 150) { times = 5; } // 5x | |
else if(intval_h > 6 * 700 - 150 && intval_h < 6 * 700 + 150) { times = 6; } // 6x | |
else if(intval_h > 7 * 700 - 150 && intval_h < 7 * 700 + 150) { times = 7; } // 7x | |
else { // ??? | |
result = 0; | |
pulsecounter = 0; | |
} | |
for(uint8_t i = 0; i < times; i++) { | |
result <<= 1; | |
result += 1; | |
} | |
pulsecounter += times; | |
} | |
else { | |
intval_l = t1 - t0; // Compute interval, only low | |
uint8_t times = 0; | |
if(intval_l > 700 - 150 && intval_l < 700 + 150) { times = 1; } // 1x | |
else if(intval_l > 2 * 700 - 150 && intval_l < 2 * 700 + 150) { times = 2; } // 2x | |
else if(intval_l > 3 * 700 - 150 && intval_l < 3 * 700 + 150) { times = 3; } // 3x | |
else if(intval_l > 4 * 700 - 150 && intval_l < 4 * 700 + 150) { times = 4; } // 4x | |
else if(intval_l > 5 * 700 - 150 && intval_l < 5 * 700 + 150) { times = 5; } // 5x | |
else if(intval_l > 6 * 700 - 150 && intval_l < 6 * 700 + 150) { times = 6; } // 6x | |
else if(intval_l > 7 * 700 - 150 && intval_l < 7 * 700 + 150) { times = 7; } // 7x | |
else { // ??? | |
result = 0; | |
pulsecounter = 0; | |
} | |
for(uint8_t i = 0; i < times; i++) { | |
result <<= 1; | |
result += 0; | |
} | |
pulsecounter += times; | |
} | |
t0 = t1; | |
if(pulsecounter > 20 )ir_result = result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment