Skip to content

Commit

Permalink
Support modulino joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Jan 9, 2025
1 parent b7a2d49 commit ddb5f44
Show file tree
Hide file tree
Showing 4 changed files with 2,285 additions and 875 deletions.
2 changes: 2 additions & 0 deletions examples/Utilities/AddressChanger/AddressChanger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ String pinstrapToName(uint8_t pinstrap) {
switch (pinstrap) {
case 0x3C:
return "BUZZER";
case 0x58:
return "JOYSTICK";
case 0x7C:
return "BUTTONS";
case 0x76:
Expand Down
6 changes: 6 additions & 0 deletions examples/Utilities/Modulino_PlugNPlay/Modulino_PlugNPlay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ModulinoKnob encoder;
ModulinoDistance distance;
ModulinoMovement imu;
ModulinoThermo thermo;
ModulinoJoystick joystick;

void setup() {

Expand All @@ -22,6 +23,7 @@ void setup() {

imu.begin();
thermo.begin();
joystick.begin();
}

int skip = 0;
Expand Down Expand Up @@ -55,6 +57,10 @@ void loop() {
Serial.println("\tTemperature: " + String(thermo.getTemperature()));
}

if (joystick.update()) {
Serial.println("x: " + String(joystick.getX()) + " // y: " + String(joystick.getY()) + " // pressed: " + String(joystick.isPressed()));
}

if (buttons.update()) {
if (buttons.isPressed(0)) {
leds.set(1 + skip, RED, 100);
Expand Down
37 changes: 37 additions & 0 deletions src/Modulino.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2024 Arduino SA
// SPDX-License-Identifier: MPL-2.0

#include "Arduino.h"
#include "Wire.h"
#include <vector>
#include <vl53l4cd_class.h> // from stm32duino
Expand Down Expand Up @@ -155,6 +156,42 @@ class ModulinoButtons : public Module {
std::vector<uint8_t> match = { 0x7C }; // same as fw main.c
};

class ModulinoJoystick : public Module {
public:
ModulinoJoystick(uint8_t address = 0xFF)
: Module(address, "JOYSTICK") {}
bool update() {
uint8_t buf[3];
auto res = read((uint8_t*)buf, 3);
auto ret = res && (buf[0] != last_status[0] || buf[1] != last_status[1] || buf[2] != last_status[2]);
last_status[0] = buf[0];
last_status[1] = buf[1];
last_status[2] = buf[2];
return ret;
}
PinStatus isPressed() {
return last_status[2] ? HIGH : LOW;
}
int8_t getX() {
return (last_status[0] < 128 ? (128 - last_status[0]) : -(last_status[0] - 128));
}
int8_t getY() {
return (last_status[1] < 128 ? (128 - last_status[1]) : -(last_status[1] - 128));
}
virtual uint8_t discover() {
for (int i = 0; i < match.size(); i++) {
if (scan(match[i])) {
return match[i];
}
}
return 0xFF;
}
private:
uint8_t last_status[3];
protected:
std::vector<uint8_t> match = { 0x58 }; // same as fw main.c
};

class ModulinoBuzzer : public Module {
public:
ModulinoBuzzer(uint8_t address = 0xFF)
Expand Down
Loading

0 comments on commit ddb5f44

Please sign in to comment.