mirror of
https://git.j0bot.ch/admin_j0bot/xerbot-arduino.git
synced 2026-11-29 07:55:08 +00:00
xerbot 0.2 : les broches pilotées depuis le labo (xb.broches, pin.mode / pin.ecrire / pins → state.pins, entrées relues 250 ms) ; exemple 07 ; test 12/12
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R75H65SynGwRG5vBSoYeZ1
This commit is contained in:
parent
a361215dcf
commit
d63a4bd79e
12
README.md
12
README.md
@ -43,6 +43,15 @@ void loop() { xb.loop(); /* toutes les 2 s : */ xb.telemetrie().add("t", 21.5).
|
||||
|
||||
`xb.commandes("led,seuil")` déclare ses sorties (le labo les montre en boutons, contribuer › ⬢ xeroction) ; `c.texte("mode", buf, n)`, `c.nombre("w", 0)`, `c.booleen("on")` lisent les arguments ; `ping` et `hello` sont gérés tout seuls ; l'état part toutes les 10 s (`xb.battement(ms)`).
|
||||
|
||||
## Les broches, pilotées depuis le labo
|
||||
|
||||
```cpp
|
||||
static const uint8_t BROCHES[] = {2, 4, 5, 34}; // SEULES celles-ci (jamais flash / USB / boot)
|
||||
xb.broches(BROCHES, sizeof BROCHES);
|
||||
```
|
||||
|
||||
Le labo affiche alors un **panneau des broches** (contribuer › ⬢ xeroction › tes devices) : pour chacune, le mode (`off` `in` `pullup` `out` `pwm` `adc`), la valeur en direct, un interrupteur (out) ou un curseur (pwm). Commandes : `pin.mode {p, m}`, `pin.ecrire {p, v}`, `pins`. Les entrées sont relues toutes les 250 ms ; un changement part aussitôt (`state.pins`). Exemple 07. La configuration n'est pas encore gardée au redémarrage.
|
||||
|
||||
## Par carte
|
||||
|
||||
- **ESP32 / S3 / C3 / C6** — USB, ESP-NOW (`#include <XerbotEspNow.h>`, `xerbotEspNowNoeud(xb)`), Bluetooth (`#include <XerbotBLE.h>`, `xerbotBLE(xb, "nom")`), RuView (exemple 05). PlatformIO : `platform = espressif32`. Arduino IDE : gestionnaire de cartes « esp32 » (Espressif).
|
||||
@ -62,9 +71,10 @@ void loop() { xb.loop(); /* toutes les 2 s : */ xb.telemetrie().add("t", 21.5).
|
||||
| `04-ble-capteur` | un ESP32 en Bluetooth (et USB) |
|
||||
| `05-ruview-csi` | RuView : la présence par le Wi-Fi (CSI), pour le ploxion ruview |
|
||||
| `06-uno-minimal` | le plus petit : un Uno |
|
||||
| `07-broches` | chaque broche reconfigurable depuis le labo |
|
||||
|
||||
## Tester sans carte
|
||||
|
||||
`g++ -std=c++17 -I test -o t test/test.cpp src/Xerbot.cpp && ./t` → `xerbot : 8/8`. ESP-NOW et Bluetooth (headers ESP) : pas encore compilés sur une vraie chaîne ESP32 — le premier test réel les valide.
|
||||
`g++ -std=c++17 -I test -o t test/test.cpp src/Xerbot.cpp && ./t` → `xerbot : 12/12`. ESP-NOW et Bluetooth (headers ESP) : pas encore compilés sur une vraie chaîne ESP32 — le premier test réel les valide.
|
||||
|
||||
© 2026 chill-tekk · MIT · ne pas nuire
|
||||
|
||||
8
examples/07-broches/07-broches.ino
Normal file
8
examples/07-broches/07-broches.ino
Normal file
@ -0,0 +1,8 @@
|
||||
// 07-broches : chaque broche se reconfigure DEPUIS LE LABO (contribuer › ⬢ xeroction, ou xerbot) — entrée, pull-up, sortie, PWM, ADC.
|
||||
// Seules les broches listées ici sont pilotables (ne pas nuire : jamais une broche de flash, d'USB ou de boot de ta carte).
|
||||
#include <Xerbot.h>
|
||||
Xerbot xb("banc-1", "banc");
|
||||
// ESP32 DevKit : des broches sans risque. Uno : {2, 3, 4, 5, 6, A0}.
|
||||
static const uint8_t BROCHES[] = {2, 4, 5, 18, 19, 21, 22, 23, 25, 26, 32, 33, 34, 35};
|
||||
void setup() { Serial.begin(115200); xb.broches(BROCHES, sizeof BROCHES); xb.begin(Serial); }
|
||||
void loop() { xb.loop(); }
|
||||
@ -66,7 +66,57 @@ void Xerbot::commandes(const char* csv) {
|
||||
if (!deb) _cmds[n++] = '"';
|
||||
_cmds[n++] = ']'; _cmds[n] = 0;
|
||||
}
|
||||
XerbotMsg Xerbot::etatComplet() { XerbotMsg m(this, "state"); m.add("online", true); if (_cmds[0]) m.brut("cmds", _cmds); return m; }
|
||||
XerbotMsg Xerbot::etatComplet() { XerbotMsg m(this, "state"); m.add("online", true); if (_cmds[0]) m.brut("cmds", _cmds); if (_np) m.add("pins_n", (int)_np); return m; }
|
||||
|
||||
// ---------------------------------------------------------------- les broches (04.11)
|
||||
static const char* MODES[] = {"off", "in", "pullup", "out", "pwm", "adc"};
|
||||
enum { M_OFF, M_IN, M_PULLUP, M_OUT, M_PWM, M_ADC };
|
||||
void Xerbot::broches(const uint8_t* liste, uint8_t n) {
|
||||
_np = n > XB_PINS ? XB_PINS : n;
|
||||
for (uint8_t i = 0; i < _np; i++) { _p[i] = liste[i]; _m[i] = M_OFF; _v[i] = 0; }
|
||||
}
|
||||
void Xerbot::envoyerPins() {
|
||||
// {"pins":[{"p":2,"m":"out","v":1},…]}
|
||||
char b[XERBOT_LIGNE - 48]; size_t n = 0; b[n++] = '[';
|
||||
for (uint8_t i = 0; i < _np && n + 32 < sizeof(b); i++) n += snprintf(b + n, sizeof(b) - n, "%s{\"p\":%u,\"m\":\"%s\",\"v\":%d}", i ? "," : "", _p[i], MODES[_m[i]], _v[i]);
|
||||
b[n++] = ']'; b[n] = 0;
|
||||
etat().brut("pins", b).envoyer();
|
||||
}
|
||||
static int lire(uint8_t p, uint8_t m) {
|
||||
#ifdef XERBOT_SANS_MATERIEL
|
||||
(void)p; (void)m; return 0;
|
||||
#else
|
||||
return m == M_ADC ? analogRead(p) : digitalRead(p);
|
||||
#endif
|
||||
}
|
||||
bool Xerbot::pinCommande(const XerbotCommande& c) {
|
||||
bool estMode = !strcmp(c.cmd, "pin.mode"), estEcr = !strcmp(c.cmd, "pin.ecrire");
|
||||
if (!strcmp(c.cmd, "pins")) { ack(c.id, true); envoyerPins(); return true; }
|
||||
if (!estMode && !estEcr) return false;
|
||||
int p = (int)c.nombre("p", -1); int8_t i = -1;
|
||||
for (uint8_t k = 0; k < _np; k++) if (_p[k] == p) i = k;
|
||||
if (i < 0) { ack(c.id, false, "broche non autorisée (xb.broches)"); return true; }
|
||||
if (estMode) {
|
||||
char m[10] = {0}; c.texte("m", m, sizeof m); int8_t mi = -1;
|
||||
for (uint8_t k = 0; k < 6; k++) if (!strcmp(m, MODES[k])) mi = k;
|
||||
if (mi < 0) { ack(c.id, false, "mode : off in pullup out pwm adc"); return true; }
|
||||
#ifndef XERBOT_SANS_MATERIEL
|
||||
if (mi == M_IN || mi == M_ADC || mi == M_OFF) pinMode(p, INPUT);
|
||||
else if (mi == M_PULLUP) pinMode(p, INPUT_PULLUP);
|
||||
else { pinMode(p, OUTPUT); if (mi == M_OUT) digitalWrite(p, LOW); else analogWrite(p, 0); }
|
||||
#endif
|
||||
_m[i] = mi; _v[i] = (mi == M_OFF || mi == M_OUT || mi == M_PWM) ? 0 : lire(p, mi);
|
||||
} else {
|
||||
if (_m[i] != M_OUT && _m[i] != M_PWM) { ack(c.id, false, "la broche n'est pas en out ou pwm"); return true; }
|
||||
int v = (int)c.nombre("v", 0);
|
||||
if (_m[i] == M_OUT) v = v ? 1 : 0; else v = v < 0 ? 0 : v > 255 ? 255 : v;
|
||||
#ifndef XERBOT_SANS_MATERIEL
|
||||
if (_m[i] == M_OUT) digitalWrite(p, v ? HIGH : LOW); else analogWrite(p, v);
|
||||
#endif
|
||||
_v[i] = v;
|
||||
}
|
||||
ack(c.id, true); envoyerPins(); return true;
|
||||
}
|
||||
void Xerbot::loop() {
|
||||
while (_s && _s->available()) {
|
||||
char c = (char)_s->read();
|
||||
@ -74,6 +124,14 @@ void Xerbot::loop() {
|
||||
if (c == '\n') { _l[_ln] = 0; if (_ln) recevoir(_l); _ln = 0; continue; }
|
||||
if (_ln + 1 < sizeof(_l)) _l[_ln++] = c; else _ln = 0; // ligne trop longue : jetée
|
||||
}
|
||||
if (_np && millis() - _lu >= 250) {
|
||||
_lu = millis(); bool chg = false;
|
||||
for (uint8_t i = 0; i < _np; i++) if (_m[i] == M_IN || _m[i] == M_PULLUP || _m[i] == M_ADC) {
|
||||
int v = lire(_p[i], _m[i]);
|
||||
if (_m[i] == M_ADC ? (v > _v[i] + 16 || v < _v[i] - 16) : v != _v[i]) { _v[i] = v; chg = true; } // l'ADC : un seuil, sinon le bruit inonde
|
||||
}
|
||||
if (chg) envoyerPins();
|
||||
}
|
||||
if (_bat && millis() - _dernier >= _bat) { _dernier = millis(); etatComplet().add("uptime_s", (long)(millis() / 1000)).envoyer(); }
|
||||
}
|
||||
void Xerbot::recevoir(const char* ligne) {
|
||||
@ -87,6 +145,7 @@ void Xerbot::recevoir(const char* ligne) {
|
||||
c.args = jsonObjet(ligne, "args", &c.argsLen);
|
||||
if (!c.args) { c.args = "{}"; c.argsLen = 2; }
|
||||
if (strcmp(c.cmd, "ping") == 0) { ack(c.id, true); return; }
|
||||
if (pinCommande(c)) return;
|
||||
if (_cb) _cb(*this, c); else ack(c.id, false, "pas de onCommande");
|
||||
}
|
||||
|
||||
|
||||
11
src/Xerbot.h
11
src/Xerbot.h
@ -57,7 +57,12 @@ class Xerbot {
|
||||
void sortie(XerbotSortie f, void* ctx) { _sortie = f; _ctx = ctx; }
|
||||
void onCommande(void (*cb)(Xerbot&, const XerbotCommande&)) { _cb = cb; }
|
||||
void battement(unsigned long ms) { _bat = ms; } // l'état toutes les ms (10 s par défaut ; 0 = jamais)
|
||||
void commandes(const char* csv); // ses SORTIES : « led,seuil » → state.cmds (le labo les montre en boutons)
|
||||
void commandes(const char* csv);
|
||||
// LES BROCHES pilotées depuis le labo (04.11) : SEULES celles listées ici (ne pas nuire : jamais une broche de flash, d'USB, de boot).
|
||||
// static const uint8_t B[] = {2, 4, 5, 34}; xb.broches(B, 4);
|
||||
// Commandes intégrées : pin.mode {p, m: off|in|pullup|out|pwm|adc} · pin.ecrire {p, v} (out : 0/1, pwm : 0-255) · pins (→ state.pins).
|
||||
// Les entrées (in, pullup, adc) sont relues toutes les 250 ms : un changement → state.pins aussitôt.
|
||||
void broches(const uint8_t* liste, uint8_t n); // ses SORTIES : « led,seuil » → state.cmds (le labo les montre en boutons)
|
||||
void loop();
|
||||
void recevoir(const char* ligne); // une ligne reçue par n'importe quel transport
|
||||
XerbotMsg telemetrie() { return XerbotMsg(this, "telemetry"); }
|
||||
@ -78,5 +83,9 @@ class Xerbot {
|
||||
void (*_cb)(Xerbot&, const XerbotCommande&) = nullptr;
|
||||
char _l[XERBOT_LIGNE]; size_t _ln = 0; unsigned long _bat = 10000, _dernier = 0;
|
||||
char _cmds[96] = {0};
|
||||
static const uint8_t XB_PINS = 24;
|
||||
uint8_t _np = 0, _p[XB_PINS], _m[XB_PINS]; int _v[XB_PINS]; unsigned long _lu = 0;
|
||||
bool pinCommande(const XerbotCommande& c);
|
||||
void envoyerPins();
|
||||
XerbotMsg etatComplet();
|
||||
};
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
// un faux Arduino.h pour tester le cœur sur la machine (g++)
|
||||
#pragma once
|
||||
#define XERBOT_SANS_MATERIEL 1
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
@ -25,5 +25,12 @@ int main() {
|
||||
OK(s.out.find("\"id\":\"z\"") != std::string::npos, "une ligne trop longue est jetée, la suivante passe");
|
||||
Stream s2; Xerbot x2("d2", "radar"); x2.battement(0); x2.commandes("led, seuil"); x2.begin(s2);
|
||||
OK(s2.out.find("\"cmds\":[\"led\",\"seuil\"]") != std::string::npos, "commandes() → state.cmds : " + s2.out);
|
||||
Stream s3; Xerbot x3("d3"); x3.battement(0); static const uint8_t B[] = {2, 34}; x3.broches(B, 2); x3.begin(s3); s3.out.clear();
|
||||
s3.in = "{\"cmd\":\"pin.mode\",\"args\":{\"p\":2,\"m\":\"out\"},\"id\":\"a\"}\n{\"cmd\":\"pin.ecrire\",\"args\":{\"p\":2,\"v\":1},\"id\":\"b\"}\n"; x3.loop();
|
||||
OK(s3.out.find("{\"p\":2,\"m\":\"out\",\"v\":1}") != std::string::npos, "pin.mode out + pin.ecrire 1 → state.pins : " + s3.out);
|
||||
s3.out.clear(); s3.in = "{\"cmd\":\"pin.mode\",\"args\":{\"p\":6,\"m\":\"out\"},\"id\":\"c\"}\n"; x3.loop();
|
||||
OK(s3.out.find("non autoris") != std::string::npos, "une broche hors liste est refusée");
|
||||
s3.out.clear(); s3.in = "{\"cmd\":\"pin.ecrire\",\"args\":{\"p\":34,\"v\":1},\"id\":\"d\"}\n"; x3.loop();
|
||||
OK(s3.out.find("\"ok\":false") != std::string::npos, "écrire sur une broche qui n'est pas en sortie est refusé");
|
||||
std::cout << "xerbot : " << n << "/" << n << "\n"; return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user