Skip to content

Commit b3d7bfe

Browse files
committed
feat: add support for Latch Relay
1 parent 84a7dfb commit b3d7bfe

File tree

4 files changed

+1202
-1104
lines changed

4 files changed

+1202
-1104
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Modulino Latch Relay - Basic
3+
*
4+
* This example code is in the public domain.
5+
* Copyright (c) 2025 Arduino
6+
* SPDX-License-Identifier: MPL-2.0
7+
*/
8+
9+
#include <Modulino.h>
10+
11+
ModulinoLatchRelay relay;
12+
13+
void setup() {
14+
// put your setup code here, to run once:
15+
Modulino.begin();
16+
Serial.begin(115200);
17+
relay.begin();
18+
}
19+
20+
void loop() {
21+
// put your main code here, to run repeatedly:
22+
if (Serial.available()) {
23+
char c = Serial.read();
24+
switch (c) {
25+
case 's':
26+
relay.set();
27+
break;
28+
case 'r':
29+
relay.reset();
30+
break;
31+
case 'x':
32+
auto status = relay.getStatus();
33+
if (status == 0) {
34+
Serial.println("Relay OFF");
35+
}
36+
if (status == 1) {
37+
Serial.println("Relay ON");
38+
}
39+
if (status < 0) {
40+
Serial.println("Relay status unknown");
41+
}
42+
break;
43+
}
44+
}
45+
}

examples/Utilities/FirmwareUpdater/FirmwareUpdater.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ final_ack:
294294
int sendReset() {
295295
uint8_t buf[3] = { 'D', 'I', 'E' };
296296
int ret;
297-
for (int i = 8; i < 0x78; i++) {
297+
for (int i = 0; i < 0x78; i++) {
298298
modulino.getWire()->beginTransmission(i);
299299
ret = modulino.getWire()->endTransmission();
300300
if (ret != 2) {

src/Modulino.h

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,10 @@ class ModulinoDistance : public Module {
639639
_distance_api* api = nullptr;
640640
};
641641

642-
class ModulinoRelay : public Module {
642+
class ModulinoOptoRelay : public Module {
643643
public:
644-
ModulinoRelay(uint8_t address = 0xFF)
645-
: Module(address, "RELAY") {}
644+
ModulinoOptoRelay(uint8_t address = 0xFF)
645+
: Module(address, "OPTO_RELAY") {}
646646
bool update() {
647647
uint8_t buf[3];
648648
auto res = read((uint8_t*)buf, 3);
@@ -686,4 +686,57 @@ class ModulinoRelay : public Module {
686686
uint8_t match[1] = { 0x28 }; // same as fw main.c
687687
};
688688

689+
class ModulinoLatchRelay : public Module {
690+
public:
691+
ModulinoLatchRelay(uint8_t address = 0xFF)
692+
: Module(address, "REL") {}
693+
bool update() {
694+
uint8_t buf[3];
695+
auto res = read((uint8_t*)buf, 3);
696+
auto ret = res && (buf[0] != last_status[0] || buf[1] != last_status[1] || buf[2] != last_status[2]);
697+
last_status[0] = buf[0];
698+
last_status[1] = buf[1];
699+
last_status[2] = buf[2];
700+
return ret;
701+
}
702+
void set() {
703+
uint8_t buf[3];
704+
buf[0] = 1;
705+
buf[1] = 0;
706+
buf[2] = 0;
707+
write((uint8_t*)buf, 3);
708+
return;
709+
}
710+
void reset() {
711+
uint8_t buf[3];
712+
buf[0] = 0;
713+
buf[1] = 0;
714+
buf[2] = 0;
715+
write((uint8_t*)buf, 3);
716+
return;
717+
}
718+
int getStatus() {
719+
update();
720+
if (last_status[0] == 0 && last_status[1] == 0) {
721+
return -1; // unknown, last status before poweroff is maintained
722+
} else if (last_status[0] == 1) {
723+
return 0; // off
724+
} else {
725+
return 1; // on
726+
}
727+
}
728+
virtual uint8_t discover() {
729+
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
730+
if (scan(match[i])) {
731+
return match[i];
732+
}
733+
}
734+
return 0xFF;
735+
}
736+
private:
737+
bool last_status[3];
738+
protected:
739+
uint8_t match[1] = { 0x04 }; // same as fw main.c
740+
};
741+
689742
#endif

0 commit comments

Comments
 (0)