This commit is contained in:
Maksym 2024-08-11 20:23:12 +02:00
parent 58dc8c5f06
commit 8d9a157577
2 changed files with 159 additions and 97 deletions

95
.vscode/settings.json vendored
View File

@ -1,21 +1,78 @@
{ {
"files.associations": { "files.associations": {
"optional": "cpp", "optional": "cpp",
"istream": "cpp", "istream": "cpp",
"ostream": "cpp", "ostream": "cpp",
"ratio": "cpp", "ratio": "cpp",
"system_error": "cpp", "system_error": "cpp",
"array": "cpp", "array": "cpp",
"functional": "cpp", "functional": "cpp",
"tuple": "cpp", "tuple": "cpp",
"type_traits": "cpp", "type_traits": "cpp",
"utility": "cpp", "utility": "cpp",
"*.tcc": "cpp", "*.tcc": "cpp",
"chrono": "cpp", "chrono": "cpp",
"ctime": "cpp", "ctime": "cpp",
"iomanip": "cpp", "iomanip": "cpp",
"limits": "cpp", "limits": "cpp",
"numeric": "cpp", "numeric": "cpp",
"streambuf": "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
} }

View File

@ -1,99 +1,104 @@
#include <Arduino.h> #include <Arduino.h>
#define PORT PWM_2 #define PORT PWM_2
class HallButton//TODO class HallButton // TODO
{ {
public: public:
HallButton(byte pin); HallButton(byte pin) {};
unsigned long lowTime; void Pulse(unsigned long time, bool high = false)
unsigned int lowDuration;
void Pulse(unsigned long time, bool end = 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)}; HallButton myHallButton[2] = {HallButton(3), HallButton(5)};
// bool is_gpio_fall(char *buf, uint32_t events);
byte event;
uint eventGpio;
void gpio_callback(uint gpio, uint32_t events) void gpio_callback(uint gpio, uint32_t events)
{ {
eventGpio = gpio;
event = events == 4? 1: events == 8? 2: 0; myHallButton[gpio == 3 ? 0 : 1].Pulse(micros(), events == GPIO_IRQ_EDGE_RISE);
// 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;
// }
} }
// 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() void setup()
{ {
Serial.begin(9600); // gpio_set_irq_enabled_with_callback(PORT, GPIO_IRQ_EDGE_RISE, true, rising);
gpio_set_input_enabled(PORT, true); // alarmPool = alarm_pool_create(1, 1);
gpio_pull_up(PORT); // 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_set_input_enabled(3, true);
gpio_pull_up(3); gpio_pull_up(3);
// gpio_set_irq_enabled_with_callback(PORT, GPIO_IRQ_EDGE_RISE, true, rising); gpio_set_input_enabled(5, true);
gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); gpio_pull_up(5);
gpio_set_irq_enabled_with_callback(3, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback); 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() void loop()
{ {
// static byte lowest = 100; // int width = myHallButton[0].percent;
// byte value = GetPWM(PORT); // Serial.println(String(width));
// if (value < lowest) // if (Serial.available() > 0)
// lowest = value; // parseSerial();
// Serial.print("$" + String(GetPWM(PORT)) + ";\n"); uint64_t timer = millis();
// if (is_done) if (timer > flag + 100)
Serial.print("$" + String(eventGpio) + " " + String(event) + ";\n"); {
// unsigned long currentTime = millis(); flag = timer;
// if (currentTime >= flag + 1) for (size_t i = 0; i < 2; i++)
// { {
// flag = currentTime; 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");
}
} }