robot

Dependencies:   FastPWM3 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LedBlinker.cpp Source File

LedBlinker.cpp

00001 #include "mbed.h"
00002 #include "LedBlinker.h"
00003 
00004 LedBlinker::LedBlinker(PinName p, float sample_frequency, float code_frequency) {
00005     _led = new DigitalOut(p);
00006     
00007     _tics_per_code = (int)(sample_frequency / code_frequency);
00008     _tics_per_bit = _tics_per_code / 8;
00009     
00010     _counter = 0;
00011     _code = 0;
00012     _bit = 0;
00013 }
00014 
00015 void LedBlinker::set_code(uint8_t code) {_code = code;}
00016 
00017 void LedBlinker::update() {
00018     if (_counter % _tics_per_code == 0) {_counter = 0; _bit = 0;}
00019     _counter++;
00020     if (_counter % _tics_per_bit != 0) return;
00021     _led->write((_code & (1 << _bit)) >> _bit);
00022     _bit++;
00023 }