library for Seeed's Grove - 4 Digit Display

Dependents:   SeeedStudio_Grove_Sensors_Demo thermostat_fan_demo--fan SeeedStudio_Grove_Sensors_Demo microbit-8-ultrasonic-meter ... more

Fork of DigitDisplay by Seeed Studio

Committer:
seeed
Date:
Sat Feb 08 05:42:09 2014 +0000
Revision:
0:d3173c8bfd48
initial

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
seeed 0:d3173c8bfd48 40 const uint8_t DIGIT_TABLE[] = {0x3f, 0x06, 0x5b, 0x4f,
seeed 0:d3173c8bfd48 41 0x66, 0x6d, 0x7d, 0x07,
seeed 0:d3173c8bfd48 42 0x7f, 0x6f, 0x77, 0x7c,
seeed 0:d3173c8bfd48 43 0x39, 0x5e, 0x79, 0x71
seeed 0:d3173c8bfd48 44 }; //0~9,A,b,C,d,E,F
seeed 0:d3173c8bfd48 45
seeed 0:d3173c8bfd48 46
seeed 0:d3173c8bfd48 47 inline uint8_t conv(uint8_t n)
seeed 0:d3173c8bfd48 48 {
seeed 0:d3173c8bfd48 49 uint8_t segments;
seeed 0:d3173c8bfd48 50
seeed 0:d3173c8bfd48 51 if (n <= sizeof(DIGIT_TABLE)) {
seeed 0:d3173c8bfd48 52 segments = DIGIT_TABLE[n];
seeed 0:d3173c8bfd48 53 }else if (n == 0xFF) {
seeed 0:d3173c8bfd48 54 segments = DIGIT_NULL;
seeed 0:d3173c8bfd48 55 } else {
seeed 0:d3173c8bfd48 56 segments = DIGIT_UNKOWN;
seeed 0:d3173c8bfd48 57 }
seeed 0:d3173c8bfd48 58
seeed 0:d3173c8bfd48 59 return segments;
seeed 0:d3173c8bfd48 60 }
seeed 0:d3173c8bfd48 61
seeed 0:d3173c8bfd48 62 DigitDisplay::DigitDisplay(PinName clk, PinName dio) : _clk(clk), _dio(dio)
seeed 0:d3173c8bfd48 63 {
seeed 0:d3173c8bfd48 64 _dio.output();
seeed 0:d3173c8bfd48 65 _dio = 1;
seeed 0:d3173c8bfd48 66 _clk = 1;
seeed 0:d3173c8bfd48 67
seeed 0:d3173c8bfd48 68 _brightness = 2;
seeed 0:d3173c8bfd48 69 _colon = false;
seeed 0:d3173c8bfd48 70 _off = true;
seeed 0:d3173c8bfd48 71
seeed 0:d3173c8bfd48 72 for (uint8_t i = 0; i < sizeof(_content); i++) {
seeed 0:d3173c8bfd48 73 _content[i] = DIGIT_NULL;
seeed 0:d3173c8bfd48 74 }
seeed 0:d3173c8bfd48 75 }
seeed 0:d3173c8bfd48 76
seeed 0:d3173c8bfd48 77 void DigitDisplay::on()
seeed 0:d3173c8bfd48 78 {
seeed 0:d3173c8bfd48 79 start();
seeed 0:d3173c8bfd48 80 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 81 stop();
seeed 0:d3173c8bfd48 82 }
seeed 0:d3173c8bfd48 83
seeed 0:d3173c8bfd48 84 void DigitDisplay::off()
seeed 0:d3173c8bfd48 85 {
seeed 0:d3173c8bfd48 86 start();
seeed 0:d3173c8bfd48 87 send(0x80);
seeed 0:d3173c8bfd48 88 stop();
seeed 0:d3173c8bfd48 89 }
seeed 0:d3173c8bfd48 90
seeed 0:d3173c8bfd48 91 void DigitDisplay::setBrightness(uint8_t brightness)
seeed 0:d3173c8bfd48 92 {
seeed 0:d3173c8bfd48 93 if (brightness > 7) {
seeed 0:d3173c8bfd48 94 brightness = 7;
seeed 0:d3173c8bfd48 95 }
seeed 0:d3173c8bfd48 96
seeed 0:d3173c8bfd48 97 _brightness = brightness;
seeed 0:d3173c8bfd48 98
seeed 0:d3173c8bfd48 99 start();
seeed 0:d3173c8bfd48 100 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 101 stop();
seeed 0:d3173c8bfd48 102 }
seeed 0:d3173c8bfd48 103
seeed 0:d3173c8bfd48 104 void DigitDisplay::setColon(bool enable)
seeed 0:d3173c8bfd48 105 {
seeed 0:d3173c8bfd48 106 if (_colon != enable) {
seeed 0:d3173c8bfd48 107 _colon = enable;
seeed 0:d3173c8bfd48 108
seeed 0:d3173c8bfd48 109 if (enable) {
seeed 0:d3173c8bfd48 110 _content[POSITION_COLON] |= 0x80;
seeed 0:d3173c8bfd48 111 } else {
seeed 0:d3173c8bfd48 112 _content[POSITION_COLON] &= 0x7F;
seeed 0:d3173c8bfd48 113 }
seeed 0:d3173c8bfd48 114
seeed 0:d3173c8bfd48 115 writeRaw(POSITION_COLON, _content[POSITION_COLON]);
seeed 0:d3173c8bfd48 116 }
seeed 0:d3173c8bfd48 117 }
seeed 0:d3173c8bfd48 118
seeed 0:d3173c8bfd48 119 void DigitDisplay::write(int16_t n)
seeed 0:d3173c8bfd48 120 {
seeed 0:d3173c8bfd48 121 uint8_t negative = 0;
seeed 0:d3173c8bfd48 122
seeed 0:d3173c8bfd48 123 if (n < 0) {
seeed 0:d3173c8bfd48 124 negative = 1;
seeed 0:d3173c8bfd48 125 n = (-n) % 1000;
seeed 0:d3173c8bfd48 126 } else {
seeed 0:d3173c8bfd48 127 n = n % 10000;
seeed 0:d3173c8bfd48 128 }
seeed 0:d3173c8bfd48 129
seeed 0:d3173c8bfd48 130 int8_t i = 3;
seeed 0:d3173c8bfd48 131 do {
seeed 0:d3173c8bfd48 132 uint8_t r = n % 10;
seeed 0:d3173c8bfd48 133 _content[i] = conv(r);
seeed 0:d3173c8bfd48 134 i--;
seeed 0:d3173c8bfd48 135 n = n / 10;
seeed 0:d3173c8bfd48 136 } while (n != 0);
seeed 0:d3173c8bfd48 137
seeed 0:d3173c8bfd48 138 if (negative) {
seeed 0:d3173c8bfd48 139 _content[i] = DIGIT_MINUS;
seeed 0:d3173c8bfd48 140 i--;
seeed 0:d3173c8bfd48 141 }
seeed 0:d3173c8bfd48 142
seeed 0:d3173c8bfd48 143 for (int8_t j = 0; j <= i; j++) {
seeed 0:d3173c8bfd48 144 _content[j] = DIGIT_NULL;
seeed 0:d3173c8bfd48 145 }
seeed 0:d3173c8bfd48 146
seeed 0:d3173c8bfd48 147 if (_colon) {
seeed 0:d3173c8bfd48 148 _content[POSITION_COLON] |= 0x80;
seeed 0:d3173c8bfd48 149 }
seeed 0:d3173c8bfd48 150
seeed 0:d3173c8bfd48 151 writeRaw(_content);
seeed 0:d3173c8bfd48 152 }
seeed 0:d3173c8bfd48 153
seeed 0:d3173c8bfd48 154 void DigitDisplay::write(uint8_t numbers[])
seeed 0:d3173c8bfd48 155 {
seeed 0:d3173c8bfd48 156 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 157 _content[i] = conv(numbers[i]);
seeed 0:d3173c8bfd48 158 }
seeed 0:d3173c8bfd48 159
seeed 0:d3173c8bfd48 160 if (_colon) {
seeed 0:d3173c8bfd48 161 _content[POSITION_COLON] |= 0x80;
seeed 0:d3173c8bfd48 162 }
seeed 0:d3173c8bfd48 163
seeed 0:d3173c8bfd48 164 start();
seeed 0:d3173c8bfd48 165 send(ADDR_AUTO);
seeed 0:d3173c8bfd48 166 stop();
seeed 0:d3173c8bfd48 167 start();
seeed 0:d3173c8bfd48 168 send(0xC0);
seeed 0:d3173c8bfd48 169 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 170 send(_content[i]);
seeed 0:d3173c8bfd48 171 }
seeed 0:d3173c8bfd48 172 stop();
seeed 0:d3173c8bfd48 173
seeed 0:d3173c8bfd48 174 if (_off) {
seeed 0:d3173c8bfd48 175 _off = 0;
seeed 0:d3173c8bfd48 176 start();
seeed 0:d3173c8bfd48 177 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 178 stop();
seeed 0:d3173c8bfd48 179 }
seeed 0:d3173c8bfd48 180 }
seeed 0:d3173c8bfd48 181
seeed 0:d3173c8bfd48 182 void DigitDisplay::write(uint8_t position, uint8_t number)
seeed 0:d3173c8bfd48 183 {
seeed 0:d3173c8bfd48 184 if (position >= 4) {
seeed 0:d3173c8bfd48 185 return;
seeed 0:d3173c8bfd48 186 }
seeed 0:d3173c8bfd48 187
seeed 0:d3173c8bfd48 188 uint8_t segments = conv(number);
seeed 0:d3173c8bfd48 189
seeed 0:d3173c8bfd48 190 if ((position == POSITION_COLON) && _colon) {
seeed 0:d3173c8bfd48 191 segments |= 0x80;
seeed 0:d3173c8bfd48 192 }
seeed 0:d3173c8bfd48 193
seeed 0:d3173c8bfd48 194 _content[position] = segments;
seeed 0:d3173c8bfd48 195
seeed 0:d3173c8bfd48 196 start();
seeed 0:d3173c8bfd48 197 send(ADDR_FIXED);
seeed 0:d3173c8bfd48 198 stop();
seeed 0:d3173c8bfd48 199 start();
seeed 0:d3173c8bfd48 200 send(0xC0 | position);
seeed 0:d3173c8bfd48 201 send(segments);
seeed 0:d3173c8bfd48 202 stop();
seeed 0:d3173c8bfd48 203
seeed 0:d3173c8bfd48 204 if (_off) {
seeed 0:d3173c8bfd48 205 _off = 0;
seeed 0:d3173c8bfd48 206 start();
seeed 0:d3173c8bfd48 207 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 208 stop();
seeed 0:d3173c8bfd48 209 }
seeed 0:d3173c8bfd48 210 }
seeed 0:d3173c8bfd48 211
seeed 0:d3173c8bfd48 212 void DigitDisplay::writeRaw(uint8_t segments[])
seeed 0:d3173c8bfd48 213 {
seeed 0:d3173c8bfd48 214 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 215 _content[i] = segments[i];
seeed 0:d3173c8bfd48 216 }
seeed 0:d3173c8bfd48 217
seeed 0:d3173c8bfd48 218 start();
seeed 0:d3173c8bfd48 219 send(ADDR_AUTO);
seeed 0:d3173c8bfd48 220 stop();
seeed 0:d3173c8bfd48 221 start();
seeed 0:d3173c8bfd48 222 send(0xC0);
seeed 0:d3173c8bfd48 223 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 224 send(segments[i]);
seeed 0:d3173c8bfd48 225 }
seeed 0:d3173c8bfd48 226 stop();
seeed 0:d3173c8bfd48 227
seeed 0:d3173c8bfd48 228 if (_off) {
seeed 0:d3173c8bfd48 229 _off = 0;
seeed 0:d3173c8bfd48 230 start();
seeed 0:d3173c8bfd48 231 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 232 stop();
seeed 0:d3173c8bfd48 233 }
seeed 0:d3173c8bfd48 234 }
seeed 0:d3173c8bfd48 235
seeed 0:d3173c8bfd48 236 void DigitDisplay::writeRaw(uint8_t position, uint8_t segments)
seeed 0:d3173c8bfd48 237 {
seeed 0:d3173c8bfd48 238 if (position >= 4) {
seeed 0:d3173c8bfd48 239 return;
seeed 0:d3173c8bfd48 240 }
seeed 0:d3173c8bfd48 241
seeed 0:d3173c8bfd48 242 _content[position] = segments;
seeed 0:d3173c8bfd48 243
seeed 0:d3173c8bfd48 244 start();
seeed 0:d3173c8bfd48 245 send(ADDR_FIXED);
seeed 0:d3173c8bfd48 246 stop();
seeed 0:d3173c8bfd48 247 start();
seeed 0:d3173c8bfd48 248 send(0xC0 | position);
seeed 0:d3173c8bfd48 249 send(segments);
seeed 0:d3173c8bfd48 250 stop();
seeed 0:d3173c8bfd48 251
seeed 0:d3173c8bfd48 252 if (_off) {
seeed 0:d3173c8bfd48 253 _off = 0;
seeed 0:d3173c8bfd48 254 start();
seeed 0:d3173c8bfd48 255 send(0x88 | _brightness);
seeed 0:d3173c8bfd48 256 stop();
seeed 0:d3173c8bfd48 257 }
seeed 0:d3173c8bfd48 258 }
seeed 0:d3173c8bfd48 259
seeed 0:d3173c8bfd48 260 void DigitDisplay::clear()
seeed 0:d3173c8bfd48 261 {
seeed 0:d3173c8bfd48 262 for (uint8_t i = 0; i < 4; i++) {
seeed 0:d3173c8bfd48 263 _content[i] = DIGIT_NULL;
seeed 0:d3173c8bfd48 264 }
seeed 0:d3173c8bfd48 265 _colon = false;
seeed 0:d3173c8bfd48 266
seeed 0:d3173c8bfd48 267 writeRaw(0, DIGIT_NULL);
seeed 0:d3173c8bfd48 268 writeRaw(1, DIGIT_NULL);
seeed 0:d3173c8bfd48 269 writeRaw(2, DIGIT_NULL);
seeed 0:d3173c8bfd48 270 writeRaw(3, DIGIT_NULL);
seeed 0:d3173c8bfd48 271 }
seeed 0:d3173c8bfd48 272
seeed 0:d3173c8bfd48 273 void DigitDisplay::start()
seeed 0:d3173c8bfd48 274 {
seeed 0:d3173c8bfd48 275 _clk = 1;
seeed 0:d3173c8bfd48 276 _dio = 1;
seeed 0:d3173c8bfd48 277 _dio = 0;
seeed 0:d3173c8bfd48 278 _clk = 0;
seeed 0:d3173c8bfd48 279 }
seeed 0:d3173c8bfd48 280
seeed 0:d3173c8bfd48 281 bool DigitDisplay::send(uint8_t data)
seeed 0:d3173c8bfd48 282 {
seeed 0:d3173c8bfd48 283 for (uint8_t i = 0; i < 8; i++) {
seeed 0:d3173c8bfd48 284 _clk = 0;
seeed 0:d3173c8bfd48 285 _dio = data & 1;
seeed 0:d3173c8bfd48 286 data >>= 1;
seeed 0:d3173c8bfd48 287 _clk = 1;
seeed 0:d3173c8bfd48 288 }
seeed 0:d3173c8bfd48 289
seeed 0:d3173c8bfd48 290 // check ack
seeed 0:d3173c8bfd48 291 _clk = 0;
seeed 0:d3173c8bfd48 292 _dio = 1;
seeed 0:d3173c8bfd48 293 _clk = 1;
seeed 0:d3173c8bfd48 294 _dio.input();
seeed 0:d3173c8bfd48 295
seeed 0:d3173c8bfd48 296 uint16_t count = 0;
seeed 0:d3173c8bfd48 297 while (_dio) {
seeed 0:d3173c8bfd48 298 count++;
seeed 0:d3173c8bfd48 299 if (count >= 200) {
seeed 0:d3173c8bfd48 300 _dio.output();
seeed 0:d3173c8bfd48 301 return false;
seeed 0:d3173c8bfd48 302 }
seeed 0:d3173c8bfd48 303 }
seeed 0:d3173c8bfd48 304
seeed 0:d3173c8bfd48 305 _dio.output();
seeed 0:d3173c8bfd48 306 return true;
seeed 0:d3173c8bfd48 307 }
seeed 0:d3173c8bfd48 308
seeed 0:d3173c8bfd48 309 void DigitDisplay::stop()
seeed 0:d3173c8bfd48 310 {
seeed 0:d3173c8bfd48 311 _clk = 0;
seeed 0:d3173c8bfd48 312 _dio = 0;
seeed 0:d3173c8bfd48 313 _clk = 1;
seeed 0:d3173c8bfd48 314 _dio = 1;
seeed 0:d3173c8bfd48 315 }
seeed 0:d3173c8bfd48 316
seeed 0:d3173c8bfd48 317