another clone

Dependents:   AwesomeAlarm_BLE

Committer:
mkkharel
Date:
Fri Sep 09 05:26:16 2016 +0000
Revision:
1:cae630b12d63
Parent:
0:d3173c8bfd48
Child:
2:2a21c0342751
Expanded the number of characters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seeed 0:d3173c8bfd48 1 /* The library of Grove - 4 Digit Display
seeed 0:d3173c8bfd48 2 *
seeed 0:d3173c8bfd48 3 * \author Yihui Xiong
seeed 0:d3173c8bfd48 4 * \date 2014/2/8
seeed 0:d3173c8bfd48 5 *
seeed 0:d3173c8bfd48 6 * The MIT License (MIT)
seeed 0:d3173c8bfd48 7 *
seeed 0:d3173c8bfd48 8 * Copyright (c) 2014 Seeed Technology Inc.
seeed 0:d3173c8bfd48 9 *
seeed 0:d3173c8bfd48 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
seeed 0:d3173c8bfd48 11 * of this software and associated documentation files (the "Software"), to deal
seeed 0:d3173c8bfd48 12 * in the Software without restriction, including without limitation the rights
seeed 0:d3173c8bfd48 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
seeed 0:d3173c8bfd48 14 * copies of the Software, and to permit persons to whom the Software is
seeed 0:d3173c8bfd48 15 * furnished to do so, subject to the following conditions:
seeed 0:d3173c8bfd48 16 *
seeed 0:d3173c8bfd48 17 * The above copyright notice and this permission notice shall be included in
seeed 0:d3173c8bfd48 18 * all copies or substantial portions of the Software.
seeed 0:d3173c8bfd48 19 *
seeed 0:d3173c8bfd48 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
seeed 0:d3173c8bfd48 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
seeed 0:d3173c8bfd48 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
seeed 0:d3173c8bfd48 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
seeed 0:d3173c8bfd48 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
seeed 0:d3173c8bfd48 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
seeed 0:d3173c8bfd48 26 * THE SOFTWARE.
seeed 0:d3173c8bfd48 27 */
seeed 0:d3173c8bfd48 28
seeed 0:d3173c8bfd48 29 #include "DigitDisplay.h"
seeed 0:d3173c8bfd48 30
seeed 0:d3173c8bfd48 31 #define ADDR_AUTO 0x40
seeed 0:d3173c8bfd48 32 #define ADDR_FIXED 0x44
seeed 0:d3173c8bfd48 33
seeed 0:d3173c8bfd48 34 #define POSITION_COLON 1
seeed 0:d3173c8bfd48 35
seeed 0:d3173c8bfd48 36 #define DIGIT_UNKOWN 0x08
seeed 0:d3173c8bfd48 37 #define DIGIT_NULL 0x00
seeed 0:d3173c8bfd48 38 #define DIGIT_MINUS 0x40
seeed 0:d3173c8bfd48 39
mkkharel 1:cae630b12d63 40 const uint8_t DIGIT_TABLE[] = {0x3f, 0x06, 0x5b, 0x4f, // 0000 0110 0101 1011
seeed 0:d3173c8bfd48 41 0x66, 0x6d, 0x7d, 0x07,
seeed 0:d3173c8bfd48 42 0x7f, 0x6f, 0x77, 0x7c,
mkkharel 1:cae630b12d63 43 0x39, 0x5e, 0x79, 0x71,
mkkharel 1:cae630b12d63 44 0x6d
seeed 0:d3173c8bfd48 45 }; //0~9,A,b,C,d,E,F
mkkharel 1:cae630b12d63 46 // TEST
mkkharel 1:cae630b12d63 47 //SENT 0010 1101
mkkharel 1:cae630b12d63 48 //SET
seeed 0:d3173c8bfd48 49
seeed 0:d3173c8bfd48 50 inline uint8_t conv(uint8_t n)
seeed 0:d3173c8bfd48 51 {
seeed 0:d3173c8bfd48 52 uint8_t segments;
seeed 0:d3173c8bfd48 53
seeed 0:d3173c8bfd48 54 if (n <= sizeof(DIGIT_TABLE)) {
seeed 0:d3173c8bfd48 55 segments = DIGIT_TABLE[n];
seeed 0:d3173c8bfd48 56 }else if (n == 0xFF) {
seeed 0:d3173c8bfd48 57 segments = DIGIT_NULL;
seeed 0:d3173c8bfd48 58 } else {
seeed 0:d3173c8bfd48 59 segments = DIGIT_UNKOWN;
seeed 0:d3173c8bfd48 60 }
seeed 0:d3173c8bfd48 61
seeed 0:d3173c8bfd48 62 return segments;
seeed 0:d3173c8bfd48 63 }
seeed 0:d3173c8bfd48 64
seeed 0:d3173c8bfd48 65 DigitDisplay::DigitDisplay(PinName clk, PinName dio) : _clk(clk), _dio(dio)
seeed 0:d3173c8bfd48 66 {
seeed 0:d3173c8bfd48 67 _dio.output();
seeed 0:d3173c8bfd48 68 _dio = 1;
seeed 0:d3173c8bfd48 69 _clk = 1;
seeed 0:d3173c8bfd48 70
seeed 0:d3173c8bfd48 71 _brightness = 2;
seeed 0:d3173c8bfd48 72 _colon = false;
seeed 0:d3173c8bfd48 73 _off = true;
seeed 0:d3173c8bfd48 74
seeed 0:d3173c8bfd48 75 for (uint8_t i = 0; i < sizeof(_content); i++) {
seeed 0:d3173c8bfd48 76 _content[i] = DIGIT_NULL;
seeed 0:d3173c8bfd48 77 }
seeed 0:d3173c8bfd48 78 }
seeed 0:d3173c8bfd48 79
seeed 0:d3173c8bfd48 80 void DigitDisplay::on()
seeed 0:d3173c8bfd48 81 {
seeed 0:d3173c8bfd48 82 start();
seeed 0:d3173c8bfd48 83 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 84 stop();
seeed 0:d3173c8bfd48 85 }
seeed 0:d3173c8bfd48 86
seeed 0:d3173c8bfd48 87 void DigitDisplay::off()
seeed 0:d3173c8bfd48 88 {
seeed 0:d3173c8bfd48 89 start();
seeed 0:d3173c8bfd48 90 send(0x80);
seeed 0:d3173c8bfd48 91 stop();
seeed 0:d3173c8bfd48 92 }
seeed 0:d3173c8bfd48 93
seeed 0:d3173c8bfd48 94 void DigitDisplay::setBrightness(uint8_t brightness)
seeed 0:d3173c8bfd48 95 {
seeed 0:d3173c8bfd48 96 if (brightness > 7) {
seeed 0:d3173c8bfd48 97 brightness = 7;
seeed 0:d3173c8bfd48 98 }
seeed 0:d3173c8bfd48 99
seeed 0:d3173c8bfd48 100 _brightness = brightness;
seeed 0:d3173c8bfd48 101
seeed 0:d3173c8bfd48 102 start();
seeed 0:d3173c8bfd48 103 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 104 stop();
seeed 0:d3173c8bfd48 105 }
seeed 0:d3173c8bfd48 106
seeed 0:d3173c8bfd48 107 void DigitDisplay::setColon(bool enable)
seeed 0:d3173c8bfd48 108 {
seeed 0:d3173c8bfd48 109 if (_colon != enable) {
seeed 0:d3173c8bfd48 110 _colon = enable;
seeed 0:d3173c8bfd48 111
seeed 0:d3173c8bfd48 112 if (enable) {
seeed 0:d3173c8bfd48 113 _content[POSITION_COLON] |= 0x80;
seeed 0:d3173c8bfd48 114 } else {
seeed 0:d3173c8bfd48 115 _content[POSITION_COLON] &= 0x7F;
seeed 0:d3173c8bfd48 116 }
seeed 0:d3173c8bfd48 117
seeed 0:d3173c8bfd48 118 writeRaw(POSITION_COLON, _content[POSITION_COLON]);
seeed 0:d3173c8bfd48 119 }
seeed 0:d3173c8bfd48 120 }
seeed 0:d3173c8bfd48 121
seeed 0:d3173c8bfd48 122 void DigitDisplay::write(int16_t n)
seeed 0:d3173c8bfd48 123 {
seeed 0:d3173c8bfd48 124 uint8_t negative = 0;
seeed 0:d3173c8bfd48 125
seeed 0:d3173c8bfd48 126 if (n < 0) {
seeed 0:d3173c8bfd48 127 negative = 1;
seeed 0:d3173c8bfd48 128 n = (-n) % 1000;
seeed 0:d3173c8bfd48 129 } else {
seeed 0:d3173c8bfd48 130 n = n % 10000;
seeed 0:d3173c8bfd48 131 }
seeed 0:d3173c8bfd48 132
seeed 0:d3173c8bfd48 133 int8_t i = 3;
seeed 0:d3173c8bfd48 134 do {
seeed 0:d3173c8bfd48 135 uint8_t r = n % 10;
seeed 0:d3173c8bfd48 136 _content[i] = conv(r);
seeed 0:d3173c8bfd48 137 i--;
seeed 0:d3173c8bfd48 138 n = n / 10;
seeed 0:d3173c8bfd48 139 } while (n != 0);
seeed 0:d3173c8bfd48 140
seeed 0:d3173c8bfd48 141 if (negative) {
seeed 0:d3173c8bfd48 142 _content[i] = DIGIT_MINUS;
seeed 0:d3173c8bfd48 143 i--;
seeed 0:d3173c8bfd48 144 }
seeed 0:d3173c8bfd48 145
seeed 0:d3173c8bfd48 146 for (int8_t j = 0; j <= i; j++) {
seeed 0:d3173c8bfd48 147 _content[j] = DIGIT_NULL;
seeed 0:d3173c8bfd48 148 }
seeed 0:d3173c8bfd48 149
seeed 0:d3173c8bfd48 150 if (_colon) {
seeed 0:d3173c8bfd48 151 _content[POSITION_COLON] |= 0x80;
seeed 0:d3173c8bfd48 152 }
seeed 0:d3173c8bfd48 153
seeed 0:d3173c8bfd48 154 writeRaw(_content);
seeed 0:d3173c8bfd48 155 }
seeed 0:d3173c8bfd48 156
seeed 0:d3173c8bfd48 157 void DigitDisplay::write(uint8_t numbers[])
seeed 0:d3173c8bfd48 158 {
seeed 0:d3173c8bfd48 159 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 160 _content[i] = conv(numbers[i]);
seeed 0:d3173c8bfd48 161 }
seeed 0:d3173c8bfd48 162
seeed 0:d3173c8bfd48 163 if (_colon) {
seeed 0:d3173c8bfd48 164 _content[POSITION_COLON] |= 0x80;
seeed 0:d3173c8bfd48 165 }
seeed 0:d3173c8bfd48 166
seeed 0:d3173c8bfd48 167 start();
seeed 0:d3173c8bfd48 168 send(ADDR_AUTO);
seeed 0:d3173c8bfd48 169 stop();
seeed 0:d3173c8bfd48 170 start();
seeed 0:d3173c8bfd48 171 send(0xC0);
seeed 0:d3173c8bfd48 172 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 173 send(_content[i]);
seeed 0:d3173c8bfd48 174 }
seeed 0:d3173c8bfd48 175 stop();
seeed 0:d3173c8bfd48 176
seeed 0:d3173c8bfd48 177 if (_off) {
seeed 0:d3173c8bfd48 178 _off = 0;
seeed 0:d3173c8bfd48 179 start();
seeed 0:d3173c8bfd48 180 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 181 stop();
seeed 0:d3173c8bfd48 182 }
seeed 0:d3173c8bfd48 183 }
seeed 0:d3173c8bfd48 184
seeed 0:d3173c8bfd48 185 void DigitDisplay::write(uint8_t position, uint8_t number)
seeed 0:d3173c8bfd48 186 {
seeed 0:d3173c8bfd48 187 if (position >= 4) {
seeed 0:d3173c8bfd48 188 return;
seeed 0:d3173c8bfd48 189 }
seeed 0:d3173c8bfd48 190
seeed 0:d3173c8bfd48 191 uint8_t segments = conv(number);
seeed 0:d3173c8bfd48 192
seeed 0:d3173c8bfd48 193 if ((position == POSITION_COLON) && _colon) {
seeed 0:d3173c8bfd48 194 segments |= 0x80;
seeed 0:d3173c8bfd48 195 }
seeed 0:d3173c8bfd48 196
seeed 0:d3173c8bfd48 197 _content[position] = segments;
seeed 0:d3173c8bfd48 198
seeed 0:d3173c8bfd48 199 start();
seeed 0:d3173c8bfd48 200 send(ADDR_FIXED);
seeed 0:d3173c8bfd48 201 stop();
seeed 0:d3173c8bfd48 202 start();
seeed 0:d3173c8bfd48 203 send(0xC0 | position);
seeed 0:d3173c8bfd48 204 send(segments);
seeed 0:d3173c8bfd48 205 stop();
seeed 0:d3173c8bfd48 206
seeed 0:d3173c8bfd48 207 if (_off) {
seeed 0:d3173c8bfd48 208 _off = 0;
seeed 0:d3173c8bfd48 209 start();
seeed 0:d3173c8bfd48 210 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 211 stop();
seeed 0:d3173c8bfd48 212 }
seeed 0:d3173c8bfd48 213 }
seeed 0:d3173c8bfd48 214
seeed 0:d3173c8bfd48 215 void DigitDisplay::writeRaw(uint8_t segments[])
seeed 0:d3173c8bfd48 216 {
seeed 0:d3173c8bfd48 217 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 218 _content[i] = segments[i];
seeed 0:d3173c8bfd48 219 }
seeed 0:d3173c8bfd48 220
seeed 0:d3173c8bfd48 221 start();
seeed 0:d3173c8bfd48 222 send(ADDR_AUTO);
seeed 0:d3173c8bfd48 223 stop();
seeed 0:d3173c8bfd48 224 start();
seeed 0:d3173c8bfd48 225 send(0xC0);
seeed 0:d3173c8bfd48 226 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 227 send(segments[i]);
seeed 0:d3173c8bfd48 228 }
seeed 0:d3173c8bfd48 229 stop();
seeed 0:d3173c8bfd48 230
seeed 0:d3173c8bfd48 231 if (_off) {
seeed 0:d3173c8bfd48 232 _off = 0;
seeed 0:d3173c8bfd48 233 start();
seeed 0:d3173c8bfd48 234 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 235 stop();
seeed 0:d3173c8bfd48 236 }
seeed 0:d3173c8bfd48 237 }
seeed 0:d3173c8bfd48 238
seeed 0:d3173c8bfd48 239 void DigitDisplay::writeRaw(uint8_t position, uint8_t segments)
seeed 0:d3173c8bfd48 240 {
seeed 0:d3173c8bfd48 241 if (position >= 4) {
seeed 0:d3173c8bfd48 242 return;
seeed 0:d3173c8bfd48 243 }
seeed 0:d3173c8bfd48 244
seeed 0:d3173c8bfd48 245 _content[position] = segments;
seeed 0:d3173c8bfd48 246
seeed 0:d3173c8bfd48 247 start();
seeed 0:d3173c8bfd48 248 send(ADDR_FIXED);
seeed 0:d3173c8bfd48 249 stop();
seeed 0:d3173c8bfd48 250 start();
seeed 0:d3173c8bfd48 251 send(0xC0 | position);
seeed 0:d3173c8bfd48 252 send(segments);
seeed 0:d3173c8bfd48 253 stop();
seeed 0:d3173c8bfd48 254
seeed 0:d3173c8bfd48 255 if (_off) {
seeed 0:d3173c8bfd48 256 _off = 0;
seeed 0:d3173c8bfd48 257 start();
seeed 0:d3173c8bfd48 258 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 259 stop();
seeed 0:d3173c8bfd48 260 }
seeed 0:d3173c8bfd48 261 }
seeed 0:d3173c8bfd48 262
seeed 0:d3173c8bfd48 263 void DigitDisplay::clear()
seeed 0:d3173c8bfd48 264 {
seeed 0:d3173c8bfd48 265 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 266 _content[i] = DIGIT_NULL;
seeed 0:d3173c8bfd48 267 }
seeed 0:d3173c8bfd48 268 _colon = false;
seeed 0:d3173c8bfd48 269
seeed 0:d3173c8bfd48 270 writeRaw(0, DIGIT_NULL);
seeed 0:d3173c8bfd48 271 writeRaw(1, DIGIT_NULL);
seeed 0:d3173c8bfd48 272 writeRaw(2, DIGIT_NULL);
seeed 0:d3173c8bfd48 273 writeRaw(3, DIGIT_NULL);
seeed 0:d3173c8bfd48 274 }
seeed 0:d3173c8bfd48 275
seeed 0:d3173c8bfd48 276 void DigitDisplay::start()
seeed 0:d3173c8bfd48 277 {
seeed 0:d3173c8bfd48 278 _clk = 1;
seeed 0:d3173c8bfd48 279 _dio = 1;
seeed 0:d3173c8bfd48 280 _dio = 0;
seeed 0:d3173c8bfd48 281 _clk = 0;
seeed 0:d3173c8bfd48 282 }
seeed 0:d3173c8bfd48 283
seeed 0:d3173c8bfd48 284 bool DigitDisplay::send(uint8_t data)
seeed 0:d3173c8bfd48 285 {
seeed 0:d3173c8bfd48 286 for (uint8_t i = 0; i < 8; i++) {
seeed 0:d3173c8bfd48 287 _clk = 0;
seeed 0:d3173c8bfd48 288 _dio = data & 1;
seeed 0:d3173c8bfd48 289 data >>= 1;
seeed 0:d3173c8bfd48 290 _clk = 1;
seeed 0:d3173c8bfd48 291 }
seeed 0:d3173c8bfd48 292
seeed 0:d3173c8bfd48 293 // check ack
seeed 0:d3173c8bfd48 294 _clk = 0;
seeed 0:d3173c8bfd48 295 _dio = 1;
seeed 0:d3173c8bfd48 296 _clk = 1;
seeed 0:d3173c8bfd48 297 _dio.input();
seeed 0:d3173c8bfd48 298
seeed 0:d3173c8bfd48 299 uint16_t count = 0;
seeed 0:d3173c8bfd48 300 while (_dio) {
seeed 0:d3173c8bfd48 301 count++;
seeed 0:d3173c8bfd48 302 if (count >= 200) {
seeed 0:d3173c8bfd48 303 _dio.output();
seeed 0:d3173c8bfd48 304 return false;
seeed 0:d3173c8bfd48 305 }
seeed 0:d3173c8bfd48 306 }
seeed 0:d3173c8bfd48 307
seeed 0:d3173c8bfd48 308 _dio.output();
seeed 0:d3173c8bfd48 309 return true;
seeed 0:d3173c8bfd48 310 }
seeed 0:d3173c8bfd48 311
seeed 0:d3173c8bfd48 312 void DigitDisplay::stop()
seeed 0:d3173c8bfd48 313 {
seeed 0:d3173c8bfd48 314 _clk = 0;
seeed 0:d3173c8bfd48 315 _dio = 0;
seeed 0:d3173c8bfd48 316 _clk = 1;
seeed 0:d3173c8bfd48 317 _dio = 1;
seeed 0:d3173c8bfd48 318 }
seeed 0:d3173c8bfd48 319
seeed 0:d3173c8bfd48 320