7 segment display library for mbed This routine support 2,4 and 8 digit anode-common or cathode-common LED.
Sseg.cpp@9:6d3229c8dc0f, 2015-01-12 (annotated)
- Committer:
- morecat_lab
- Date:
- Mon Jan 12 08:18:59 2015 +0000
- Revision:
- 9:6d3229c8dc0f
- Parent:
- 7:a19431131213
- Child:
- 10:1b8dbe52e5a8
avoid overflow 32bit counter (~=30min)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
morecat_lab | 0:6bf4ee8ee342 | 1 | /* |
morecat_lab | 4:858e42224b50 | 2 | Sseg.cpp - mbed library for 2/4/8 digit seven segment LED driver. |
morecat_lab | 4:858e42224b50 | 3 | Copyright 2013,2014 by morecat_lab |
morecat_lab | 0:6bf4ee8ee342 | 4 | |
morecat_lab | 0:6bf4ee8ee342 | 5 | base on Dots library. |
morecat_lab | 0:6bf4ee8ee342 | 6 | Copyright 2010 arms22. All right reserved. |
morecat_lab | 0:6bf4ee8ee342 | 7 | |
morecat_lab | 0:6bf4ee8ee342 | 8 | This library is distributed in the hope that it will be useful, |
morecat_lab | 0:6bf4ee8ee342 | 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
morecat_lab | 0:6bf4ee8ee342 | 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
morecat_lab | 0:6bf4ee8ee342 | 11 | */ |
morecat_lab | 0:6bf4ee8ee342 | 12 | |
morecat_lab | 0:6bf4ee8ee342 | 13 | #include "Sseg.h" |
morecat_lab | 0:6bf4ee8ee342 | 14 | #include <Timer.h> |
morecat_lab | 0:6bf4ee8ee342 | 15 | |
morecat_lab | 0:6bf4ee8ee342 | 16 | const int Sseg::numConv[] = { |
morecat_lab | 0:6bf4ee8ee342 | 17 | NUM_PAT_0, NUM_PAT_1, NUM_PAT_2, NUM_PAT_3, |
morecat_lab | 0:6bf4ee8ee342 | 18 | NUM_PAT_4, NUM_PAT_5, NUM_PAT_6, NUM_PAT_7, |
morecat_lab | 0:6bf4ee8ee342 | 19 | NUM_PAT_8, NUM_PAT_9, NUM_PAT_A, NUM_PAT_B, |
morecat_lab | 0:6bf4ee8ee342 | 20 | NUM_PAT_C, NUM_PAT_D, NUM_PAT_E, NUM_PAT_F}; |
morecat_lab | 0:6bf4ee8ee342 | 21 | |
morecat_lab | 0:6bf4ee8ee342 | 22 | // 2 digit |
morecat_lab | 0:6bf4ee8ee342 | 23 | Sseg::Sseg(PinName a,PinName b,PinName c,PinName d, |
morecat_lab | 0:6bf4ee8ee342 | 24 | PinName e,PinName f,PinName g,PinName dp, |
morecat_lab | 0:6bf4ee8ee342 | 25 | PinName d1,PinName d2) : |
morecat_lab | 0:6bf4ee8ee342 | 26 | _segPins( dp, g, f, e, d, c, b, a), _digPins(d1, d2) |
morecat_lab | 0:6bf4ee8ee342 | 27 | { |
morecat_lab | 0:6bf4ee8ee342 | 28 | _numOfDigs = 2; |
morecat_lab | 0:6bf4ee8ee342 | 29 | _updateInterval = (8333 / 2); |
morecat_lab | 0:6bf4ee8ee342 | 30 | _zeroSupress = true; |
morecat_lab | 0:6bf4ee8ee342 | 31 | _kcommon = false; |
morecat_lab | 7:a19431131213 | 32 | _sinkDriver = false; |
morecat_lab | 0:6bf4ee8ee342 | 33 | } |
morecat_lab | 0:6bf4ee8ee342 | 34 | |
morecat_lab | 0:6bf4ee8ee342 | 35 | // 4 digit |
morecat_lab | 0:6bf4ee8ee342 | 36 | Sseg::Sseg(PinName a,PinName b,PinName c,PinName d, |
morecat_lab | 0:6bf4ee8ee342 | 37 | PinName e,PinName f,PinName g,PinName dp, |
morecat_lab | 0:6bf4ee8ee342 | 38 | PinName d1,PinName d2,PinName d3,PinName d4) : |
morecat_lab | 0:6bf4ee8ee342 | 39 | _segPins( dp, g, f, e, d, c, b, a), _digPins(d1, d2, d3, d4) |
morecat_lab | 0:6bf4ee8ee342 | 40 | { |
morecat_lab | 0:6bf4ee8ee342 | 41 | _numOfDigs = 4; |
morecat_lab | 0:6bf4ee8ee342 | 42 | _updateInterval = (8333 / 4); |
morecat_lab | 0:6bf4ee8ee342 | 43 | _zeroSupress = true; |
morecat_lab | 0:6bf4ee8ee342 | 44 | _kcommon = false; |
morecat_lab | 7:a19431131213 | 45 | _sinkDriver = false; |
morecat_lab | 0:6bf4ee8ee342 | 46 | } |
morecat_lab | 0:6bf4ee8ee342 | 47 | |
morecat_lab | 0:6bf4ee8ee342 | 48 | // 8 digit |
morecat_lab | 0:6bf4ee8ee342 | 49 | Sseg::Sseg(PinName a,PinName b,PinName c,PinName d, |
morecat_lab | 0:6bf4ee8ee342 | 50 | PinName e,PinName f,PinName g,PinName dp, |
morecat_lab | 0:6bf4ee8ee342 | 51 | PinName d1,PinName d2,PinName d3,PinName d4, |
morecat_lab | 0:6bf4ee8ee342 | 52 | PinName d5,PinName d6,PinName d7,PinName d8) : |
morecat_lab | 0:6bf4ee8ee342 | 53 | _segPins( dp, g, f, e, d, c, b, a), _digPins(d1, d2, d3, d4, d5, d6, d7, d8) |
morecat_lab | 0:6bf4ee8ee342 | 54 | { |
morecat_lab | 0:6bf4ee8ee342 | 55 | _numOfDigs = 8; |
morecat_lab | 0:6bf4ee8ee342 | 56 | _updateInterval = (8333 / 8); |
morecat_lab | 0:6bf4ee8ee342 | 57 | _zeroSupress = true; |
morecat_lab | 0:6bf4ee8ee342 | 58 | _kcommon = false; |
morecat_lab | 7:a19431131213 | 59 | _sinkDriver = false; |
morecat_lab | 0:6bf4ee8ee342 | 60 | } |
morecat_lab | 0:6bf4ee8ee342 | 61 | |
morecat_lab | 0:6bf4ee8ee342 | 62 | |
morecat_lab | 0:6bf4ee8ee342 | 63 | void Sseg::begin(void) { |
morecat_lab | 0:6bf4ee8ee342 | 64 | timer.start(); |
morecat_lab | 0:6bf4ee8ee342 | 65 | clear(); |
morecat_lab | 0:6bf4ee8ee342 | 66 | } |
morecat_lab | 0:6bf4ee8ee342 | 67 | |
morecat_lab | 0:6bf4ee8ee342 | 68 | void Sseg::setAcommon(void) { |
morecat_lab | 0:6bf4ee8ee342 | 69 | _kcommon = false; |
morecat_lab | 0:6bf4ee8ee342 | 70 | } |
morecat_lab | 0:6bf4ee8ee342 | 71 | |
morecat_lab | 0:6bf4ee8ee342 | 72 | void Sseg::setKcommon(void) { |
morecat_lab | 0:6bf4ee8ee342 | 73 | _kcommon = true; |
morecat_lab | 0:6bf4ee8ee342 | 74 | } |
morecat_lab | 0:6bf4ee8ee342 | 75 | |
morecat_lab | 7:a19431131213 | 76 | void Sseg::setSinkDriver(void) { |
morecat_lab | 7:a19431131213 | 77 | _sinkDriver = true; |
morecat_lab | 7:a19431131213 | 78 | } |
morecat_lab | 7:a19431131213 | 79 | |
morecat_lab | 0:6bf4ee8ee342 | 80 | char Sseg::segCh(char i) { |
morecat_lab | 0:6bf4ee8ee342 | 81 | return Sseg::numConv[i]; |
morecat_lab | 0:6bf4ee8ee342 | 82 | } |
morecat_lab | 0:6bf4ee8ee342 | 83 | |
morecat_lab | 0:6bf4ee8ee342 | 84 | void Sseg::setDot(int d) { |
morecat_lab | 0:6bf4ee8ee342 | 85 | _buffer[d] |= 0x01; |
morecat_lab | 0:6bf4ee8ee342 | 86 | } |
morecat_lab | 0:6bf4ee8ee342 | 87 | |
morecat_lab | 0:6bf4ee8ee342 | 88 | void Sseg::clearDot(int d) { |
morecat_lab | 0:6bf4ee8ee342 | 89 | _buffer[d] &= 0xfe; |
morecat_lab | 0:6bf4ee8ee342 | 90 | } |
morecat_lab | 0:6bf4ee8ee342 | 91 | |
morecat_lab | 0:6bf4ee8ee342 | 92 | void Sseg::writeNum(int n) { |
morecat_lab | 0:6bf4ee8ee342 | 93 | if (_numOfDigs == 2) { |
morecat_lab | 0:6bf4ee8ee342 | 94 | writeNum2(n); |
morecat_lab | 0:6bf4ee8ee342 | 95 | } else if (_numOfDigs == 4) { |
morecat_lab | 0:6bf4ee8ee342 | 96 | writeNum4(n); |
morecat_lab | 0:6bf4ee8ee342 | 97 | } else if (_numOfDigs == 8) { |
morecat_lab | 0:6bf4ee8ee342 | 98 | writeNum8((long)n); |
morecat_lab | 0:6bf4ee8ee342 | 99 | } |
morecat_lab | 0:6bf4ee8ee342 | 100 | } |
morecat_lab | 0:6bf4ee8ee342 | 101 | |
morecat_lab | 0:6bf4ee8ee342 | 102 | void Sseg::writeNum2(int n) { |
morecat_lab | 0:6bf4ee8ee342 | 103 | if (n < 100) { |
morecat_lab | 0:6bf4ee8ee342 | 104 | _buffer[0] = segCh((n % 100) / 10); |
morecat_lab | 0:6bf4ee8ee342 | 105 | _buffer[1] = segCh(n % 10); |
morecat_lab | 0:6bf4ee8ee342 | 106 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 107 | } else { |
morecat_lab | 0:6bf4ee8ee342 | 108 | _buffer[0] = _buffer[1] = 0x02;// overflow |
morecat_lab | 0:6bf4ee8ee342 | 109 | } |
morecat_lab | 0:6bf4ee8ee342 | 110 | } |
morecat_lab | 0:6bf4ee8ee342 | 111 | |
morecat_lab | 0:6bf4ee8ee342 | 112 | void Sseg::writeNum4(int n) { |
morecat_lab | 0:6bf4ee8ee342 | 113 | if (n < 10000) { |
morecat_lab | 0:6bf4ee8ee342 | 114 | _buffer[0] = segCh((n % 10000) / 1000); |
morecat_lab | 0:6bf4ee8ee342 | 115 | _buffer[1] = segCh((n % 1000) / 100); |
morecat_lab | 0:6bf4ee8ee342 | 116 | _buffer[2] = segCh((n % 100) / 10); |
morecat_lab | 0:6bf4ee8ee342 | 117 | _buffer[3] = segCh(n % 10); |
morecat_lab | 0:6bf4ee8ee342 | 118 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 119 | } else { |
morecat_lab | 0:6bf4ee8ee342 | 120 | _buffer[0] = _buffer[1] = _buffer[2] = _buffer[3] = 0x02;// overflow |
morecat_lab | 0:6bf4ee8ee342 | 121 | } |
morecat_lab | 0:6bf4ee8ee342 | 122 | } |
morecat_lab | 0:6bf4ee8ee342 | 123 | |
morecat_lab | 0:6bf4ee8ee342 | 124 | void Sseg::writeNum8(int n) { |
morecat_lab | 0:6bf4ee8ee342 | 125 | _buffer[0] = segCh((n % 100000000) / 10000000); |
morecat_lab | 0:6bf4ee8ee342 | 126 | _buffer[1] = segCh((n % 10000000) / 1000000); |
morecat_lab | 0:6bf4ee8ee342 | 127 | _buffer[2] = segCh((n % 1000000) / 100000); |
morecat_lab | 0:6bf4ee8ee342 | 128 | _buffer[3] = segCh((n % 100000) / 10000); |
morecat_lab | 0:6bf4ee8ee342 | 129 | _buffer[4] = segCh((n % 10000) / 1000); |
morecat_lab | 0:6bf4ee8ee342 | 130 | _buffer[5] = segCh((n % 1000) / 100); |
morecat_lab | 0:6bf4ee8ee342 | 131 | _buffer[6] = segCh((n % 100) / 10); |
morecat_lab | 0:6bf4ee8ee342 | 132 | _buffer[7] = segCh(n % 10); |
morecat_lab | 0:6bf4ee8ee342 | 133 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 134 | } |
morecat_lab | 0:6bf4ee8ee342 | 135 | |
morecat_lab | 0:6bf4ee8ee342 | 136 | void Sseg::writeNum(char d1, char d2) { |
morecat_lab | 0:6bf4ee8ee342 | 137 | _buffer[0] = segCh(d1); |
morecat_lab | 0:6bf4ee8ee342 | 138 | _buffer[1] = segCh(d2); |
morecat_lab | 0:6bf4ee8ee342 | 139 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 140 | } |
morecat_lab | 0:6bf4ee8ee342 | 141 | |
morecat_lab | 0:6bf4ee8ee342 | 142 | void Sseg::writeNum(char d1, char d2, char d3, char d4) { |
morecat_lab | 0:6bf4ee8ee342 | 143 | _buffer[0] = segCh(d1); |
morecat_lab | 0:6bf4ee8ee342 | 144 | _buffer[1] = segCh(d2); |
morecat_lab | 0:6bf4ee8ee342 | 145 | _buffer[2] = segCh(d3); |
morecat_lab | 0:6bf4ee8ee342 | 146 | _buffer[3] = segCh(d4); |
morecat_lab | 0:6bf4ee8ee342 | 147 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 148 | } |
morecat_lab | 0:6bf4ee8ee342 | 149 | |
morecat_lab | 0:6bf4ee8ee342 | 150 | void Sseg::writeNum(char d1, char d2, char d3, char d4, |
morecat_lab | 0:6bf4ee8ee342 | 151 | char d5, char d6, char d7, char d8) |
morecat_lab | 0:6bf4ee8ee342 | 152 | { |
morecat_lab | 0:6bf4ee8ee342 | 153 | _buffer[0] = segCh(d1); |
morecat_lab | 0:6bf4ee8ee342 | 154 | _buffer[1] = segCh(d2); |
morecat_lab | 0:6bf4ee8ee342 | 155 | _buffer[2] = segCh(d3); |
morecat_lab | 0:6bf4ee8ee342 | 156 | _buffer[3] = segCh(d4); |
morecat_lab | 0:6bf4ee8ee342 | 157 | _buffer[4] = segCh(d5); |
morecat_lab | 0:6bf4ee8ee342 | 158 | _buffer[5] = segCh(d6); |
morecat_lab | 0:6bf4ee8ee342 | 159 | _buffer[6] = segCh(d7); |
morecat_lab | 0:6bf4ee8ee342 | 160 | _buffer[7] = segCh(d8); |
morecat_lab | 0:6bf4ee8ee342 | 161 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 162 | } |
morecat_lab | 0:6bf4ee8ee342 | 163 | |
morecat_lab | 0:6bf4ee8ee342 | 164 | void Sseg::writeHex(int n) { |
morecat_lab | 0:6bf4ee8ee342 | 165 | if(_numOfDigs == 2) { |
morecat_lab | 0:6bf4ee8ee342 | 166 | _buffer[0] = segCh((n >> 4) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 167 | _buffer[1] = segCh(n & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 168 | } else if (_numOfDigs == 4) { |
morecat_lab | 0:6bf4ee8ee342 | 169 | _buffer[0] = segCh((n >> 12) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 170 | _buffer[1] = segCh((n >> 8) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 171 | _buffer[2] = segCh((n >> 4) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 172 | _buffer[3] = segCh(n & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 173 | } |
morecat_lab | 0:6bf4ee8ee342 | 174 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 175 | } |
morecat_lab | 0:6bf4ee8ee342 | 176 | |
morecat_lab | 0:6bf4ee8ee342 | 177 | void Sseg::writeHex(long n) { |
morecat_lab | 0:6bf4ee8ee342 | 178 | _buffer[0] = segCh((n >> 28) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 179 | _buffer[1] = segCh((n >> 24) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 180 | _buffer[2] = segCh((n >> 20) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 181 | _buffer[3] = segCh((n >> 16) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 182 | _buffer[4] = segCh((n >> 12) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 183 | _buffer[5] = segCh((n >> 8) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 184 | _buffer[6] = segCh((n >> 4) & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 185 | _buffer[7] = segCh(n & 0xf); |
morecat_lab | 0:6bf4ee8ee342 | 186 | Sseg::supressZero(); |
morecat_lab | 0:6bf4ee8ee342 | 187 | } |
morecat_lab | 0:6bf4ee8ee342 | 188 | |
morecat_lab | 0:6bf4ee8ee342 | 189 | void Sseg::setZeroSupress(bool t) { |
morecat_lab | 0:6bf4ee8ee342 | 190 | _zeroSupress = t; |
morecat_lab | 0:6bf4ee8ee342 | 191 | } |
morecat_lab | 0:6bf4ee8ee342 | 192 | |
morecat_lab | 0:6bf4ee8ee342 | 193 | void Sseg::supressZero() { |
morecat_lab | 0:6bf4ee8ee342 | 194 | int i; |
morecat_lab | 0:6bf4ee8ee342 | 195 | if (_zeroSupress ) { |
morecat_lab | 0:6bf4ee8ee342 | 196 | for (i = 0 ; i < (_numOfDigs-1) ; i++) { |
morecat_lab | 0:6bf4ee8ee342 | 197 | if (_buffer[i] == segCh(0)) { |
morecat_lab | 0:6bf4ee8ee342 | 198 | _buffer[i] = _buffer[i] & 0x1; |
morecat_lab | 0:6bf4ee8ee342 | 199 | } else { |
morecat_lab | 0:6bf4ee8ee342 | 200 | break; |
morecat_lab | 0:6bf4ee8ee342 | 201 | } |
morecat_lab | 0:6bf4ee8ee342 | 202 | } |
morecat_lab | 0:6bf4ee8ee342 | 203 | } |
morecat_lab | 0:6bf4ee8ee342 | 204 | } |
morecat_lab | 0:6bf4ee8ee342 | 205 | |
morecat_lab | 0:6bf4ee8ee342 | 206 | void Sseg::writeRawData(char d1, char d2) { |
morecat_lab | 0:6bf4ee8ee342 | 207 | _buffer[0] = d1; |
morecat_lab | 0:6bf4ee8ee342 | 208 | _buffer[1] = d2; |
morecat_lab | 0:6bf4ee8ee342 | 209 | } |
morecat_lab | 0:6bf4ee8ee342 | 210 | |
morecat_lab | 0:6bf4ee8ee342 | 211 | void Sseg::writeRawData(char d1, char d2, char d3, char d4) { |
morecat_lab | 0:6bf4ee8ee342 | 212 | _buffer[0] = d1; |
morecat_lab | 0:6bf4ee8ee342 | 213 | _buffer[1] = d2; |
morecat_lab | 0:6bf4ee8ee342 | 214 | _buffer[2] = d3; |
morecat_lab | 0:6bf4ee8ee342 | 215 | _buffer[3] = d4; |
morecat_lab | 0:6bf4ee8ee342 | 216 | } |
morecat_lab | 0:6bf4ee8ee342 | 217 | |
morecat_lab | 0:6bf4ee8ee342 | 218 | void Sseg::writeRawData(char d1, char d2, char d3, char d4, |
morecat_lab | 0:6bf4ee8ee342 | 219 | char d5, char d6, char d7, char d8) |
morecat_lab | 0:6bf4ee8ee342 | 220 | { |
morecat_lab | 0:6bf4ee8ee342 | 221 | _buffer[0] = d1; |
morecat_lab | 0:6bf4ee8ee342 | 222 | _buffer[1] = d2; |
morecat_lab | 0:6bf4ee8ee342 | 223 | _buffer[2] = d3; |
morecat_lab | 0:6bf4ee8ee342 | 224 | _buffer[3] = d4; |
morecat_lab | 0:6bf4ee8ee342 | 225 | _buffer[4] = d5; |
morecat_lab | 0:6bf4ee8ee342 | 226 | _buffer[5] = d6; |
morecat_lab | 0:6bf4ee8ee342 | 227 | _buffer[6] = d7; |
morecat_lab | 0:6bf4ee8ee342 | 228 | _buffer[7] = d8; |
morecat_lab | 0:6bf4ee8ee342 | 229 | } |
morecat_lab | 0:6bf4ee8ee342 | 230 | |
morecat_lab | 0:6bf4ee8ee342 | 231 | void Sseg::write(uint8_t x, uint8_t y, uint8_t value) { |
morecat_lab | 0:6bf4ee8ee342 | 232 | uint8_t tmp,msk; |
morecat_lab | 0:6bf4ee8ee342 | 233 | tmp = _buffer[y]; |
morecat_lab | 0:6bf4ee8ee342 | 234 | msk = 0x80 >> x; |
morecat_lab | 0:6bf4ee8ee342 | 235 | tmp = tmp & ~msk; |
morecat_lab | 0:6bf4ee8ee342 | 236 | tmp = tmp | (value ? msk : 0); |
morecat_lab | 0:6bf4ee8ee342 | 237 | _buffer[y] = tmp; |
morecat_lab | 0:6bf4ee8ee342 | 238 | } |
morecat_lab | 0:6bf4ee8ee342 | 239 | |
morecat_lab | 1:12daa491059c | 240 | void Sseg::write(uint8_t d, uint8_t value) { |
morecat_lab | 1:12daa491059c | 241 | _buffer[d] = value; |
morecat_lab | 0:6bf4ee8ee342 | 242 | } |
morecat_lab | 0:6bf4ee8ee342 | 243 | |
morecat_lab | 0:6bf4ee8ee342 | 244 | void Sseg::write(uint8_t y, const uint8_t values[], uint8_t size) { |
morecat_lab | 0:6bf4ee8ee342 | 245 | uint8_t i; |
morecat_lab | 0:6bf4ee8ee342 | 246 | for(i=0;i<size;i++) |
morecat_lab | 0:6bf4ee8ee342 | 247 | _buffer[(y++) & 0x07] = values[i]; |
morecat_lab | 0:6bf4ee8ee342 | 248 | } |
morecat_lab | 0:6bf4ee8ee342 | 249 | |
morecat_lab | 0:6bf4ee8ee342 | 250 | void Sseg::clear(void) { |
morecat_lab | 0:6bf4ee8ee342 | 251 | int i; |
morecat_lab | 0:6bf4ee8ee342 | 252 | for(i=0;i<8;i++){ |
morecat_lab | 0:6bf4ee8ee342 | 253 | _buffer[i] = 0; |
morecat_lab | 0:6bf4ee8ee342 | 254 | } |
morecat_lab | 0:6bf4ee8ee342 | 255 | _dig = _numOfDigs - 1; |
morecat_lab | 0:6bf4ee8ee342 | 256 | } |
morecat_lab | 0:6bf4ee8ee342 | 257 | |
morecat_lab | 0:6bf4ee8ee342 | 258 | void Sseg::turnOff(void) { |
morecat_lab | 0:6bf4ee8ee342 | 259 | if ( _kcommon) { |
morecat_lab | 0:6bf4ee8ee342 | 260 | _digPins = _digPins & (1 << _dig); // set LOW |
morecat_lab | 0:6bf4ee8ee342 | 261 | } else { |
morecat_lab | 0:6bf4ee8ee342 | 262 | _digPins = _digPins | (~(1 << _dig)); // set HIGH |
morecat_lab | 0:6bf4ee8ee342 | 263 | } |
morecat_lab | 0:6bf4ee8ee342 | 264 | } |
morecat_lab | 0:6bf4ee8ee342 | 265 | |
morecat_lab | 0:6bf4ee8ee342 | 266 | void Sseg::turnOn(void) { |
morecat_lab | 0:6bf4ee8ee342 | 267 | if ( _kcommon) { |
morecat_lab | 0:6bf4ee8ee342 | 268 | _digPins = _digPins | (~(1 << _dig)); // set HIGH |
morecat_lab | 0:6bf4ee8ee342 | 269 | } else { |
morecat_lab | 0:6bf4ee8ee342 | 270 | _digPins = _digPins & (1 << _dig); // set LOW |
morecat_lab | 0:6bf4ee8ee342 | 271 | } |
morecat_lab | 0:6bf4ee8ee342 | 272 | } |
morecat_lab | 0:6bf4ee8ee342 | 273 | |
morecat_lab | 0:6bf4ee8ee342 | 274 | void Sseg::updateSeg(void) { |
morecat_lab | 0:6bf4ee8ee342 | 275 | if( (++_dig) >= _numOfDigs) |
morecat_lab | 0:6bf4ee8ee342 | 276 | _dig = 0; |
morecat_lab | 7:a19431131213 | 277 | _segPins = (( _kcommon) || (_sinkDriver)) ? _buffer[_dig] : ~_buffer[_dig]; |
morecat_lab | 0:6bf4ee8ee342 | 278 | } |
morecat_lab | 0:6bf4ee8ee342 | 279 | |
morecat_lab | 0:6bf4ee8ee342 | 280 | bool Sseg::update(void) { |
morecat_lab | 0:6bf4ee8ee342 | 281 | int t = timer.read_us(); |
morecat_lab | 0:6bf4ee8ee342 | 282 | bool sync = false; |
morecat_lab | 0:6bf4ee8ee342 | 283 | if((t - _lastUpdateTime) > _updateInterval){ |
morecat_lab | 0:6bf4ee8ee342 | 284 | turnOff(); |
morecat_lab | 0:6bf4ee8ee342 | 285 | updateSeg(); |
morecat_lab | 0:6bf4ee8ee342 | 286 | turnOn(); |
morecat_lab | 0:6bf4ee8ee342 | 287 | _lastUpdateTime = t; |
morecat_lab | 0:6bf4ee8ee342 | 288 | sync = (_dig == 0); |
morecat_lab | 0:6bf4ee8ee342 | 289 | } |
morecat_lab | 0:6bf4ee8ee342 | 290 | return sync; |
morecat_lab | 0:6bf4ee8ee342 | 291 | } |
morecat_lab | 0:6bf4ee8ee342 | 292 | |
morecat_lab | 0:6bf4ee8ee342 | 293 | void Sseg::updateWithDelay(int ms) { |
morecat_lab | 9:6d3229c8dc0f | 294 | timer.reset(); // to avoid overflow 32bit counter (~=30min) |
morecat_lab | 0:6bf4ee8ee342 | 295 | int start = timer.read_ms(); |
morecat_lab | 0:6bf4ee8ee342 | 296 | do { |
morecat_lab | 0:6bf4ee8ee342 | 297 | bool sync = update(); |
morecat_lab | 0:6bf4ee8ee342 | 298 | if(sync){ |
morecat_lab | 0:6bf4ee8ee342 | 299 | int t = timer.read_ms(); |
morecat_lab | 0:6bf4ee8ee342 | 300 | if((t - start) >= ms){ |
morecat_lab | 0:6bf4ee8ee342 | 301 | break; |
morecat_lab | 0:6bf4ee8ee342 | 302 | } |
morecat_lab | 0:6bf4ee8ee342 | 303 | } |
morecat_lab | 0:6bf4ee8ee342 | 304 | } while(1); |
morecat_lab | 0:6bf4ee8ee342 | 305 | } |
morecat_lab | 0:6bf4ee8ee342 | 306 | |
morecat_lab | 0:6bf4ee8ee342 | 307 | void Sseg::updateOnce(void) { |
morecat_lab | 0:6bf4ee8ee342 | 308 | uint8_t i; |
morecat_lab | 0:6bf4ee8ee342 | 309 | _dig = _numOfDigs - 1; |
morecat_lab | 0:6bf4ee8ee342 | 310 | turnOff(); |
morecat_lab | 0:6bf4ee8ee342 | 311 | for (i = 0 ; i < _numOfDigs ; i++) { |
morecat_lab | 0:6bf4ee8ee342 | 312 | updateSeg(); |
morecat_lab | 0:6bf4ee8ee342 | 313 | turnOn(); |
morecat_lab | 0:6bf4ee8ee342 | 314 | wait(0.001f); // wait 1ms |
morecat_lab | 0:6bf4ee8ee342 | 315 | turnOff(); |
morecat_lab | 0:6bf4ee8ee342 | 316 | } |
morecat_lab | 0:6bf4ee8ee342 | 317 | } |
morecat_lab | 0:6bf4ee8ee342 | 318 | |
morecat_lab | 0:6bf4ee8ee342 | 319 | // EOF |