This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
in202:seance_4:led [2021/04/11 16:16] bmonsuez |
in202:seance_4:led [2022/11/18 10:47] (current) |
||
---|---|---|---|
Line 24: | Line 24: | ||
} | } | ||
</code> | </code> | ||
+ | |||
+ | Nous voyons que le pseudo-code équivalent serait : | ||
+ | |||
+ | <code> | ||
+ | Initialisation: | ||
+ | port LED_BUILDIN est un composant LED. Donc le port est en écriture. | ||
+ | | ||
+ | Loop: | ||
+ | allume la LED. | ||
+ | attend une seconde. | ||
+ | eteint la LED. | ||
+ | attend une seconde. | ||
+ | </code> | ||
+ | |||
+ | Si nous avions une classe ''Led'', nous pourrions réécrire le code comme suit : | ||
+ | |||
+ | <code cpp> | ||
+ | |||
+ | Led builtinLed(LED_BUILTIN); | ||
+ | // Crée un objet LED connecté au port LED_BUILTIN | ||
+ | | ||
+ | void setup() {} | ||
+ | |||
+ | void loop() { | ||
+ | builtinLed = true; | ||
+ | delay(1000); | ||
+ | builtinLed = false; | ||
+ | delay(1000); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Le code serait nettement plus lisible. | ||
+ | |||
+ | On pourrait même inverser l'état de la led et le code se réduirait à : | ||
+ | |||
+ | <code cpp> | ||
+ | |||
+ | Led builtinLed(LED_BUILTIN); | ||
+ | // Crée un objet LED connecté au port LED_BUILTIN | ||
+ | | ||
+ | void setup() {} | ||
+ | |||
+ | void loop() { | ||
+ | builtinLed = !(builtinLed; | ||
+ | delay(1000); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ====== Action : Créer une classe Led ====== | ||
+ | |||
+ | Nous vous proposons de créer une classe ''Led'' ayant l'entête suivante : | ||
+ | |||
+ | <code cpp> | ||
+ | class Led | ||
+ | { | ||
+ | public: | ||
+ | Led(uint32_t thePin); | ||
+ | Led& operator = (bool isOn); | ||
+ | operator bool() const; | ||
+ | Led& operator ~(); // Optionel. | ||
+ | }; | ||
+ | <code> | ||
+ | |||
+ | |||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | | ||
+ |