robot

Dependencies:   FastPWM3 mbed

Revision:
223:b986e7cee521
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LedBlinker/LedBlinker.cpp	Tue Oct 02 07:12:01 2018 +0000
@@ -0,0 +1,23 @@
+#include "mbed.h"
+#include "LedBlinker.h"
+
+LedBlinker::LedBlinker(PinName p, float sample_frequency, float code_frequency) {
+    _led = new DigitalOut(p);
+    
+    _tics_per_code = (int)(sample_frequency / code_frequency);
+    _tics_per_bit = _tics_per_code / 8;
+    
+    _counter = 0;
+    _code = 0;
+    _bit = 0;
+}
+
+void LedBlinker::set_code(uint8_t code) {_code = code;}
+
+void LedBlinker::update() {
+    if (_counter % _tics_per_code == 0) {_counter = 0; _bit = 0;}
+    _counter++;
+    if (_counter % _tics_per_bit != 0) return;
+    _led->write((_code & (1 << _bit)) >> _bit);
+    _bit++;
+}
\ No newline at end of file