pp

Dependencies:   mbed

Committer:
NamSom
Date:
Sat Dec 05 14:01:16 2015 +0000
Revision:
1:ba7154d2d7ca
Parent:
0:b2064b3558b6
Child:
2:6b4dda997dc0
qqq

Who changed what in which revision?

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