// Xerbot.h — le contrat xerbot v1 du xerboxion, pour tout microcontrôleur Arduino (04.11.2026). // Une ligne JSON par message, dans les deux sens : // appareil → {"dev":"cube-1","type":"capteur","telemetry":{"t":21.5}} {"dev":…,"state":{"online":true}} {"dev":…,"ack":{"id":"x1","ok":true}} // labo → {"dev":"cube-1","cmd":"led","args":{"on":true},"id":"x1"} // Le labo (ploxion xerbot › ⌁ trouver, jOSXerbotLien) lit ces lignes en USB (Web Serial), en Bluetooth (service Nordic UART) ou via une // passerelle ESP-NOW ; chaque appareil devient ploxion.xerbot..* sur le bus. Aucune dépendance ; tient sur un Arduino Uno. // Xerbot xb("cube-1", "capteur"); // void setup() { Serial.begin(115200); xb.begin(Serial); xb.onCommande(maCommande); } // void loop() { xb.loop(); if (toutesLes2s) xb.telemetrie().add("t", 21.5).envoyer(); } #pragma once #include #if defined(__AVR__) #define XERBOT_LIGNE 128 #else #define XERBOT_LIGNE 512 #endif class Xerbot; // Un message en construction : add(clé, valeur)…, puis envoyer(). Les valeurs texte sont échappées. class XerbotMsg { public: XerbotMsg(Xerbot* x, const char* section) : _x(x), _sec(section), _n(0), _vide(true) { _b[0] = 0; } XerbotMsg& add(const char* k, const char* v); XerbotMsg& add(const char* k, double v, uint8_t decimales = 2); XerbotMsg& add(const char* k, long v); XerbotMsg& add(const char* k, int v) { return add(k, (long)v); } XerbotMsg& add(const char* k, bool v); XerbotMsg& brut(const char* k, const char* json); // une valeur JSON déjà faite (tableau, objet) bool envoyer(); const char* corps() const { return _b; } private: void cle(const char* k); void ecrire(const char* s); Xerbot* _x; const char* _sec; char _b[XERBOT_LIGNE]; size_t _n; bool _vide; }; // Une commande reçue. args = l'objet JSON brut des arguments ({…}, ou "{}"). struct XerbotCommande { char cmd[24]; char id[28]; const char* args; size_t argsLen; bool texte(const char* nom, char* out, size_t n) const; // args.nom en texte double nombre(const char* nom, double defaut = 0) const; // args.nom en nombre bool booleen(const char* nom, bool defaut = false) const; }; // Où partent les lignes : par défaut le Stream de begin() ; ESP-NOW et Bluetooth en posent un autre. typedef void (*XerbotSortie)(const char* ligne, void* ctx); class Xerbot { public: Xerbot(const char* dev, const char* type = "capteur") : _dev(dev), _type(type) {} void begin(Stream& s) { _s = &s; etatComplet().envoyer(); } 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); // 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"); } XerbotMsg etat() { return XerbotMsg(this, "state"); } bool ack(const char* id, bool ok = true, const char* err = nullptr); bool ligne(const char* json); // envoyer une ligne JSON brute (déjà complète) const char* dev() const { return _dev; } const char* type() const { return _type; } // les briques du mini-lecteur JSON (aussi utiles dans un croquis) static bool jsonTexte(const char* src, const char* cle, char* out, size_t n); static bool jsonNombre(const char* src, const char* cle, double* out); static const char* jsonObjet(const char* src, const char* cle, size_t* len); private: friend class XerbotMsg; bool emettre(const char* section, const char* corps); const char* _dev; const char* _type; Stream* _s = nullptr; XerbotSortie _sortie = nullptr; void* _ctx = nullptr; 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(); };