diff --git a/IO4/platformio.ini b/IO4/platformio.ini index ed4078e..c58b365 100644 --- a/IO4/platformio.ini +++ b/IO4/platformio.ini @@ -12,3 +12,6 @@ platform = espressif32 board = lolin_s2_mini framework = arduino +build_flags = + -D ARDUINO_USB_MODE=0 + -D ARDUINO_USB_CDC_ON_BOOT=1 \ No newline at end of file diff --git a/IO4/src/main.cpp b/IO4/src/main.cpp index 77a0e54..14405ff 100644 --- a/IO4/src/main.cpp +++ b/IO4/src/main.cpp @@ -1,8 +1,41 @@ #include -void setup() { -// put your setup code here, to run once: +#include +#include "sweets.h" + +#define KEY_COUNT 5 + +USBHIDKeyboard keyboard; +constexpr u8 keys[] = {'1', '2', '3', 'a', 'b'}; +constexpr u8 pins[] = {GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7}; +bool keyStates[KEY_COUNT] = {false}; + +val led_pin = GPIO_NUM_15; + +void setup() +{ + // Set up the GPIO pins + for (val pin : pins) pinMode(pin, INPUT_PULLUP); + pinMode(led_pin, OUTPUT); + + // Set up the USB keyboard + keyboard.begin(); + Serial.begin(); } -void loop() { -// write your code here -} \ No newline at end of file +void loop() +{ + // Check for key presses + for (var i = 0; i < KEY_COUNT; i++) + { + val state = !digitalRead(pins[i]); + + if (state != keyStates[i]) + { + if (state) keyboard.press(keys[i]); + else keyboard.release(keys[i]); + + digitalWrite(led_pin, state); + keyStates[i] = state; + } + } +} diff --git a/IO4/src/sweets.h b/IO4/src/sweets.h new file mode 100644 index 0000000..9e42c91 --- /dev/null +++ b/IO4/src/sweets.h @@ -0,0 +1,15 @@ +// +// Created by Azalea on 12/12/23. +// + +#ifndef SWEETS_H +#define SWEETS_H + +#define u8 uint8_t +#define u16 uint16_t +#define u32 uint32_t + +#define val const auto +#define var auto + +#endif //SWEETS_H