xerbot 0.1.0 : le contrat xerbot v1 pour Arduino / PlatformIO (USB, ESP-NOW, Bluetooth), 6 exemples dont RuView CSI, test natif 8/8
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R75H65SynGwRG5vBSoYeZ1
This commit is contained in:
79
src/Xerbot.h
Normal file
79
src/Xerbot.h
Normal file
@@ -0,0 +1,79 @@
|
||||
// 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.<dev>.* 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 <Arduino.h>
|
||||
|
||||
#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; etat().add("online", true).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 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;
|
||||
};
|
||||
Reference in New Issue
Block a user