summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-05-05 17:14:38 +0200
committerNao Pross <naopross@thearcway.org>2018-05-05 17:14:38 +0200
commit1dfe3e1f1be38824ecc0367314e2b3fef108b65f (patch)
treefdc353cd172f4903df10ddd72a4a509abfbde4fd
parentRename class pin to io_pin, add general gpio class, add uart echo (diff)
downloadSAMLiquidSmoke-1dfe3e1f1be38824ecc0367314e2b3fef108b65f.tar.gz
SAMLiquidSmoke-1dfe3e1f1be38824ecc0367314e2b3fef108b65f.zip
Add led class
Diffstat (limited to '')
-rw-r--r--led.cpp80
-rw-r--r--led.hpp44
2 files changed, 124 insertions, 0 deletions
diff --git a/led.cpp b/led.cpp
new file mode 100644
index 0000000..1d10e24
--- /dev/null
+++ b/led.cpp
@@ -0,0 +1,80 @@
+/*
+ * File: led.cpp
+ * Author: naopross
+ *
+ * Created on May 5, 2018, 2:33 PM
+ */
+
+#include "led.hpp"
+
+led::led(gpio *pin, led::color color) : _pin(pin), _color(color)
+{
+ _pin->set_mode(0);
+}
+
+
+led::~led()
+{
+
+}
+
+void led::pin(gpio *pin)
+{
+ if (pin) {
+ _pin = pin;
+ }
+}
+
+gpio * led::pin() const
+{
+ return _pin;
+}
+
+void led::get_color(led::color color)
+{
+ _color = color;
+}
+
+led::color led::set_color() const
+{
+ return _color;
+}
+
+void led::set(unsigned s)
+{
+ _pin->set(s);
+}
+
+unsigned led::read() const
+{
+ return _pin->read();
+}
+
+void led::toggle()
+{
+ _pin->toggle();
+}
+
+std::string led::to_string()
+{
+ std::string str = "led (";
+
+ switch (_color) {
+ case color::RED:
+ str += "red";
+ break;
+
+ case color::GREEN:
+ str += "green";
+ break;
+
+ case color::YELLOW:
+ str += "yellow";
+ break;
+ }
+
+ str += ") is ";
+ str += (read()) ? "on" : "off";
+
+ return str;
+}
diff --git a/led.hpp b/led.hpp
new file mode 100644
index 0000000..30b7a7a
--- /dev/null
+++ b/led.hpp
@@ -0,0 +1,44 @@
+/*
+ * File: led.hpp
+ * Author: naopross
+ *
+ * Created on May 5, 2018, 2:33 PM
+ */
+
+#ifndef LED_HPP
+#define LED_HPP
+
+#include "hal/pin.hpp"
+
+#include <string>
+
+class led {
+public:
+ enum class color
+ {
+ RED, GREEN, YELLOW,
+ };
+
+ led(gpio *pin, color color);
+ virtual ~led();
+
+ void pin(gpio *pin);
+ gpio * pin() const ;
+
+ void get_color(color color);
+ color set_color() const;
+
+ void set(unsigned s);
+ unsigned read() const;
+ void toggle();
+
+ std::string to_string();
+
+private:
+ led() {}
+
+ gpio *_pin = nullptr;
+ color _color;
+};
+
+#endif /* LED_HPP */