Simple 6-LED bar library. Provides some useful functions.
Simple 6 leds bar library. It allows you to control individual leds, and provides masks.
6LedBar.cpp@4:ecb39b6f0e18, 2017-05-21 (annotated)
- Committer:
- kryksyh
- Date:
- Sun May 21 14:59:47 2017 +0000
- Revision:
- 4:ecb39b6f0e18
- Parent:
- 1:248129e96f43
set_mask method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kryksyh | 0:52b8975f1dd9 | 1 | #include "6LedBar.h" |
kryksyh | 0:52b8975f1dd9 | 2 | |
kryksyh | 0:52b8975f1dd9 | 3 | LedBar::LedBar(PinName p0, PinName p1, PinName p2, |
kryksyh | 1:248129e96f43 | 4 | PinName p3, PinName p4, PinName p5) : |
kryksyh | 1:248129e96f43 | 5 | l0(p0), l1(p1), l2(p2), l3(p3), l4(p4), l5(p5) |
kryksyh | 1:248129e96f43 | 6 | { |
kryksyh | 1:248129e96f43 | 7 | m_leds[0] = &l0; |
kryksyh | 1:248129e96f43 | 8 | m_leds[1] = &l1; |
kryksyh | 1:248129e96f43 | 9 | m_leds[2] = &l2; |
kryksyh | 1:248129e96f43 | 10 | m_leds[3] = &l3; |
kryksyh | 1:248129e96f43 | 11 | m_leds[4] = &l4; |
kryksyh | 1:248129e96f43 | 12 | m_leds[5] = &l5; |
kryksyh | 0:52b8975f1dd9 | 13 | |
kryksyh | 0:52b8975f1dd9 | 14 | } |
kryksyh | 0:52b8975f1dd9 | 15 | |
kryksyh | 1:248129e96f43 | 16 | void LedBar::on(int n) { |
kryksyh | 1:248129e96f43 | 17 | if(n < LEDS_COUNT) { |
kryksyh | 1:248129e96f43 | 18 | *(m_leds[n]) = 1; |
kryksyh | 1:248129e96f43 | 19 | } |
kryksyh | 1:248129e96f43 | 20 | } |
kryksyh | 1:248129e96f43 | 21 | |
kryksyh | 1:248129e96f43 | 22 | void LedBar::off(int n) { |
kryksyh | 1:248129e96f43 | 23 | if(n < LEDS_COUNT) { |
kryksyh | 1:248129e96f43 | 24 | *(m_leds[n]) = 0; |
kryksyh | 1:248129e96f43 | 25 | } |
kryksyh | 1:248129e96f43 | 26 | } |
kryksyh | 1:248129e96f43 | 27 | |
kryksyh | 1:248129e96f43 | 28 | void LedBar::toggle(int n) { |
kryksyh | 1:248129e96f43 | 29 | if(n < LEDS_COUNT) { |
kryksyh | 1:248129e96f43 | 30 | *(m_leds[n]) = ! (*m_leds[n]); |
kryksyh | 1:248129e96f43 | 31 | } |
kryksyh | 1:248129e96f43 | 32 | } |
kryksyh | 1:248129e96f43 | 33 | |
kryksyh | 1:248129e96f43 | 34 | void LedBar::on_mask(int m) { |
kryksyh | 1:248129e96f43 | 35 | for(int i = 0; i < LEDS_COUNT; i++) { |
kryksyh | 1:248129e96f43 | 36 | if(m & (1 << i)) { |
kryksyh | 1:248129e96f43 | 37 | on(i); |
kryksyh | 1:248129e96f43 | 38 | } |
kryksyh | 1:248129e96f43 | 39 | } |
kryksyh | 1:248129e96f43 | 40 | } |
kryksyh | 1:248129e96f43 | 41 | |
kryksyh | 1:248129e96f43 | 42 | void LedBar::off_mask(int m) { |
kryksyh | 1:248129e96f43 | 43 | for(int i = 0; i < LEDS_COUNT; i++) { |
kryksyh | 1:248129e96f43 | 44 | if(m & (1 << i)) { |
kryksyh | 1:248129e96f43 | 45 | off(i); |
kryksyh | 1:248129e96f43 | 46 | } |
kryksyh | 1:248129e96f43 | 47 | } |
kryksyh | 1:248129e96f43 | 48 | } |
kryksyh | 1:248129e96f43 | 49 | |
kryksyh | 1:248129e96f43 | 50 | void LedBar::toggle_mask(int m) { |
kryksyh | 1:248129e96f43 | 51 | for(int i = 0; i < LEDS_COUNT; i++) { |
kryksyh | 1:248129e96f43 | 52 | if(m & (1 << i)) { |
kryksyh | 1:248129e96f43 | 53 | toggle(i); |
kryksyh | 1:248129e96f43 | 54 | } |
kryksyh | 1:248129e96f43 | 55 | } |
kryksyh | 4:ecb39b6f0e18 | 56 | } |
kryksyh | 4:ecb39b6f0e18 | 57 | |
kryksyh | 4:ecb39b6f0e18 | 58 | void LedBar::set_mask(int m) { |
kryksyh | 4:ecb39b6f0e18 | 59 | for(int i = 0; i < LEDS_COUNT; i++) { |
kryksyh | 4:ecb39b6f0e18 | 60 | if(m & (1 << i)) { |
kryksyh | 4:ecb39b6f0e18 | 61 | on(i); |
kryksyh | 4:ecb39b6f0e18 | 62 | } |
kryksyh | 4:ecb39b6f0e18 | 63 | else { |
kryksyh | 4:ecb39b6f0e18 | 64 | off(i); |
kryksyh | 4:ecb39b6f0e18 | 65 | } |
kryksyh | 4:ecb39b6f0e18 | 66 | } |
kryksyh | 1:248129e96f43 | 67 | } |