test again

This commit is contained in:
Maksym 2024-08-18 00:52:50 +02:00
parent 1aa94aaf94
commit 6cc203eef2

View File

@ -3,89 +3,98 @@
class HallButton // TODO class HallButton // TODO
{ {
public: public:
HallButton(byte pin) {}; byte pin;
HallButton(byte pin)
{
this->pin = pin;
};
void Pulse(unsigned long time, bool high = false) void Pulse(unsigned long time, bool high = false)
{ {
if (high) if (high)
{ {
highTime = time - lastRead; if(++count >= 8) count =0;
highTime[count] = time - lastRead;
} }
else else
{ {
fallCount++; lowTime[count] = time - lastRead;
lowTime = time - lastRead;
}
if (fallCount >= 2)
{
fallCount = 0;
percent = 100 * lowTime / (lowTime + highTime);
} }
lastRead = time; lastRead = time;
} }
byte fallCount; byte count = 0;
// private:
bool isWaitingForHigh;
unsigned long lastRead; unsigned long lastRead;
unsigned long lowTime; unsigned long lowTime[8];
unsigned long highTime; unsigned long highTime[8];
unsigned int lowDuration; byte getPercent()
bool isClicked()
{ {
if (activatedPercent == 0 && abs(previousPercent - percent) >= threshold) uint sumLow = 0;
uint sumHigh = 0;
for (size_t i = 0; i < sizeof(lowTime); i++)
{ {
activatedPercent = percent; sumLow += lowTime[i];
return true; sumHigh += highTime[i];
} }
else if (activatedPercent != 0 && abs(activatedPercent - percent) >= percent) byte newPercent = 100 * sumHigh / (sumHigh + sumLow);
{ // percent[count] = newPercent;
activatedPercent = 0; // if (++count >= sizeof(percent))
return false; // count = 0;
} // byte middle = (max(percent[0], percent[1]) == max(percent[1], percent[2])) ? max(percent[0], percent[2]) : max(percent[1], min(percent[0], percent[2]));
return true;
return newPercent;
} }
byte threshold = 2; byte percent[8];
byte activatedPercent;
byte percent;
byte previousPercent;
}; };
HallButton myHallButton[2] = {HallButton(3), HallButton(5)}; HallButton myHallButton[2] = {HallButton(3), HallButton(2)};
void gpio_callback(uint gpio, uint32_t events) void gpio_callback(uint gpio, uint32_t events)
{ {
byte hallButtonIndex = gpio == 3 ? 0 : 1; byte hallButtonIndex = gpio == 3 ? 0 : 1;
byte secondHallButtonIndex = hallButtonIndex == 0 ? 1 : 0; // byte secondHallButtonIndex = hallButtonIndex == 0 ? 1 : 0;
unsigned long timer = micros(); unsigned long timer = time_us_64();
myHallButton[hallButtonIndex].Pulse(timer, events == GPIO_IRQ_EDGE_RISE); myHallButton[hallButtonIndex].Pulse(timer, events == GPIO_IRQ_EDGE_RISE);
myHallButton[secondHallButtonIndex].Pulse(timer, gpio_get(secondHallButtonIndex == 1 ? 5 : 3)); // myHallButton[secondHallButtonIndex].Pulse(timer, gpio_get(myHallButton[secondHallButtonIndex].pin) != 0);
} }
void setup() void setup()
{ {
digitalWrite(23, HIGH);
gpio_set_irq_enabled_with_callback(3, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &gpio_callback); gpio_set_irq_enabled_with_callback(3, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &gpio_callback);
gpio_set_irq_enabled(5, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true); gpio_set_irq_enabled(2, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true);
_gpio_init(5); _gpio_init(2);
gpio_set_input_enabled(5, true); gpio_set_input_enabled(2, true);
gpio_pull_up(5); gpio_pull_up(2);
_gpio_init(3); _gpio_init(3);
gpio_set_input_enabled(3, true); gpio_set_input_enabled(3, true);
gpio_pull_up(3); gpio_pull_up(3);
Serial.begin(9600); Serial.begin(9600);
} }
unsigned long flag;
byte counter = 0;
void loop() void loop()
{ {
// int width = myHallButton[0].percent; // int width = myHallButton[0].percent;
// Serial.println(String(width)); // Serial.println(String(width));
// if (Serial.available() > 0) // if (Serial.available() > 0)
// parseSerial(); // parseSerial();
// uint64_t timer = millis(); uint64_t timer = millis();
// if (timer > flag + 100)
// {
// flag = timer;
for (size_t i = 0; i < 2; i++) for (size_t i = 0; i < 2; i++)
{ {
Serial.print(String(myHallButton[i].percent) + "\t" + String(myHallButton[i].lowTime) + "\t" + String(myHallButton[i].highTime) + "\t"); byte percent = myHallButton[i].getPercent();
if (timer > flag + 1)
{
if (i == 1)
flag = timer;
// for (size_t j = 0; j < sizeof(myHallButton->percent); j++)
// {
// Serial.print(String(myHallButton[i].percent[j])+":");
// }
counter++;
Serial.print(String(percent) + "\t");
}
}
if (counter > 0)
{
Serial.print("\n");
counter = 0;
} }
Serial.print("\n");
// }
} }