Example code to toggle the RGB LED of the application shield with a State Machine

Files at this revision

API Documentation at this revision

Comitter:
pcordemans
Date:
Wed Oct 28 12:17:07 2020 +0000
Commit message:
Toggles RGB led with a State Machine

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
CONTRIBUTING.md Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
StateMachine.cpp Show annotated file Show diff for this revision Revisions of this file
StateMachine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
resources/official_armmbed_example_badge.png Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CONTRIBUTING.md	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,5 @@
+# Contributing to Mbed OS
+
+Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor.
+
+To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html).
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,9 @@
+# State Machine example
+
+This is an example project for the State Machine pattern.
+
+## License and contributions
+
+The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info.
+
+This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StateMachine.cpp	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,67 @@
+#include "StateMachine.h"
+
+StateMachine::StateMachine()
+{
+
+    r = new PwmOut(D5);
+    g = new PwmOut(D9);
+    b = new PwmOut(D8);
+    currentState = RED;
+
+}
+
+StateMachine::~StateMachine()
+{
+    delete r;
+    delete g;
+    delete b;
+}
+
+void StateMachine::start()
+{
+    while(true) {
+        switch(currentState) {
+            case RED:
+                actionRed();
+                currentState = GREEN;
+                break;
+            case GREEN:
+                actionGreen();
+                currentState = BLUE;
+                break;
+            case BLUE:
+                actionBlue();
+                currentState = RED;
+                break;
+            default:
+                currentState = CERROR;
+                return;
+        }
+    }
+}
+
+void StateMachine::actionRed()
+{
+    *r = 0.0;
+    *g = 1.0;
+    *b = 1.0;
+    wait(1.0);
+}
+
+void StateMachine::actionGreen()
+{
+    *r = 1.0;
+    *g = 0.0;
+    *b = 1.0;
+    wait(1.0);
+
+}
+
+void StateMachine::actionBlue()
+{
+    *r = 1.0;
+    *g = 1.0;
+    *b = 0.0;
+    wait(1.0);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StateMachine.h	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,26 @@
+#ifndef STATEMACHINE_H
+#define STATEMACHINE_H
+
+#include "mbed.h"
+
+enum State {RED, GREEN, BLUE, CERROR};
+
+class StateMachine {
+private:
+    State currentState;
+    void actionRed();
+    void actionGreen();
+    void actionBlue();
+    PwmOut* r;
+    PwmOut* g;
+    PwmOut* b;
+    
+public:
+     
+    StateMachine();
+    ~StateMachine();
+    
+    void start();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+#include "StateMachine.h"
+
+PwmOut r (D5);
+PwmOut g (D8);
+PwmOut b (D9);
+
+int main()
+{
+    StateMachine statemachine;
+    statemachine.start();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Oct 28 12:17:07 2020 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#cf4f12a123c05fcae83fc56d76442015cb8a39e9
Binary file resources/official_armmbed_example_badge.png has changed