First working version of firmware for keypad
This commit is contained in:
parent
6cc203eef2
commit
b5cee6a623
|
@ -8,8 +8,9 @@
|
|||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:pico]
|
||||
[env:adafruit_kb2040]
|
||||
platform = raspberrypi
|
||||
board = pico
|
||||
board = adafruit_kb2040
|
||||
framework = arduino
|
||||
upload_protocol = picotool
|
||||
monitor_speed = 115200
|
148
src/main.cpp
148
src/main.cpp
|
@ -1,100 +1,108 @@
|
|||
#include <Arduino.h>
|
||||
#define PORT PWM_2
|
||||
class HallButton // TODO
|
||||
#include <pico.h>
|
||||
#include <hardware/pwm.h>
|
||||
#include <Keyboard.h>
|
||||
#define PORT 2
|
||||
#define PORT2 3
|
||||
class HallButton
|
||||
{
|
||||
public:
|
||||
byte pin;
|
||||
HallButton(byte pin)
|
||||
uint64_t firstPulseStart;
|
||||
uint64_t firstPulseLength;
|
||||
bool isDataReadyToRead;
|
||||
|
||||
HallButton(uint8_t gpioNum, char buttonOnKeyboard)
|
||||
{
|
||||
this->pin = pin;
|
||||
};
|
||||
void Pulse(unsigned long time, bool high = false)
|
||||
this->gpioNum = gpioNum;
|
||||
this->buttonOnKeyboard = buttonOnKeyboard;
|
||||
}
|
||||
|
||||
void HandleISR(bool gpioState, uint64_t currentTime)
|
||||
{
|
||||
if (high)
|
||||
if (gpioState)
|
||||
{
|
||||
if(++count >= 8) count =0;
|
||||
highTime[count] = time - lastRead;
|
||||
firstPulseLength = currentTime - firstPulseStart;
|
||||
isDataReadyToRead = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
lowTime[count] = time - lastRead;
|
||||
firstPulseStart = currentTime;
|
||||
}
|
||||
lastRead = time;
|
||||
}
|
||||
byte count = 0;
|
||||
unsigned long lastRead;
|
||||
unsigned long lowTime[8];
|
||||
unsigned long highTime[8];
|
||||
byte getPercent()
|
||||
void HandleButton()
|
||||
{
|
||||
uint sumLow = 0;
|
||||
uint sumHigh = 0;
|
||||
for (size_t i = 0; i < sizeof(lowTime); i++)
|
||||
if (!buttonPressed && firstPulseLength > 310)
|
||||
{
|
||||
sumLow += lowTime[i];
|
||||
sumHigh += highTime[i];
|
||||
Keyboard.press(buttonOnKeyboard);
|
||||
buttonPressed = true;
|
||||
}
|
||||
else if (buttonPressed && firstPulseLength < 300)
|
||||
{
|
||||
Keyboard.release(buttonOnKeyboard);
|
||||
buttonPressed = false;
|
||||
}
|
||||
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 percent[8];
|
||||
};
|
||||
|
||||
HallButton myHallButton[2] = {HallButton(3), HallButton(2)};
|
||||
void gpio_callback(uint gpio, uint32_t events)
|
||||
private:
|
||||
bool buttonPressed;
|
||||
uint8_t gpioNum;
|
||||
char buttonOnKeyboard;
|
||||
};
|
||||
HallButton btn[2] = {HallButton(PORT, 't'), HallButton(PORT2, 'y')};
|
||||
void isr(uint gpio, uint32_t events)
|
||||
{
|
||||
byte hallButtonIndex = gpio == 3 ? 0 : 1;
|
||||
// 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(myHallButton[secondHallButtonIndex].pin) != 0);
|
||||
uint64_t currentTime = time_us_64();
|
||||
bool state = events == 8;
|
||||
switch (gpio)
|
||||
{
|
||||
case PORT:
|
||||
btn[0].HandleISR(state, currentTime);
|
||||
break;
|
||||
case PORT2:
|
||||
btn[1].HandleISR(state, currentTime);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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(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);
|
||||
Keyboard.begin();
|
||||
Serial.begin(115200);
|
||||
gpio_init(PORT);
|
||||
gpio_set_dir(PORT, false);
|
||||
gpio_pull_up(PORT);
|
||||
gpio_set_irq_enabled_with_callback(PORT, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, isr);
|
||||
gpio_init(PORT2);
|
||||
gpio_set_dir(PORT2, false);
|
||||
gpio_pull_up(PORT2);
|
||||
gpio_set_irq_enabled_with_callback(PORT2, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, isr);
|
||||
}
|
||||
unsigned long flag;
|
||||
#define printf Serial.printf
|
||||
byte counter = 0;
|
||||
void loop()
|
||||
void printDebugInfo()
|
||||
{
|
||||
// int width = myHallButton[0].percent;
|
||||
// Serial.println(String(width));
|
||||
// if (Serial.available() > 0)
|
||||
// parseSerial();
|
||||
uint64_t timer = millis();
|
||||
for (size_t i = 0; i < 2; i++)
|
||||
if (counter < 1 && btn[0].isDataReadyToRead)
|
||||
{
|
||||
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");
|
||||
}
|
||||
btn[0].isDataReadyToRead = false;
|
||||
printf("BTN1: %lu", btn[0].firstPulseLength);
|
||||
counter++;
|
||||
}
|
||||
if (counter > 0)
|
||||
if (btn[1].isDataReadyToRead)
|
||||
{
|
||||
Serial.print("\n");
|
||||
btn[1].isDataReadyToRead = false;
|
||||
if (counter > 0)
|
||||
printf("\t");
|
||||
printf("BTN2: %lu", btn[1].firstPulseLength);
|
||||
counter++;
|
||||
}
|
||||
if (counter > 1)
|
||||
{
|
||||
printf("\n");
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
void loop()
|
||||
{
|
||||
btn[0].HandleButton();
|
||||
btn[1].HandleButton();
|
||||
}
|
Loading…
Reference in New Issue
Block a user