Simple control for ST M5451 LED driver.
Sample usage
#include "mbed.h" #include "M5451.h" M5451 m5451(D8, D7); void toggleOneByOne(){ for(char i = 0; i < m5451.outputs(); i++){ m5451.toggleBit(i); m5451.update(); wait_ms(250); m5451.toggleBit(i); } } int main() { while(1) { m5451.setAllBits(0); toggleOneByOne(); m5451.setAllBits(1); toggleOneByOne(); } }
Revision 1:db1e69c22bc6, committed 2020-03-01
- Comitter:
- jirrick
- Date:
- Sun Mar 01 11:41:57 2020 +0000
- Parent:
- 0:b21b3517a1d6
- Commit message:
- add toggleBit
Changed in this revision
M5451.cpp | Show annotated file Show diff for this revision Revisions of this file |
M5451.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r b21b3517a1d6 -r db1e69c22bc6 M5451.cpp --- a/M5451.cpp Sun Mar 01 10:20:26 2020 +0000 +++ b/M5451.cpp Sun Mar 01 11:41:57 2020 +0000 @@ -6,7 +6,7 @@ _state = 0ULL; } -int M5451::outputs() { +short M5451::outputs() { return JIRRICK_M5451_OUTPUTS; } @@ -14,16 +14,23 @@ _state = state; } -void M5451::setBit(char position, char value){ - if (position < JIRRICK_M5451_OUTPUTS) +void M5451::setBit(short position, short value){ + if (position >= 0 && position < JIRRICK_M5451_OUTPUTS) { uint64_t value64 = value; _state = (_state & ~(1ULL << position)) | (value64 << position); } } -void M5451::setAllBits(char value){ - for(char i = 0; i < JIRRICK_M5451_OUTPUTS; i++){ +void M5451::toggleBit(short position){ + if (position >= 0 && position < JIRRICK_M5451_OUTPUTS) + { + _state ^= 1ULL << position; + } +} + +void M5451::setAllBits(short value){ + for(short i = 0; i < JIRRICK_M5451_OUTPUTS; i++){ setBit(i, value); } } @@ -31,8 +38,8 @@ void M5451::update() { _send(1); //START BIT uint64_t input = _state; - for(char i = 0; i < JIRRICK_M5451_OUTPUTS; i++){ - char value = (input & 1ULL); + for(short i = 0; i < JIRRICK_M5451_OUTPUTS; i++){ + short value = (input & 1ULL); _send(value); input = input >> 1; } @@ -42,7 +49,7 @@ return _state; } -void M5451::_send(char bit) { +void M5451::_send(short bit) { _dataPin = (bit % 2); wait_us(JIRRICK_M5451_WAIT); _clockPin = 1;
diff -r b21b3517a1d6 -r db1e69c22bc6 M5451.h --- a/M5451.h Sun Mar 01 10:20:26 2020 +0000 +++ b/M5451.h Sun Mar 01 11:41:57 2020 +0000 @@ -9,10 +9,11 @@ class M5451 { public: M5451(PinName dataPin, PinName clockPin); - int outputs(); + short outputs(); void setState(uint64_t state); - void setBit(char position, char value); - void setAllBits(char value); + void setBit(short position, short value); + void toggleBit(short position); + void setAllBits(short value); void update(); uint64_t getState(); @@ -20,7 +21,7 @@ DigitalOut _dataPin; DigitalOut _clockPin; uint64_t _state; - void _send(char bit); + void _send(short bit); }; #endif \ No newline at end of file