diff --git a/.vscode/settings.json b/.vscode/settings.json index d3f782b..405c118 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,21 +1,78 @@ { - "files.associations": { - "optional": "cpp", - "istream": "cpp", - "ostream": "cpp", - "ratio": "cpp", - "system_error": "cpp", - "array": "cpp", - "functional": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "*.tcc": "cpp", - "chrono": "cpp", - "ctime": "cpp", - "iomanip": "cpp", - "limits": "cpp", - "numeric": "cpp", - "streambuf": "cpp" - } + "files.associations": { + "optional": "cpp", + "istream": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "system_error": "cpp", + "array": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "*.tcc": "cpp", + "chrono": "cpp", + "ctime": "cpp", + "iomanip": "cpp", + "limits": "cpp", + "numeric": "cpp", + "streambuf": "cpp" + }, + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bd21550..e0e587c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,99 +1,104 @@ #include #define PORT PWM_2 -class HallButton//TODO +class HallButton // TODO { public: - HallButton(byte pin); - unsigned long lowTime; - unsigned int lowDuration; - void Pulse(unsigned long time, bool end = false) + HallButton(byte pin) {}; + void Pulse(unsigned long time, bool high = false) { + if (high) + { + highTime = time - lastRead; + } + else + { + fallCount++; + lowTime = time - lastRead; + } + if (fallCount >= 2) + { + fallCount = 0; + percent = 100 * lowTime / (lowTime + highTime); + } + lastRead = time; } + byte fallCount; + // private: + bool isDone; + bool flag; + unsigned long lastRead; + unsigned long lowTime; + unsigned long highTime; + unsigned int lowDuration; + bool isClicked() + { + if (activatedPercent == 0 && abs(previousPercent - percent) >= threshold) + { + activatedPercent = percent; + return true; + } + else if (activatedPercent != 0 && abs(activatedPercent - percent) >= percent) + { + activatedPercent = 0; + return false; + } + return true; + } + byte threshold = 2; + byte activatedPercent; + byte percent; + byte previousPercent; }; -HallButton myHallButton[2] = {HallButton(2), HallButton(3)}; - -// bool is_gpio_fall(char *buf, uint32_t events); -byte event; -uint eventGpio; +HallButton myHallButton[2] = {HallButton(3), HallButton(5)}; void gpio_callback(uint gpio, uint32_t events) { - eventGpio = gpio; - event = events == 4? 1: events == 8? 2: 0; - // unsigned long current_time = micros(); - // bool is_fall = is_gpio_fall(event_str, events); - // if (is_fall) - // { - // high_time = current_time; - // high_duration = high_time - low_time; - // } - // else - // { - // low_time = current_time; - // low_duration = low_time - high_time; - // } + + myHallButton[gpio == 3 ? 0 : 1].Pulse(micros(), events == GPIO_IRQ_EDGE_RISE); } -// static const char *gpio_irq_str[] = { -// "LEVEL_LOW", // 0x1 -// "LEVEL_HIGH", // 0x2 -// "EDGE_FALL", // 0x4 -// "EDGE_RISE" // 0x8 -// }; -// bool is_gpio_fall(char *buf, uint32_t events) -// { -// bool value = false; -// for (uint i = 0; i < 4; i++) -// { -// uint mask = (1 << i); -// if (events & mask) -// { -// // Copy this event string into the user string -// const char *event_str = gpio_irq_str[i]; -// if (gpio_irq_str[i] == "EDGE_FALL") -// value = true; -// while (*event_str != '\0') -// { -// *buf++ = *event_str++; -// } -// events &= ~mask; - -// // If more events add ", " -// if (events) -// { -// *buf++ = ','; -// *buf++ = ' '; -// } -// } -// } -// *buf++ = '\0'; -// return value; -// } void setup() { - Serial.begin(9600); - gpio_set_input_enabled(PORT, true); - gpio_pull_up(PORT); + // gpio_set_irq_enabled_with_callback(PORT, GPIO_IRQ_EDGE_RISE, true, rising); + // alarmPool = alarm_pool_create(1, 1); + // alarm_pool_add_repeating_timer_us(alarmPool, 1, &timer_callback, NULL, &timer); + gpio_set_irq_enabled_with_callback(3, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); + gpio_set_irq_enabled(5, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true); + // gpio_set_irq_enabled_with_callback(5, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); + _gpio_init(3); + _gpio_init(5); gpio_set_input_enabled(3, true); gpio_pull_up(3); - // gpio_set_irq_enabled_with_callback(PORT, GPIO_IRQ_EDGE_RISE, true, rising); - gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); - gpio_set_irq_enabled_with_callback(3, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); + gpio_set_input_enabled(5, true); + gpio_pull_up(5); + Serial.begin(115200); } -unsigned long flag = 0; +void parseSerial() +{ + switch (Serial.read()) + { + case 'a': + int threshold = Serial.parseInt(); + Serial.println(String(threshold)); + break; + } +} +unsigned long flag; void loop() { - // static byte lowest = 100; - // byte value = GetPWM(PORT); - // if (value < lowest) - // lowest = value; - // Serial.print("$" + String(GetPWM(PORT)) + ";\n"); - // if (is_done) - Serial.print("$" + String(eventGpio) + " " + String(event) + ";\n"); - // unsigned long currentTime = millis(); - // if (currentTime >= flag + 1) - // { - // flag = currentTime; - // } + // int width = myHallButton[0].percent; + // Serial.println(String(width)); + // if (Serial.available() > 0) + // parseSerial(); + uint64_t timer = millis(); + if (timer > flag + 100) + { + flag = timer; + for (size_t i = 0; i < 2; i++) + { + Serial.print(String(myHallButton[i].fallCount) + "\t" + String(myHallButton[i].percent) + "\t" + String(myHallButton[i].lowTime) + "\t" + String(myHallButton[i].highTime) + "\t"); + } + Serial.print("\n"); + } }