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