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