Skip to content

fix for knob press #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: joystick
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/Modulino_Joystick/Basic/Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Modulino.h"

ModulinoJoystick joystick;

void setup() {
Serial.begin(9600);
Modulino.begin();
joystick.begin();
}

void loop() {
joystick.update();

if(joystick.isPressed()) {
Serial.println("Pressed");
}

Serial.print("x,y: ");
Serial.print(joystick.getX());
Serial.print(", ");
Serial.println(joystick.getY());
}
25 changes: 25 additions & 0 deletions examples/Modulino_Relay/Basic/Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <Modulino.h>

ModulinoRelay relay;

void setup() {
Serial.begin(115200);
Modulino.begin();
relay.begin();

}
void loop() {
relay.on();
if(relay.getStatus())
{
Serial.println("Relay state ON!");
}
delay(1000);
relay.off();
if(!(relay.getStatus()))
{
Serial.println("Relay state OFF!");
}
Serial.println();
delay(1000);
}
15 changes: 15 additions & 0 deletions examples/Modulino_Vibro/Basic/Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "Modulino.h"

ModulinoVibro vibro;

void setup() {
Modulino.begin();
vibro.begin();
}

void loop() {
vibro.on(1000);
delay(1000);
vibro.off();
delay(1000);
}
6 changes: 6 additions & 0 deletions examples/Utilities/AddressChanger/AddressChanger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ String pinstrapToName(uint8_t pinstrap) {
switch (pinstrap) {
case 0x3C:
return "BUZZER";
case 0x58:
return "JOYSTICK";
case 0x7C:
return "BUTTONS";
case 0x28:
return "RELAY";
case 0x76:
case 0x74:
return "ENCODER";
case 0x6C:
return "SMARTLEDS";
case 0x70:
return "VIBRO";
}
return "UNKNOWN";
}
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
137 changes: 133 additions & 4 deletions src/Modulino.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Copyright (c) 2024 Arduino SA
// SPDX-License-Identifier: MPL-2.0

#include "Arduino.h"
#include "Wire.h"
#include <vl53l4cd_class.h> // from stm32duino
#include <vl53l4ed_class.h> // from stm32duino
Expand Down Expand Up @@ -158,6 +159,42 @@ class ModulinoButtons : public Module {
uint8_t match[1] = { 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 (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
if (scan(match[i])) {
return match[i];
}
}
return 0xFF;
}
private:
uint8_t last_status[3];
protected:
uint8_t match[1] ={ 0x58 }; // same as fw main.c
};

class ModulinoBuzzer : public Module {
public:
ModulinoBuzzer(uint8_t address = 0xFF)
Expand Down Expand Up @@ -185,6 +222,34 @@ class ModulinoBuzzer : public Module {
uint8_t match[1] = { 0x3C }; // same as fw main.c
};

class ModulinoVibro : public Module {
public:
ModulinoVibro(uint8_t address = 0xFF)
: Module(address, "VIBRO") {}
void on(size_t len_ms) {
uint8_t buf[8];
uint32_t freq = 100;
memcpy(&buf[0], &freq, 4);
memcpy(&buf[4], &len_ms, 4);
write(buf, 8);
}
void off() {
uint8_t buf[8];
memset(&buf[0], 0, 8);
write(buf, 8);
}
virtual uint8_t discover() {
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
if (scan(match[i])) {
return match[i];
}
}
return 0xFF;
}
protected:
uint8_t match[1] = { 0x70 }; // same as fw main.c
};

class ModulinoColor {
public:
ModulinoColor(uint8_t r, uint8_t g, uint8_t b)
Expand Down Expand Up @@ -255,15 +320,30 @@ class ModulinoKnob : public Module {
}
return ret;
}
int16_t get() {
bool update() {
uint8_t buf[3];
auto res = read(buf, 3);
if (res == false) {
return 0;
}
get(buf);
return 1;
}
int16_t get(uint8_t * buf = nullptr) {
if (buf == nullptr) {
buf = (uint8_t*)malloc(3);
if (buf == nullptr) {
return 0;
}
auto res = read(buf, 3);
if (res == false) {
_pressed = false;
return 0;
}
}
_pressed = (buf[2] != 0);
int16_t ret = buf[0] | (buf[1] << 8);
return ret;
int16_t _last_pox = buf[0] | (buf[1] << 8);
return _last_pox;
}
void set(int16_t value) {
if (_bug_on_set) {
Expand All @@ -286,8 +366,10 @@ class ModulinoKnob : public Module {
return 0xFF;
}
private:
int16_t _last_pox = 0;
bool _pressed = false;
bool _bug_on_set = false;

protected:
uint8_t match[2] = { 0x74, 0x76 };
};
Expand Down Expand Up @@ -516,4 +598,51 @@ class ModulinoDistance : public Module {
_distance_api* api = nullptr;
};

#endif
class ModulinoRelay : public Module {
public:
ModulinoRelay(uint8_t address = 0xFF)
: Module(address, "RELAY") {}
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;
}
void on() {
uint8_t buf[3];
buf[0] = 1;
buf[1] = 0;
buf[2] = 0;
write((uint8_t*)buf, 3);
return;
}
void off() {
uint8_t buf[3];
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
write((uint8_t*)buf, 3);
return;
}
bool getStatus() {
update();
return last_status[0];
}
virtual uint8_t discover() {
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
if (scan(match[i])) {
return match[i];
}
}
return 0xFF;
}
private:
bool last_status[3];
protected:
uint8_t match[1] = { 0x28 }; // same as fw main.c
};

#endif // ARDUINO_LIBRARIES_MODULINO_H
Loading