7 segment display library for mbed This routine support 2,4 and 8 digit anode-common or cathode-common LED.

Dependents:   7segMbed

Committer:
morecat_lab
Date:
Sun Jul 05 07:21:33 2015 +0000
Revision:
10:1b8dbe52e5a8
Parent:
9:6d3229c8dc0f
Child:
11:8be75770e375
Change BusOut to DigitalOut[]

Who changed what in which revision?

UserRevisionLine numberNew 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 10:1b8dbe52e5a8 3 Copyright 2013,2014,2015 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 10:1b8dbe52e5a8 25 PinName d1,PinName d2)
morecat_lab 0:6bf4ee8ee342 26 {
morecat_lab 0:6bf4ee8ee342 27 _numOfDigs = 2;
morecat_lab 10:1b8dbe52e5a8 28 PinName segPins[8] = {a, b, c, d, e, f, g, dp};
morecat_lab 10:1b8dbe52e5a8 29 PinName digPins[2] = {d1, d2};
morecat_lab 10:1b8dbe52e5a8 30
morecat_lab 10:1b8dbe52e5a8 31 for (int i=0; i<8; i++) {
morecat_lab 10:1b8dbe52e5a8 32 _segPins[i] = new DigitalOut(segPins[i]);
morecat_lab 10:1b8dbe52e5a8 33 }
morecat_lab 10:1b8dbe52e5a8 34
morecat_lab 10:1b8dbe52e5a8 35 for (int i=0; i<_numOfDigs; i++) {
morecat_lab 10:1b8dbe52e5a8 36 _digPins[i] = new DigitalOut(digPins[i]);
morecat_lab 10:1b8dbe52e5a8 37 }
morecat_lab 10:1b8dbe52e5a8 38
morecat_lab 0:6bf4ee8ee342 39 _updateInterval = (8333 / 2);
morecat_lab 0:6bf4ee8ee342 40 _zeroSupress = true;
morecat_lab 0:6bf4ee8ee342 41 _kcommon = false;
morecat_lab 7:a19431131213 42 _sinkDriver = false;
morecat_lab 0:6bf4ee8ee342 43 }
morecat_lab 0:6bf4ee8ee342 44
morecat_lab 0:6bf4ee8ee342 45 // 4 digit
morecat_lab 0:6bf4ee8ee342 46 Sseg::Sseg(PinName a,PinName b,PinName c,PinName d,
morecat_lab 0:6bf4ee8ee342 47 PinName e,PinName f,PinName g,PinName dp,
morecat_lab 10:1b8dbe52e5a8 48 PinName d1,PinName d2,PinName d3,PinName d4)
morecat_lab 0:6bf4ee8ee342 49 {
morecat_lab 0:6bf4ee8ee342 50 _numOfDigs = 4;
morecat_lab 10:1b8dbe52e5a8 51 PinName segPins[8] = {a, b, c, d, e, f, g, dp};
morecat_lab 10:1b8dbe52e5a8 52 PinName digPins[4] = {d1, d2, d3, d4};
morecat_lab 10:1b8dbe52e5a8 53
morecat_lab 10:1b8dbe52e5a8 54 for (int i=0; i<8; i++) {
morecat_lab 10:1b8dbe52e5a8 55 _segPins[i] = new DigitalOut(segPins[i]);
morecat_lab 10:1b8dbe52e5a8 56 }
morecat_lab 10:1b8dbe52e5a8 57
morecat_lab 10:1b8dbe52e5a8 58 for (int i=0; i<_numOfDigs; i++) {
morecat_lab 10:1b8dbe52e5a8 59 _digPins[i] = new DigitalOut(digPins[i]);
morecat_lab 10:1b8dbe52e5a8 60 }
morecat_lab 0:6bf4ee8ee342 61 _updateInterval = (8333 / 4);
morecat_lab 0:6bf4ee8ee342 62 _zeroSupress = true;
morecat_lab 0:6bf4ee8ee342 63 _kcommon = false;
morecat_lab 7:a19431131213 64 _sinkDriver = false;
morecat_lab 0:6bf4ee8ee342 65 }
morecat_lab 0:6bf4ee8ee342 66
morecat_lab 0:6bf4ee8ee342 67 // 8 digit
morecat_lab 0:6bf4ee8ee342 68 Sseg::Sseg(PinName a,PinName b,PinName c,PinName d,
morecat_lab 0:6bf4ee8ee342 69 PinName e,PinName f,PinName g,PinName dp,
morecat_lab 0:6bf4ee8ee342 70 PinName d1,PinName d2,PinName d3,PinName d4,
morecat_lab 10:1b8dbe52e5a8 71 PinName d5,PinName d6,PinName d7,PinName d8)
morecat_lab 0:6bf4ee8ee342 72 {
morecat_lab 0:6bf4ee8ee342 73 _numOfDigs = 8;
morecat_lab 10:1b8dbe52e5a8 74 PinName segPins[8] = {a, b, c, d, e, f, g, dp};
morecat_lab 10:1b8dbe52e5a8 75 PinName digPins[8] = {d1, d2, d3, d4, d5, d6, d7, d8};
morecat_lab 10:1b8dbe52e5a8 76
morecat_lab 10:1b8dbe52e5a8 77 for (int i=0; i<8; i++) {
morecat_lab 10:1b8dbe52e5a8 78 _segPins[i] = new DigitalOut(segPins[i]);
morecat_lab 10:1b8dbe52e5a8 79 }
morecat_lab 10:1b8dbe52e5a8 80
morecat_lab 10:1b8dbe52e5a8 81 for (int i=0; i<_numOfDigs; i++) {
morecat_lab 10:1b8dbe52e5a8 82 _digPins[i] = new DigitalOut(digPins[i]);
morecat_lab 10:1b8dbe52e5a8 83 } _updateInterval = (8333 / 8);
morecat_lab 0:6bf4ee8ee342 84 _zeroSupress = true;
morecat_lab 0:6bf4ee8ee342 85 _kcommon = false;
morecat_lab 7:a19431131213 86 _sinkDriver = false;
morecat_lab 0:6bf4ee8ee342 87 }
morecat_lab 0:6bf4ee8ee342 88
morecat_lab 0:6bf4ee8ee342 89
morecat_lab 0:6bf4ee8ee342 90 void Sseg::begin(void) {
morecat_lab 0:6bf4ee8ee342 91 timer.start();
morecat_lab 0:6bf4ee8ee342 92 clear();
morecat_lab 0:6bf4ee8ee342 93 }
morecat_lab 0:6bf4ee8ee342 94
morecat_lab 0:6bf4ee8ee342 95 void Sseg::setAcommon(void) {
morecat_lab 0:6bf4ee8ee342 96 _kcommon = false;
morecat_lab 0:6bf4ee8ee342 97 }
morecat_lab 0:6bf4ee8ee342 98
morecat_lab 0:6bf4ee8ee342 99 void Sseg::setKcommon(void) {
morecat_lab 0:6bf4ee8ee342 100 _kcommon = true;
morecat_lab 0:6bf4ee8ee342 101 }
morecat_lab 0:6bf4ee8ee342 102
morecat_lab 7:a19431131213 103 void Sseg::setSinkDriver(void) {
morecat_lab 7:a19431131213 104 _sinkDriver = true;
morecat_lab 7:a19431131213 105 }
morecat_lab 7:a19431131213 106
morecat_lab 0:6bf4ee8ee342 107 char Sseg::segCh(char i) {
morecat_lab 0:6bf4ee8ee342 108 return Sseg::numConv[i];
morecat_lab 0:6bf4ee8ee342 109 }
morecat_lab 0:6bf4ee8ee342 110
morecat_lab 0:6bf4ee8ee342 111 void Sseg::setDot(int d) {
morecat_lab 0:6bf4ee8ee342 112 _buffer[d] |= 0x01;
morecat_lab 0:6bf4ee8ee342 113 }
morecat_lab 0:6bf4ee8ee342 114
morecat_lab 0:6bf4ee8ee342 115 void Sseg::clearDot(int d) {
morecat_lab 0:6bf4ee8ee342 116 _buffer[d] &= 0xfe;
morecat_lab 0:6bf4ee8ee342 117 }
morecat_lab 0:6bf4ee8ee342 118
morecat_lab 0:6bf4ee8ee342 119 void Sseg::writeNum(int n) {
morecat_lab 0:6bf4ee8ee342 120 if (_numOfDigs == 2) {
morecat_lab 0:6bf4ee8ee342 121 writeNum2(n);
morecat_lab 0:6bf4ee8ee342 122 } else if (_numOfDigs == 4) {
morecat_lab 0:6bf4ee8ee342 123 writeNum4(n);
morecat_lab 0:6bf4ee8ee342 124 } else if (_numOfDigs == 8) {
morecat_lab 0:6bf4ee8ee342 125 writeNum8((long)n);
morecat_lab 0:6bf4ee8ee342 126 }
morecat_lab 0:6bf4ee8ee342 127 }
morecat_lab 0:6bf4ee8ee342 128
morecat_lab 0:6bf4ee8ee342 129 void Sseg::writeNum2(int n) {
morecat_lab 0:6bf4ee8ee342 130 if (n < 100) {
morecat_lab 0:6bf4ee8ee342 131 _buffer[0] = segCh((n % 100) / 10);
morecat_lab 0:6bf4ee8ee342 132 _buffer[1] = segCh(n % 10);
morecat_lab 0:6bf4ee8ee342 133 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 134 } else {
morecat_lab 0:6bf4ee8ee342 135 _buffer[0] = _buffer[1] = 0x02;// overflow
morecat_lab 0:6bf4ee8ee342 136 }
morecat_lab 0:6bf4ee8ee342 137 }
morecat_lab 0:6bf4ee8ee342 138
morecat_lab 0:6bf4ee8ee342 139 void Sseg::writeNum4(int n) {
morecat_lab 0:6bf4ee8ee342 140 if (n < 10000) {
morecat_lab 0:6bf4ee8ee342 141 _buffer[0] = segCh((n % 10000) / 1000);
morecat_lab 0:6bf4ee8ee342 142 _buffer[1] = segCh((n % 1000) / 100);
morecat_lab 0:6bf4ee8ee342 143 _buffer[2] = segCh((n % 100) / 10);
morecat_lab 0:6bf4ee8ee342 144 _buffer[3] = segCh(n % 10);
morecat_lab 0:6bf4ee8ee342 145 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 146 } else {
morecat_lab 0:6bf4ee8ee342 147 _buffer[0] = _buffer[1] = _buffer[2] = _buffer[3] = 0x02;// overflow
morecat_lab 0:6bf4ee8ee342 148 }
morecat_lab 0:6bf4ee8ee342 149 }
morecat_lab 0:6bf4ee8ee342 150
morecat_lab 0:6bf4ee8ee342 151 void Sseg::writeNum8(int n) {
morecat_lab 0:6bf4ee8ee342 152 _buffer[0] = segCh((n % 100000000) / 10000000);
morecat_lab 0:6bf4ee8ee342 153 _buffer[1] = segCh((n % 10000000) / 1000000);
morecat_lab 0:6bf4ee8ee342 154 _buffer[2] = segCh((n % 1000000) / 100000);
morecat_lab 0:6bf4ee8ee342 155 _buffer[3] = segCh((n % 100000) / 10000);
morecat_lab 0:6bf4ee8ee342 156 _buffer[4] = segCh((n % 10000) / 1000);
morecat_lab 0:6bf4ee8ee342 157 _buffer[5] = segCh((n % 1000) / 100);
morecat_lab 0:6bf4ee8ee342 158 _buffer[6] = segCh((n % 100) / 10);
morecat_lab 0:6bf4ee8ee342 159 _buffer[7] = segCh(n % 10);
morecat_lab 0:6bf4ee8ee342 160 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 161 }
morecat_lab 0:6bf4ee8ee342 162
morecat_lab 0:6bf4ee8ee342 163 void Sseg::writeNum(char d1, char d2) {
morecat_lab 0:6bf4ee8ee342 164 _buffer[0] = segCh(d1);
morecat_lab 0:6bf4ee8ee342 165 _buffer[1] = segCh(d2);
morecat_lab 0:6bf4ee8ee342 166 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 167 }
morecat_lab 0:6bf4ee8ee342 168
morecat_lab 0:6bf4ee8ee342 169 void Sseg::writeNum(char d1, char d2, char d3, char d4) {
morecat_lab 0:6bf4ee8ee342 170 _buffer[0] = segCh(d1);
morecat_lab 0:6bf4ee8ee342 171 _buffer[1] = segCh(d2);
morecat_lab 0:6bf4ee8ee342 172 _buffer[2] = segCh(d3);
morecat_lab 0:6bf4ee8ee342 173 _buffer[3] = segCh(d4);
morecat_lab 0:6bf4ee8ee342 174 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 175 }
morecat_lab 0:6bf4ee8ee342 176
morecat_lab 0:6bf4ee8ee342 177 void Sseg::writeNum(char d1, char d2, char d3, char d4,
morecat_lab 0:6bf4ee8ee342 178 char d5, char d6, char d7, char d8)
morecat_lab 0:6bf4ee8ee342 179 {
morecat_lab 0:6bf4ee8ee342 180 _buffer[0] = segCh(d1);
morecat_lab 0:6bf4ee8ee342 181 _buffer[1] = segCh(d2);
morecat_lab 0:6bf4ee8ee342 182 _buffer[2] = segCh(d3);
morecat_lab 0:6bf4ee8ee342 183 _buffer[3] = segCh(d4);
morecat_lab 0:6bf4ee8ee342 184 _buffer[4] = segCh(d5);
morecat_lab 0:6bf4ee8ee342 185 _buffer[5] = segCh(d6);
morecat_lab 0:6bf4ee8ee342 186 _buffer[6] = segCh(d7);
morecat_lab 0:6bf4ee8ee342 187 _buffer[7] = segCh(d8);
morecat_lab 0:6bf4ee8ee342 188 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 189 }
morecat_lab 0:6bf4ee8ee342 190
morecat_lab 0:6bf4ee8ee342 191 void Sseg::writeHex(int n) {
morecat_lab 0:6bf4ee8ee342 192 if(_numOfDigs == 2) {
morecat_lab 0:6bf4ee8ee342 193 _buffer[0] = segCh((n >> 4) & 0xf);
morecat_lab 0:6bf4ee8ee342 194 _buffer[1] = segCh(n & 0xf);
morecat_lab 0:6bf4ee8ee342 195 } else if (_numOfDigs == 4) {
morecat_lab 0:6bf4ee8ee342 196 _buffer[0] = segCh((n >> 12) & 0xf);
morecat_lab 0:6bf4ee8ee342 197 _buffer[1] = segCh((n >> 8) & 0xf);
morecat_lab 0:6bf4ee8ee342 198 _buffer[2] = segCh((n >> 4) & 0xf);
morecat_lab 0:6bf4ee8ee342 199 _buffer[3] = segCh(n & 0xf);
morecat_lab 0:6bf4ee8ee342 200 }
morecat_lab 0:6bf4ee8ee342 201 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 202 }
morecat_lab 0:6bf4ee8ee342 203
morecat_lab 0:6bf4ee8ee342 204 void Sseg::writeHex(long n) {
morecat_lab 0:6bf4ee8ee342 205 _buffer[0] = segCh((n >> 28) & 0xf);
morecat_lab 0:6bf4ee8ee342 206 _buffer[1] = segCh((n >> 24) & 0xf);
morecat_lab 0:6bf4ee8ee342 207 _buffer[2] = segCh((n >> 20) & 0xf);
morecat_lab 0:6bf4ee8ee342 208 _buffer[3] = segCh((n >> 16) & 0xf);
morecat_lab 0:6bf4ee8ee342 209 _buffer[4] = segCh((n >> 12) & 0xf);
morecat_lab 0:6bf4ee8ee342 210 _buffer[5] = segCh((n >> 8) & 0xf);
morecat_lab 0:6bf4ee8ee342 211 _buffer[6] = segCh((n >> 4) & 0xf);
morecat_lab 0:6bf4ee8ee342 212 _buffer[7] = segCh(n & 0xf);
morecat_lab 0:6bf4ee8ee342 213 Sseg::supressZero();
morecat_lab 0:6bf4ee8ee342 214 }
morecat_lab 0:6bf4ee8ee342 215
morecat_lab 0:6bf4ee8ee342 216 void Sseg::setZeroSupress(bool t) {
morecat_lab 0:6bf4ee8ee342 217 _zeroSupress = t;
morecat_lab 0:6bf4ee8ee342 218 }
morecat_lab 0:6bf4ee8ee342 219
morecat_lab 0:6bf4ee8ee342 220 void Sseg::supressZero() {
morecat_lab 0:6bf4ee8ee342 221 int i;
morecat_lab 0:6bf4ee8ee342 222 if (_zeroSupress ) {
morecat_lab 0:6bf4ee8ee342 223 for (i = 0 ; i < (_numOfDigs-1) ; i++) {
morecat_lab 0:6bf4ee8ee342 224 if (_buffer[i] == segCh(0)) {
morecat_lab 0:6bf4ee8ee342 225 _buffer[i] = _buffer[i] & 0x1;
morecat_lab 0:6bf4ee8ee342 226 } else {
morecat_lab 0:6bf4ee8ee342 227 break;
morecat_lab 0:6bf4ee8ee342 228 }
morecat_lab 0:6bf4ee8ee342 229 }
morecat_lab 0:6bf4ee8ee342 230 }
morecat_lab 0:6bf4ee8ee342 231 }
morecat_lab 0:6bf4ee8ee342 232
morecat_lab 0:6bf4ee8ee342 233 void Sseg::writeRawData(char d1, char d2) {
morecat_lab 0:6bf4ee8ee342 234 _buffer[0] = d1;
morecat_lab 0:6bf4ee8ee342 235 _buffer[1] = d2;
morecat_lab 0:6bf4ee8ee342 236 }
morecat_lab 0:6bf4ee8ee342 237
morecat_lab 0:6bf4ee8ee342 238 void Sseg::writeRawData(char d1, char d2, char d3, char d4) {
morecat_lab 0:6bf4ee8ee342 239 _buffer[0] = d1;
morecat_lab 0:6bf4ee8ee342 240 _buffer[1] = d2;
morecat_lab 0:6bf4ee8ee342 241 _buffer[2] = d3;
morecat_lab 0:6bf4ee8ee342 242 _buffer[3] = d4;
morecat_lab 0:6bf4ee8ee342 243 }
morecat_lab 0:6bf4ee8ee342 244
morecat_lab 0:6bf4ee8ee342 245 void Sseg::writeRawData(char d1, char d2, char d3, char d4,
morecat_lab 0:6bf4ee8ee342 246 char d5, char d6, char d7, char d8)
morecat_lab 0:6bf4ee8ee342 247 {
morecat_lab 0:6bf4ee8ee342 248 _buffer[0] = d1;
morecat_lab 0:6bf4ee8ee342 249 _buffer[1] = d2;
morecat_lab 0:6bf4ee8ee342 250 _buffer[2] = d3;
morecat_lab 0:6bf4ee8ee342 251 _buffer[3] = d4;
morecat_lab 0:6bf4ee8ee342 252 _buffer[4] = d5;
morecat_lab 0:6bf4ee8ee342 253 _buffer[5] = d6;
morecat_lab 0:6bf4ee8ee342 254 _buffer[6] = d7;
morecat_lab 0:6bf4ee8ee342 255 _buffer[7] = d8;
morecat_lab 0:6bf4ee8ee342 256 }
morecat_lab 0:6bf4ee8ee342 257
morecat_lab 0:6bf4ee8ee342 258 void Sseg::write(uint8_t x, uint8_t y, uint8_t value) {
morecat_lab 0:6bf4ee8ee342 259 uint8_t tmp,msk;
morecat_lab 0:6bf4ee8ee342 260 tmp = _buffer[y];
morecat_lab 0:6bf4ee8ee342 261 msk = 0x80 >> x;
morecat_lab 0:6bf4ee8ee342 262 tmp = tmp & ~msk;
morecat_lab 0:6bf4ee8ee342 263 tmp = tmp | (value ? msk : 0);
morecat_lab 0:6bf4ee8ee342 264 _buffer[y] = tmp;
morecat_lab 0:6bf4ee8ee342 265 }
morecat_lab 0:6bf4ee8ee342 266
morecat_lab 1:12daa491059c 267 void Sseg::write(uint8_t d, uint8_t value) {
morecat_lab 1:12daa491059c 268 _buffer[d] = value;
morecat_lab 0:6bf4ee8ee342 269 }
morecat_lab 0:6bf4ee8ee342 270
morecat_lab 0:6bf4ee8ee342 271 void Sseg::write(uint8_t y, const uint8_t values[], uint8_t size) {
morecat_lab 0:6bf4ee8ee342 272 uint8_t i;
morecat_lab 0:6bf4ee8ee342 273 for(i=0;i<size;i++)
morecat_lab 0:6bf4ee8ee342 274 _buffer[(y++) & 0x07] = values[i];
morecat_lab 0:6bf4ee8ee342 275 }
morecat_lab 0:6bf4ee8ee342 276
morecat_lab 0:6bf4ee8ee342 277 void Sseg::clear(void) {
morecat_lab 0:6bf4ee8ee342 278 int i;
morecat_lab 0:6bf4ee8ee342 279 for(i=0;i<8;i++){
morecat_lab 0:6bf4ee8ee342 280 _buffer[i] = 0;
morecat_lab 0:6bf4ee8ee342 281 }
morecat_lab 0:6bf4ee8ee342 282 _dig = _numOfDigs - 1;
morecat_lab 0:6bf4ee8ee342 283 }
morecat_lab 0:6bf4ee8ee342 284
morecat_lab 0:6bf4ee8ee342 285 void Sseg::turnOff(void) {
morecat_lab 10:1b8dbe52e5a8 286 _digPins[_dig] -> write((_kcommon == 0) ? 0 : 1);
morecat_lab 0:6bf4ee8ee342 287 }
morecat_lab 0:6bf4ee8ee342 288
morecat_lab 0:6bf4ee8ee342 289 void Sseg::turnOn(void) {
morecat_lab 10:1b8dbe52e5a8 290 _digPins[_dig] -> write((_kcommon == 0) ? 1 : 1);
morecat_lab 0:6bf4ee8ee342 291 }
morecat_lab 0:6bf4ee8ee342 292
morecat_lab 0:6bf4ee8ee342 293 void Sseg::updateSeg(void) {
morecat_lab 10:1b8dbe52e5a8 294 char i,data,mask;
morecat_lab 0:6bf4ee8ee342 295 if( (++_dig) >= _numOfDigs)
morecat_lab 0:6bf4ee8ee342 296 _dig = 0;
morecat_lab 10:1b8dbe52e5a8 297 data = _buffer[_dig];
morecat_lab 10:1b8dbe52e5a8 298 mask = 0x80;
morecat_lab 10:1b8dbe52e5a8 299 for(i=0;i<8;i++){
morecat_lab 10:1b8dbe52e5a8 300 if(data & mask){
morecat_lab 10:1b8dbe52e5a8 301 _segPins[i]->write((_kcommon == 0) ? 0 : 1);
morecat_lab 10:1b8dbe52e5a8 302 }else{
morecat_lab 10:1b8dbe52e5a8 303 _segPins[i]->write((_kcommon == 0) ? 1 : 0);
morecat_lab 10:1b8dbe52e5a8 304 }
morecat_lab 10:1b8dbe52e5a8 305 mask >>= 1;
morecat_lab 10:1b8dbe52e5a8 306 }
morecat_lab 0:6bf4ee8ee342 307 }
morecat_lab 0:6bf4ee8ee342 308
morecat_lab 0:6bf4ee8ee342 309 bool Sseg::update(void) {
morecat_lab 0:6bf4ee8ee342 310 int t = timer.read_us();
morecat_lab 0:6bf4ee8ee342 311 bool sync = false;
morecat_lab 0:6bf4ee8ee342 312 if((t - _lastUpdateTime) > _updateInterval){
morecat_lab 0:6bf4ee8ee342 313 turnOff();
morecat_lab 0:6bf4ee8ee342 314 updateSeg();
morecat_lab 0:6bf4ee8ee342 315 turnOn();
morecat_lab 0:6bf4ee8ee342 316 _lastUpdateTime = t;
morecat_lab 0:6bf4ee8ee342 317 sync = (_dig == 0);
morecat_lab 0:6bf4ee8ee342 318 }
morecat_lab 0:6bf4ee8ee342 319 return sync;
morecat_lab 0:6bf4ee8ee342 320 }
morecat_lab 0:6bf4ee8ee342 321
morecat_lab 0:6bf4ee8ee342 322 void Sseg::updateWithDelay(int ms) {
morecat_lab 9:6d3229c8dc0f 323 timer.reset(); // to avoid overflow 32bit counter (~=30min)
morecat_lab 0:6bf4ee8ee342 324 int start = timer.read_ms();
morecat_lab 0:6bf4ee8ee342 325 do {
morecat_lab 0:6bf4ee8ee342 326 bool sync = update();
morecat_lab 0:6bf4ee8ee342 327 if(sync){
morecat_lab 0:6bf4ee8ee342 328 int t = timer.read_ms();
morecat_lab 0:6bf4ee8ee342 329 if((t - start) >= ms){
morecat_lab 0:6bf4ee8ee342 330 break;
morecat_lab 0:6bf4ee8ee342 331 }
morecat_lab 0:6bf4ee8ee342 332 }
morecat_lab 0:6bf4ee8ee342 333 } while(1);
morecat_lab 0:6bf4ee8ee342 334 }
morecat_lab 0:6bf4ee8ee342 335
morecat_lab 0:6bf4ee8ee342 336 void Sseg::updateOnce(void) {
morecat_lab 0:6bf4ee8ee342 337 uint8_t i;
morecat_lab 0:6bf4ee8ee342 338 _dig = _numOfDigs - 1;
morecat_lab 0:6bf4ee8ee342 339 turnOff();
morecat_lab 0:6bf4ee8ee342 340 for (i = 0 ; i < _numOfDigs ; i++) {
morecat_lab 0:6bf4ee8ee342 341 updateSeg();
morecat_lab 0:6bf4ee8ee342 342 turnOn();
morecat_lab 0:6bf4ee8ee342 343 wait(0.001f); // wait 1ms
morecat_lab 0:6bf4ee8ee342 344 turnOff();
morecat_lab 0:6bf4ee8ee342 345 }
morecat_lab 0:6bf4ee8ee342 346 }
morecat_lab 0:6bf4ee8ee342 347
morecat_lab 0:6bf4ee8ee342 348 // EOF