arduino stuffs
rgb light thing and other changes
| -rw-r--r-- | .gitignore | 1 | ||||
| m--------- | car_driver/godot/godot-cpp | 0 | ||||
| -rw-r--r-- | rgled/rgled.ino | 74 | ||||
| -rw-r--r-- | rgled/rot.cpp | 38 | ||||
| -rw-r--r-- | rgled/rot.h | 10 | ||||
| -rw-r--r-- | steer/buttons.cpp | 69 | ||||
| -rw-r--r-- | steer/buttons.h | 6 |
7 files changed, 169 insertions, 29 deletions
@@ -4,3 +4,4 @@ car_driver/godot/car_driver/.import/ *.os *.dblite tmp/ +tests/ diff --git a/car_driver/godot/godot-cpp b/car_driver/godot/godot-cpp -Subproject 97c181a2461e590affab4c32638ca01928999db +Subproject 7c09b5484de21c5be02ffee4fe32e27e9f4b7e1 diff --git a/rgled/rgled.ino b/rgled/rgled.ino new file mode 100644 index 0000000..555414a --- /dev/null +++ b/rgled/rgled.ino @@ -0,0 +1,74 @@ +#include "rot.h" +const uint8_t RED = 5; +const uint8_t BLUE = 6; +const uint8_t GREEN = 9; + +void setup() { Rotary::setup(50); } + +const uint8_t min_delay = 1; +void loop() { + static uint8_t R, G, B; + static uint8_t color = 0; + hue_to_rgb(color, 255, R, G, B); + analogWrite(RED, R); + analogWrite(GREEN, G); + analogWrite(BLUE, B); + color++; + // color %= 256; it wraps on its own! + int result = Rotary::read(); + if (!Rotary::button_just_pressed()) { + if (result > min_delay) { + delay(result); + return; + } + } else { + delay(50); + Rotary::set(50); + return; + } + if (result != min_delay) + Rotary::set(min_delay); + delay(min_delay); +} + +void hue_to_rgb(uint8_t hue, uint8_t brightness, uint8_t &R, uint8_t &B, uint8_t &G) { + uint16_t scaled = hue * 6; + uint8_t segment = scaled / 256; + uint8_t offset = scaled - segment * 256; + uint8_t complement = 0; + uint8_t prev = (brightness * (255 - offset)) / 256; + uint8_t next = (brightness * offset) / 256; + switch (segment) { + case 0: + R = brightness; + G = next; + B = complement; + break; + case 1: + R = prev; + G = brightness; + B = complement; + break; + case 2: + R = complement; + G = brightness; + B = next; + break; + case 3: + R = complement; + G = prev; + B = brightness; + break; + case 4: + R = next; + G = complement; + B = brightness; + break; + case 5: + default: + R = brightness; + G = complement; + B = prev; + break; + } +}
\ No newline at end of file diff --git a/rgled/rot.cpp b/rgled/rot.cpp new file mode 100644 index 0000000..4724d0a --- /dev/null +++ b/rgled/rot.cpp @@ -0,0 +1,38 @@ +#define ENCODER_OPTIMIZE_INTERRUPTS +#include "rot.h" +#include "Encoder.h" + +namespace Rotary { + +const uint8_t CLOCK = 2; +const uint8_t DATA = 3; + +const uint8_t SWITCH = 4; + +namespace { +Encoder encoder(CLOCK, DATA); +} + +void setup(const int start = 0) { + pinMode(SWITCH, INPUT_PULLUP); + set(start); +} + +/// @brief check if button just pressed +bool button_just_pressed() { + static uint8_t last_button_state = LOW; + uint8_t button_state = digitalRead(SWITCH); + if (button_state != last_button_state) { + last_button_state = button_state; + if (button_state == LOW) + return true; + } + return false; +} + +/// @brief resets the reading to 0 and returns the reading before reset +void set(const int n = 0) { encoder.write(n); } +/// @brief reads the encoder +/// @return encoder position +int read() { return encoder.read(); } +} // namespace Rotary
\ No newline at end of file diff --git a/rgled/rot.h b/rgled/rot.h new file mode 100644 index 0000000..d1ae6bf --- /dev/null +++ b/rgled/rot.h @@ -0,0 +1,10 @@ +#ifndef ROT_H +#define ROT_H +namespace Rotary { +void setup(const int start = 0); +bool button_just_pressed(); + +void set(const int n = 0); +int read(); +} // namespace Rotary +#endif
\ No newline at end of file diff --git a/steer/buttons.cpp b/steer/buttons.cpp index 8372941..17c9651 100644 --- a/steer/buttons.cpp +++ b/steer/buttons.cpp @@ -1,46 +1,44 @@ #include "buttons.h" #include "Joystick.h" -// #include "Streaming.h" +#include "Streaming.h" -namespace Controller { -/// vars -unsigned int last_x_value = 1023 / 2; -unsigned int last_accel_value = 0; -unsigned int last_brake_value = LOW; // still digital +#define INVERT == HIGH ? LOW : HIGH +#define TEXT == HIGH ? "high" : "low" +namespace Controller { /// pins #define STEER_X A0 -#define ACCEL A1 +#define ACCEL 10 +#define ANY 7 // used for A in gamepad mode, and x in button mode (a accel)```` #define BRAKE 8 #ifdef GAMEPAD_USE_BUTTONS -#define PAD_A 0 #define PAD_B 1 +#define PAD_X 2 #endif #ifdef MODE_STEERING_WHEEL -Joystick_ Gamepad(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_MULTI_AXIS, 0, 0, false, false, false, false, false, - false, false, false, true, true, true); +Joystick_ Gamepad(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_MULTI_AXIS, 16, 1, false, false, true, false, false, + true, false, false, true, true, true); #elif defined(MODE_GAMEPAD) -#ifdef GAMEPAD_USE_TRIGGERS -Joystick_ Gamepad(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 0, 0, true, true, true, false, false, true, - false, false, false, false, false); -#elif defined(GAMEPAD_USE_BUTTONS) -Joystick_ Gamepad(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 2, 0, true, true, false, false, false, false, +#define PAD_A 0 +Joystick_ Gamepad(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 10, 0, true, true, true, true, true, true, false, false, false, false, false); #endif -#endif void begin() { pinMode(STEER_X, INPUT); - pinMode(ACCEL, INPUT); + pinMode(ACCEL, INPUT_PULLUP); pinMode(BRAKE, INPUT_PULLUP); + pinMode(ANY, INPUT_PULLUP); Gamepad.begin(); #ifdef MODE_STEERING_WHEEL Gamepad.setBrakeRange(0, 1); - Gamepad.setSteering(last_x_value); + Gamepad.setSteering(1023 / 2); + Gamepad.setSteeringRange(0, 1); #elif defined(MODE_GAMEPAD) - Gamepad.setXAxis(last_x_value); + Gamepad.setRzAxisRange(0, 1); + Gamepad.setXAxis(1023 / 2); Gamepad.setYAxis(1023 / 2); #ifdef GAMEPAD_USE_TRIGGERS Gamepad.setZAxisRange(0, 1); @@ -48,15 +46,29 @@ void begin() { #endif } -inline bool boolify(unsigned int analog) { return analog < (1023 / 2) ? 0 : 1; } +void any() { + static uint8_t last_any = LOW; + uint8_t new_any = digitalRead(ANY) INVERT; + if (new_any != last_any) { + Serial << "ANY set to " << (new_any TEXT) << endl; + last_any = new_any; +#ifdef GAMEPAD_USE_BUTTONS + Gamepad.setButton(PAD_X, new_any); +#else + Gamepad.setButton(PAD_A, new_any); +#endif + } +} void accel() { - unsigned int new_accel = analogRead(ACCEL); + static uint8_t last_accel_value = LOW; // my pot broke so were back to digital + uint8_t new_accel = digitalRead(ACCEL) INVERT; if (new_accel != last_accel_value) { + Serial << "ACCEL set to " << (new_accel TEXT) << endl; last_accel_value = new_accel; #ifdef MODE_GAMEPAD #ifdef GAMEPAD_USE_BUTTONS - Gamepad.setButton(PAD_A, boolify(new_accel)); + Gamepad.setButton(PAD_A, new_accel); #elif defined(GAMEPAD_USE_TRIGGERS) Gamepad.setRzAxis(new_accel); #endif @@ -68,8 +80,10 @@ void accel() { /// @brief still a button void brake() { - unsigned int new_brake = digitalRead(BRAKE) == HIGH ? LOW : HIGH; + static uint8_t last_brake_value = LOW; // still digital + uint8_t new_brake = digitalRead(BRAKE) INVERT; if (new_brake != last_brake_value) { + Serial << "BRAKE set to " << (new_brake TEXT) << endl; last_brake_value = new_brake; #ifdef MODE_GAMEPAD #ifdef GAMEPAD_USE_BUTTONS @@ -84,8 +98,10 @@ void brake() { } void x() { - unsigned int new_x = analogRead(STEER_X); + static uint16_t last_x_value = 1023 / 2; + uint16_t new_x = analogRead(STEER_X); if (new_x != last_x_value) { + // Serial << "X set to " << new_x << endl; last_x_value = new_x; #ifdef MODE_GAMEPAD Gamepad.setXAxis(new_x); @@ -96,9 +112,10 @@ void x() { } void send() { + x(); + any(); accel(); brake(); - x(); } -} // namespace Controller
\ No newline at end of file +} // namespace Controller diff --git a/steer/buttons.h b/steer/buttons.h index 2b0f12d..dcef935 100644 --- a/steer/buttons.h +++ b/steer/buttons.h @@ -4,8 +4,8 @@ namespace Controller { // #define MODE_STEERING_WHEEL #define MODE_GAMEPAD -// #define GAMEPAD_USE_TRIGGERS -#define GAMEPAD_USE_BUTTONS +#define GAMEPAD_USE_TRIGGERS +// #define GAMEPAD_USE_BUTTONS /// @brief sets up the pins void begin(); @@ -13,4 +13,4 @@ void begin(); /// @brief reads the pins, and sends it to the computer void send(); }; // namespace Controller -#endif
\ No newline at end of file +#endif |