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/05/14 08:13] bmonsuez [Etape 4 : Profiter de l'héritage] |
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 432: | Line 459: | ||
| class CustomComponent | class CustomComponent | ||
| { | { | ||
| + | private: | ||
| + | bool m_Debug; | ||
| + | const char* m_Name; | ||
| protected; | protected; | ||
| - | CustomComponent(const char* name, bool debug) | + | CustomComponent(const char* theName, bool debug = false): |
| + | Name(theName), m_Debug(debug) | ||
| {} | {} | ||
| public: | public: | ||
| - | bool debugIsActive() const; | + | bool debugIsActive() const { return m_Debug; } |
| | | ||
| - | const char* getName() const; | + | bool EnableDebug() { m_Debug = true; } |
| - | const char* header() const; // Génère l'entête de message avec le nom du composant. | + | bool DisableDebug() { m_Debug = false; } |
| + | |||
| + | const char* getName() const { return m_Name; } | ||
| + | String header() const { return (String)m_Name + ": "; } | ||
| }; | }; | ||
| class Led: public CustomComponent | class Led: public CustomComponent | ||
| { | { | ||
| - | public: | + | 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(); | ||
| + | } | ||
| + | | ||
| | | ||