This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
in202:seance_5:component_led [2021/04/30 10:12] bmonsuez |
in202:seance_5:component_led [2022/11/18 10:46] (current) |
||
---|---|---|---|
Line 407: | Line 407: | ||
L'idée est de proposer une classe de base que l'on appellera ''CustomComponent'' dont héritera l'ensemble des autres classes composants comme par exemple ''Led''. | L'idée est de proposer une classe de base que l'on appellera ''CustomComponent'' dont héritera l'ensemble des autres classes composants comme par exemple ''Led''. | ||
+ | |||
+ | |||
+ | <code cpp> | ||
+ | bool isSwitchOn() const { return m_isSwitchedOn; } | ||
+ | bool getPort() const { return m_portNumber; } | ||
+ | | ||
+ | void switchOn() | ||
+ | { | ||
+ | if(!m_isSwitchedOn) | ||
+ | { | ||
+ | m_isSwitchedOn = true; | ||
+ | digitalWrite(m_portNumber, HIGH); | ||
+ | } | ||
+ | } | ||
+ | void switchOff() | ||
+ | { | ||
+ | if(m_isSwitchedOn) | ||
+ | { | ||
+ | m_isSwitchedOn = false; | ||
+ | digitalWrite(m_portNumber, LOW); | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | </code> | ||
+ | |||
+ | |||
+ | </hidden> | ||
<code cpp> | <code cpp> | ||
Line 412: | Line 439: | ||
{ | { | ||
protected; | protected; | ||
- | CustomComponent(std::string name, bool debug) | + | CustomComponent(const char* name, bool debug) |
{} | {} | ||
public: | public: | ||
bool debugIsActive() const; | bool debugIsActive() const; | ||
- | std::string getName() const; | ||
| | ||
- | std::string header() const; // Génère l'entête de message avec le nom du composant. | + | const char* getName() const; |
+ | const char* header() const; // Génère l'entête de message avec le nom du composant. | ||
}; | }; | ||
</code> | </code> | ||
Line 427: | Line 454: | ||
* Tester votre code. | * Tester votre code. | ||
+ | |||
+ | <code cpp> | ||
+ | |||
+ | class CustomComponent | ||
+ | { | ||
+ | private: | ||
+ | bool m_Debug; | ||
+ | const char* m_Name; | ||
+ | protected; | ||
+ | CustomComponent(const char* theName, bool debug = false): | ||
+ | Name(theName), m_Debug(debug) | ||
+ | {} | ||
+ | public: | ||
+ | bool debugIsActive() const { return m_Debug; } | ||
+ | | ||
+ | bool EnableDebug() { m_Debug = true; } | ||
+ | bool DisableDebug() { m_Debug = false; } | ||
+ | |||
+ | const char* getName() const { return m_Name; } | ||
+ | String header() const { return (String)m_Name + ": "; } | ||
+ | |||
+ | }; | ||
+ | |||
+ | class Led: public CustomComponent | ||
+ | { | ||
+ | private: | ||
+ | bool m_isSwitchedOn; | ||
+ | uint32_t m_portNumber; | ||
+ | public: | ||
+ | Led(uint32_t thePort, const char* theName): Led(thePort, theName, false) {} | ||
+ | Led(uint32_t thePort, const char* theName, bool switchOnWhenStarted): CustomComponent(theName, false), | ||
+ | m_portNumber(thePort), m_isSwitchedOn(switchOnWhenStarted) | ||
+ | { | ||
+ | pinMode(m_portNumber, OUTPUT); | ||
+ | if(switchOnWhenStarted) | ||
+ | switchOn(); | ||
+ | } | ||
+ | | ||
+ | |||
+ | | ||
+ | }; | ||
+ | </code> | ||
====== Navigation ====== | ====== Navigation ====== |