pp
Dependencies: mbed
DigitDisplay.cpp@5:60262753a05f, 2015-12-09 (annotated)
- Committer:
- Peeraya
- Date:
- Wed Dec 09 02:09:36 2015 +0000
- Revision:
- 5:60262753a05f
- Parent:
- 4:a61cdd4e7afa
Super Cute Alarm Clock
Who changed what in which revision?
User | Revision | Line number | New 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 |
Peeraya | 0:b2064b3558b6 | 39 | |
Peeraya | 0:b2064b3558b6 | 40 | const uint8_t DIGIT_TABLE[] = {0x3f, 0x06, 0x5b, 0x4f, |
Peeraya | 0:b2064b3558b6 | 41 | 0x66, 0x6d, 0x7d, 0x07, |
Peeraya | 0:b2064b3558b6 | 42 | 0x7f, 0x6f, 0x77, 0x7c, |
Peeraya | 0:b2064b3558b6 | 43 | 0x39, 0x5e, 0x79, 0x71 |
Peeraya | 0:b2064b3558b6 | 44 | }; //0~9,A,b,C,d,E,F |
Peeraya | 0:b2064b3558b6 | 45 | |
Peeraya | 0:b2064b3558b6 | 46 | |
Peeraya | 0:b2064b3558b6 | 47 | inline uint8_t conv(uint8_t n) |
Peeraya | 0:b2064b3558b6 | 48 | { |
Peeraya | 0:b2064b3558b6 | 49 | uint8_t segments; |
Peeraya | 0:b2064b3558b6 | 50 | |
Peeraya | 0:b2064b3558b6 | 51 | if (n <= sizeof(DIGIT_TABLE)) { |
Peeraya | 0:b2064b3558b6 | 52 | segments = DIGIT_TABLE[n]; |
Peeraya | 0:b2064b3558b6 | 53 | }else if (n == 0xFF) { |
Peeraya | 0:b2064b3558b6 | 54 | segments = DIGIT_NULL; |
Peeraya | 0:b2064b3558b6 | 55 | } else { |
Peeraya | 0:b2064b3558b6 | 56 | segments = DIGIT_UNKOWN; |
Peeraya | 0:b2064b3558b6 | 57 | } |
Peeraya | 0:b2064b3558b6 | 58 | |
Peeraya | 0:b2064b3558b6 | 59 | return segments; |
Peeraya | 0:b2064b3558b6 | 60 | } |
Peeraya | 0:b2064b3558b6 | 61 | |
Peeraya | 0:b2064b3558b6 | 62 | DigitDisplay::DigitDisplay(PinName clk, PinName dio) : _clk(clk), _dio(dio) |
Peeraya | 0:b2064b3558b6 | 63 | { |
Peeraya | 0:b2064b3558b6 | 64 | _dio.output(); |
Peeraya | 0:b2064b3558b6 | 65 | _dio = 1; |
Peeraya | 0:b2064b3558b6 | 66 | _clk = 1; |
Peeraya | 0:b2064b3558b6 | 67 | |
Peeraya | 0:b2064b3558b6 | 68 | _brightness = 2; |
Peeraya | 0:b2064b3558b6 | 69 | _colon = false; |
Peeraya | 0:b2064b3558b6 | 70 | _off = true; |
Peeraya | 0:b2064b3558b6 | 71 | |
Peeraya | 0:b2064b3558b6 | 72 | for (uint8_t i = 0; i < sizeof(_content); i++) { |
Peeraya | 0:b2064b3558b6 | 73 | _content[i] = DIGIT_NULL; |
Peeraya | 0:b2064b3558b6 | 74 | } |
NamSom | 1:ba7154d2d7ca | 75 | buttonState1 = 0; |
NamSom | 1:ba7154d2d7ca | 76 | buttonState2 = 0; |
NamSom | 1:ba7154d2d7ca | 77 | buttonState3 = 0; |
Peeraya | 2:6b4dda997dc0 | 78 | prebuttonState1 = 0; |
Peeraya | 2:6b4dda997dc0 | 79 | prebuttonState2 = 0; |
Peeraya | 2:6b4dda997dc0 | 80 | prebuttonState3 = 0; |
Peeraya | 0:b2064b3558b6 | 81 | } |
Peeraya | 0:b2064b3558b6 | 82 | |
Peeraya | 0:b2064b3558b6 | 83 | void DigitDisplay::on() |
Peeraya | 0:b2064b3558b6 | 84 | { |
Peeraya | 0:b2064b3558b6 | 85 | start(); |
Peeraya | 0:b2064b3558b6 | 86 | send(0x88 | _brightness); |
Peeraya | 0:b2064b3558b6 | 87 | stop(); |
Peeraya | 0:b2064b3558b6 | 88 | } |
Peeraya | 0:b2064b3558b6 | 89 | |
Peeraya | 0:b2064b3558b6 | 90 | void DigitDisplay::off() |
Peeraya | 0:b2064b3558b6 | 91 | { |
Peeraya | 0:b2064b3558b6 | 92 | start(); |
Peeraya | 0:b2064b3558b6 | 93 | send(0x80); |
Peeraya | 0:b2064b3558b6 | 94 | stop(); |
Peeraya | 0:b2064b3558b6 | 95 | } |
Peeraya | 0:b2064b3558b6 | 96 | |
Peeraya | 0:b2064b3558b6 | 97 | void DigitDisplay::setBrightness(uint8_t brightness) |
Peeraya | 0:b2064b3558b6 | 98 | { |
Peeraya | 0:b2064b3558b6 | 99 | if (brightness > 7) { |
Peeraya | 0:b2064b3558b6 | 100 | brightness = 7; |
Peeraya | 0:b2064b3558b6 | 101 | } |
Peeraya | 0:b2064b3558b6 | 102 | |
Peeraya | 0:b2064b3558b6 | 103 | _brightness = brightness; |
Peeraya | 0:b2064b3558b6 | 104 | |
Peeraya | 0:b2064b3558b6 | 105 | start(); |
Peeraya | 0:b2064b3558b6 | 106 | send(0x88 | _brightness); |
Peeraya | 0:b2064b3558b6 | 107 | stop(); |
Peeraya | 0:b2064b3558b6 | 108 | } |
Peeraya | 0:b2064b3558b6 | 109 | |
Peeraya | 0:b2064b3558b6 | 110 | void DigitDisplay::setColon(bool enable) |
Peeraya | 0:b2064b3558b6 | 111 | { |
Peeraya | 0:b2064b3558b6 | 112 | if (_colon != enable) { |
Peeraya | 0:b2064b3558b6 | 113 | _colon = enable; |
Peeraya | 0:b2064b3558b6 | 114 | |
Peeraya | 0:b2064b3558b6 | 115 | if (enable) { |
Peeraya | 0:b2064b3558b6 | 116 | _content[POSITION_COLON] |= 0x80; |
Peeraya | 0:b2064b3558b6 | 117 | } else { |
Peeraya | 0:b2064b3558b6 | 118 | _content[POSITION_COLON] &= 0x7F; |
Peeraya | 0:b2064b3558b6 | 119 | } |
Peeraya | 0:b2064b3558b6 | 120 | |
Peeraya | 0:b2064b3558b6 | 121 | writeRaw(POSITION_COLON, _content[POSITION_COLON]); |
Peeraya | 0:b2064b3558b6 | 122 | } |
Peeraya | 0:b2064b3558b6 | 123 | } |
Peeraya | 0:b2064b3558b6 | 124 | |
Peeraya | 0:b2064b3558b6 | 125 | void DigitDisplay::write(int16_t n) |
Peeraya | 0:b2064b3558b6 | 126 | { |
Peeraya | 0:b2064b3558b6 | 127 | uint8_t negative = 0; |
Peeraya | 0:b2064b3558b6 | 128 | |
Peeraya | 0:b2064b3558b6 | 129 | if (n < 0) { |
Peeraya | 0:b2064b3558b6 | 130 | negative = 1; |
Peeraya | 0:b2064b3558b6 | 131 | n = (-n) % 1000; |
Peeraya | 0:b2064b3558b6 | 132 | } else { |
Peeraya | 0:b2064b3558b6 | 133 | n = n % 10000; |
Peeraya | 0:b2064b3558b6 | 134 | } |
Peeraya | 0:b2064b3558b6 | 135 | |
Peeraya | 0:b2064b3558b6 | 136 | int8_t i = 3; |
Peeraya | 0:b2064b3558b6 | 137 | do { |
Peeraya | 0:b2064b3558b6 | 138 | uint8_t r = n % 10; |
Peeraya | 0:b2064b3558b6 | 139 | _content[i] = conv(r); |
Peeraya | 0:b2064b3558b6 | 140 | i--; |
Peeraya | 0:b2064b3558b6 | 141 | n = n / 10; |
Peeraya | 0:b2064b3558b6 | 142 | } while (n != 0); |
Peeraya | 0:b2064b3558b6 | 143 | |
Peeraya | 0:b2064b3558b6 | 144 | if (negative) { |
Peeraya | 0:b2064b3558b6 | 145 | _content[i] = DIGIT_MINUS; |
Peeraya | 0:b2064b3558b6 | 146 | i--; |
Peeraya | 0:b2064b3558b6 | 147 | } |
Peeraya | 0:b2064b3558b6 | 148 | |
Peeraya | 0:b2064b3558b6 | 149 | for (int8_t j = 0; j <= i; j++) { |
Peeraya | 0:b2064b3558b6 | 150 | _content[j] = DIGIT_NULL; |
Peeraya | 0:b2064b3558b6 | 151 | } |
Peeraya | 0:b2064b3558b6 | 152 | |
Peeraya | 0:b2064b3558b6 | 153 | if (_colon) { |
Peeraya | 0:b2064b3558b6 | 154 | _content[POSITION_COLON] |= 0x80; |
Peeraya | 0:b2064b3558b6 | 155 | } |
Peeraya | 0:b2064b3558b6 | 156 | |
Peeraya | 0:b2064b3558b6 | 157 | writeRaw(_content); |
Peeraya | 0:b2064b3558b6 | 158 | } |
Peeraya | 0:b2064b3558b6 | 159 | |
Peeraya | 0:b2064b3558b6 | 160 | void DigitDisplay::write(uint8_t numbers[]) |
Peeraya | 0:b2064b3558b6 | 161 | { |
Peeraya | 0:b2064b3558b6 | 162 | for (uint8_t i = 0; i < 4; i++) { |
Peeraya | 0:b2064b3558b6 | 163 | _content[i] = conv(numbers[i]); |
Peeraya | 0:b2064b3558b6 | 164 | } |
Peeraya | 0:b2064b3558b6 | 165 | |
Peeraya | 0:b2064b3558b6 | 166 | if (_colon) { |
Peeraya | 0:b2064b3558b6 | 167 | _content[POSITION_COLON] |= 0x80; |
Peeraya | 0:b2064b3558b6 | 168 | } |
Peeraya | 0:b2064b3558b6 | 169 | |
Peeraya | 0:b2064b3558b6 | 170 | start(); |
Peeraya | 0:b2064b3558b6 | 171 | send(ADDR_AUTO); |
Peeraya | 0:b2064b3558b6 | 172 | stop(); |
Peeraya | 0:b2064b3558b6 | 173 | start(); |
Peeraya | 0:b2064b3558b6 | 174 | send(0xC0); |
Peeraya | 0:b2064b3558b6 | 175 | for (uint8_t i = 0; i < 4; i++) { |
Peeraya | 0:b2064b3558b6 | 176 | send(_content[i]); |
Peeraya | 0:b2064b3558b6 | 177 | } |
Peeraya | 0:b2064b3558b6 | 178 | stop(); |
Peeraya | 0:b2064b3558b6 | 179 | |
Peeraya | 0:b2064b3558b6 | 180 | if (_off) { |
Peeraya | 0:b2064b3558b6 | 181 | _off = 0; |
Peeraya | 0:b2064b3558b6 | 182 | start(); |
Peeraya | 0:b2064b3558b6 | 183 | send(0x88 | _brightness); |
Peeraya | 0:b2064b3558b6 | 184 | stop(); |
Peeraya | 0:b2064b3558b6 | 185 | } |
Peeraya | 0:b2064b3558b6 | 186 | } |
Peeraya | 0:b2064b3558b6 | 187 | |
Peeraya | 0:b2064b3558b6 | 188 | void DigitDisplay::write(uint8_t position, uint8_t number) |
Peeraya | 0:b2064b3558b6 | 189 | { |
Peeraya | 0:b2064b3558b6 | 190 | if (position >= 4) { |
Peeraya | 0:b2064b3558b6 | 191 | return; |
Peeraya | 0:b2064b3558b6 | 192 | } |
Peeraya | 0:b2064b3558b6 | 193 | |
Peeraya | 0:b2064b3558b6 | 194 | uint8_t segments = conv(number); |
Peeraya | 0:b2064b3558b6 | 195 | |
Peeraya | 0:b2064b3558b6 | 196 | if ((position == POSITION_COLON) && _colon) { |
Peeraya | 0:b2064b3558b6 | 197 | segments |= 0x80; |
Peeraya | 0:b2064b3558b6 | 198 | } |
Peeraya | 0:b2064b3558b6 | 199 | |
Peeraya | 0:b2064b3558b6 | 200 | _content[position] = segments; |
Peeraya | 0:b2064b3558b6 | 201 | |
Peeraya | 0:b2064b3558b6 | 202 | start(); |
Peeraya | 0:b2064b3558b6 | 203 | send(ADDR_FIXED); |
Peeraya | 0:b2064b3558b6 | 204 | stop(); |
Peeraya | 0:b2064b3558b6 | 205 | start(); |
Peeraya | 0:b2064b3558b6 | 206 | send(0xC0 | position); |
Peeraya | 0:b2064b3558b6 | 207 | send(segments); |
Peeraya | 0:b2064b3558b6 | 208 | stop(); |
Peeraya | 0:b2064b3558b6 | 209 | |
Peeraya | 0:b2064b3558b6 | 210 | if (_off) { |
Peeraya | 0:b2064b3558b6 | 211 | _off = 0; |
Peeraya | 0:b2064b3558b6 | 212 | start(); |
Peeraya | 0:b2064b3558b6 | 213 | send(0x88 | _brightness); |
Peeraya | 0:b2064b3558b6 | 214 | stop(); |
Peeraya | 0:b2064b3558b6 | 215 | } |
Peeraya | 0:b2064b3558b6 | 216 | } |
Peeraya | 0:b2064b3558b6 | 217 | |
Peeraya | 0:b2064b3558b6 | 218 | void DigitDisplay::writeRaw(uint8_t segments[]) |
Peeraya | 0:b2064b3558b6 | 219 | { |
Peeraya | 0:b2064b3558b6 | 220 | for (uint8_t i = 0; i < 4; i++) { |
Peeraya | 0:b2064b3558b6 | 221 | _content[i] = segments[i]; |
Peeraya | 0:b2064b3558b6 | 222 | } |
Peeraya | 0:b2064b3558b6 | 223 | |
Peeraya | 0:b2064b3558b6 | 224 | start(); |
Peeraya | 0:b2064b3558b6 | 225 | send(ADDR_AUTO); |
Peeraya | 0:b2064b3558b6 | 226 | stop(); |
Peeraya | 0:b2064b3558b6 | 227 | start(); |
Peeraya | 0:b2064b3558b6 | 228 | send(0xC0); |
Peeraya | 0:b2064b3558b6 | 229 | for (uint8_t i = 0; i < 4; i++) { |
Peeraya | 0:b2064b3558b6 | 230 | send(segments[i]); |
Peeraya | 0:b2064b3558b6 | 231 | } |
Peeraya | 0:b2064b3558b6 | 232 | stop(); |
Peeraya | 0:b2064b3558b6 | 233 | |
Peeraya | 0:b2064b3558b6 | 234 | if (_off) { |
Peeraya | 0:b2064b3558b6 | 235 | _off = 0; |
Peeraya | 0:b2064b3558b6 | 236 | start(); |
Peeraya | 0:b2064b3558b6 | 237 | send(0x88 | _brightness); |
Peeraya | 0:b2064b3558b6 | 238 | stop(); |
Peeraya | 0:b2064b3558b6 | 239 | } |
Peeraya | 0:b2064b3558b6 | 240 | } |
Peeraya | 0:b2064b3558b6 | 241 | |
Peeraya | 0:b2064b3558b6 | 242 | void DigitDisplay::writeRaw(uint8_t position, uint8_t segments) |
Peeraya | 0:b2064b3558b6 | 243 | { |
Peeraya | 0:b2064b3558b6 | 244 | if (position >= 4) { |
Peeraya | 0:b2064b3558b6 | 245 | return; |
Peeraya | 0:b2064b3558b6 | 246 | } |
Peeraya | 0:b2064b3558b6 | 247 | |
Peeraya | 0:b2064b3558b6 | 248 | _content[position] = segments; |
Peeraya | 0:b2064b3558b6 | 249 | |
Peeraya | 0:b2064b3558b6 | 250 | start(); |
Peeraya | 0:b2064b3558b6 | 251 | send(ADDR_FIXED); |
Peeraya | 0:b2064b3558b6 | 252 | stop(); |
Peeraya | 0:b2064b3558b6 | 253 | start(); |
Peeraya | 0:b2064b3558b6 | 254 | send(0xC0 | position); |
Peeraya | 0:b2064b3558b6 | 255 | send(segments); |
Peeraya | 0:b2064b3558b6 | 256 | stop(); |
Peeraya | 0:b2064b3558b6 | 257 | |
Peeraya | 0:b2064b3558b6 | 258 | if (_off) { |
Peeraya | 0:b2064b3558b6 | 259 | _off = 0; |
Peeraya | 0:b2064b3558b6 | 260 | start(); |
Peeraya | 0:b2064b3558b6 | 261 | send(0x88 | _brightness); |
Peeraya | 0:b2064b3558b6 | 262 | stop(); |
Peeraya | 0:b2064b3558b6 | 263 | } |
Peeraya | 0:b2064b3558b6 | 264 | } |
Peeraya | 0:b2064b3558b6 | 265 | |
Peeraya | 0:b2064b3558b6 | 266 | void DigitDisplay::clear() |
Peeraya | 0:b2064b3558b6 | 267 | { |
Peeraya | 0:b2064b3558b6 | 268 | for (uint8_t i = 0; i < 4; i++) { |
Peeraya | 0:b2064b3558b6 | 269 | _content[i] = DIGIT_NULL; |
Peeraya | 0:b2064b3558b6 | 270 | } |
Peeraya | 0:b2064b3558b6 | 271 | _colon = false; |
Peeraya | 0:b2064b3558b6 | 272 | |
Peeraya | 0:b2064b3558b6 | 273 | writeRaw(0, DIGIT_NULL); |
Peeraya | 0:b2064b3558b6 | 274 | writeRaw(1, DIGIT_NULL); |
Peeraya | 0:b2064b3558b6 | 275 | writeRaw(2, DIGIT_NULL); |
Peeraya | 0:b2064b3558b6 | 276 | writeRaw(3, DIGIT_NULL); |
Peeraya | 0:b2064b3558b6 | 277 | } |
Peeraya | 0:b2064b3558b6 | 278 | |
Peeraya | 0:b2064b3558b6 | 279 | void DigitDisplay::start() |
Peeraya | 0:b2064b3558b6 | 280 | { |
Peeraya | 0:b2064b3558b6 | 281 | _clk = 1; |
Peeraya | 0:b2064b3558b6 | 282 | _dio = 1; |
Peeraya | 0:b2064b3558b6 | 283 | _dio = 0; |
Peeraya | 0:b2064b3558b6 | 284 | _clk = 0; |
Peeraya | 0:b2064b3558b6 | 285 | } |
Peeraya | 0:b2064b3558b6 | 286 | |
Peeraya | 0:b2064b3558b6 | 287 | bool DigitDisplay::send(uint8_t data) |
Peeraya | 0:b2064b3558b6 | 288 | { |
Peeraya | 0:b2064b3558b6 | 289 | for (uint8_t i = 0; i < 8; i++) { |
Peeraya | 0:b2064b3558b6 | 290 | _clk = 0; |
Peeraya | 0:b2064b3558b6 | 291 | _dio = data & 1; |
Peeraya | 0:b2064b3558b6 | 292 | data >>= 1; |
Peeraya | 0:b2064b3558b6 | 293 | _clk = 1; |
Peeraya | 0:b2064b3558b6 | 294 | } |
Peeraya | 0:b2064b3558b6 | 295 | |
Peeraya | 0:b2064b3558b6 | 296 | // check ack |
Peeraya | 0:b2064b3558b6 | 297 | _clk = 0; |
Peeraya | 0:b2064b3558b6 | 298 | _dio = 1; |
Peeraya | 0:b2064b3558b6 | 299 | _clk = 1; |
Peeraya | 0:b2064b3558b6 | 300 | _dio.input(); |
Peeraya | 0:b2064b3558b6 | 301 | |
Peeraya | 0:b2064b3558b6 | 302 | uint16_t count = 0; |
Peeraya | 0:b2064b3558b6 | 303 | while (_dio) { |
Peeraya | 0:b2064b3558b6 | 304 | count++; |
Peeraya | 0:b2064b3558b6 | 305 | if (count >= 200) { |
Peeraya | 0:b2064b3558b6 | 306 | _dio.output(); |
Peeraya | 0:b2064b3558b6 | 307 | return false; |
Peeraya | 0:b2064b3558b6 | 308 | } |
Peeraya | 0:b2064b3558b6 | 309 | } |
Peeraya | 0:b2064b3558b6 | 310 | |
Peeraya | 0:b2064b3558b6 | 311 | _dio.output(); |
Peeraya | 0:b2064b3558b6 | 312 | return true; |
Peeraya | 0:b2064b3558b6 | 313 | } |
Peeraya | 0:b2064b3558b6 | 314 | |
Peeraya | 0:b2064b3558b6 | 315 | void DigitDisplay::stop() |
Peeraya | 0:b2064b3558b6 | 316 | { |
Peeraya | 0:b2064b3558b6 | 317 | _clk = 0; |
Peeraya | 0:b2064b3558b6 | 318 | _dio = 0; |
Peeraya | 0:b2064b3558b6 | 319 | _clk = 1; |
Peeraya | 0:b2064b3558b6 | 320 | _dio = 1; |
Peeraya | 0:b2064b3558b6 | 321 | } |
Peeraya | 0:b2064b3558b6 | 322 | void DigitDisplay::Displayset(int s1,int s2,int s3,int s4) |
Peeraya | 0:b2064b3558b6 | 323 | { |
Peeraya | 0:b2064b3558b6 | 324 | write(0, s1); |
Peeraya | 0:b2064b3558b6 | 325 | write(1, s2); |
Peeraya | 0:b2064b3558b6 | 326 | write(2, s3); |
Peeraya | 0:b2064b3558b6 | 327 | write(3, s4); |
Peeraya | 0:b2064b3558b6 | 328 | } |
Peeraya | 2:6b4dda997dc0 | 329 | void DigitDisplay::getButtonState(int n) |
NamSom | 1:ba7154d2d7ca | 330 | { |
Peeraya | 2:6b4dda997dc0 | 331 | prebuttonState1 = buttonState1; |
Peeraya | 2:6b4dda997dc0 | 332 | prebuttonState2 = buttonState2; |
Peeraya | 2:6b4dda997dc0 | 333 | prebuttonState3 = buttonState3; |
Peeraya | 2:6b4dda997dc0 | 334 | if(n==0) |
Peeraya | 2:6b4dda997dc0 | 335 | { |
Peeraya | 2:6b4dda997dc0 | 336 | buttonState1=0; |
Peeraya | 2:6b4dda997dc0 | 337 | buttonState2=0; |
Peeraya | 2:6b4dda997dc0 | 338 | buttonState3=0; |
Peeraya | 2:6b4dda997dc0 | 339 | } |
Peeraya | 2:6b4dda997dc0 | 340 | else if(n==1) buttonState1=1; |
Peeraya | 2:6b4dda997dc0 | 341 | else if(n==2) buttonState2=1; |
Peeraya | 2:6b4dda997dc0 | 342 | else if(n==3) buttonState3=1; |
NamSom | 1:ba7154d2d7ca | 343 | } |
NamSom | 1:ba7154d2d7ca | 344 | bool DigitDisplay::checkButton1() |
NamSom | 1:ba7154d2d7ca | 345 | { |
Peeraya | 2:6b4dda997dc0 | 346 | if(prebuttonState1==0 && buttonState1==1) return 1; |
NamSom | 1:ba7154d2d7ca | 347 | else return 0; |
NamSom | 1:ba7154d2d7ca | 348 | } |
NamSom | 1:ba7154d2d7ca | 349 | bool DigitDisplay::checkButton2() |
NamSom | 1:ba7154d2d7ca | 350 | { |
Peeraya | 2:6b4dda997dc0 | 351 | if(prebuttonState2==0 && buttonState2==1) return 1; |
NamSom | 1:ba7154d2d7ca | 352 | else return 0; |
NamSom | 1:ba7154d2d7ca | 353 | } |
NamSom | 1:ba7154d2d7ca | 354 | bool DigitDisplay::checkButton3() |
NamSom | 1:ba7154d2d7ca | 355 | { |
Peeraya | 2:6b4dda997dc0 | 356 | if(prebuttonState3==0 && buttonState3==1) return 1; |
NamSom | 1:ba7154d2d7ca | 357 | else return 0; |
NamSom | 4:a61cdd4e7afa | 358 | } |
NamSom | 4:a61cdd4e7afa | 359 | void DigitDisplay::F() |
NamSom | 4:a61cdd4e7afa | 360 | { |
NamSom | 4:a61cdd4e7afa | 361 | Displayset(0xF,0xFF,0xFF,0xFF); |
NamSom | 4:a61cdd4e7afa | 362 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 363 | Displayset(0xFF,0xF,0xFF,0xFF); |
NamSom | 4:a61cdd4e7afa | 364 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 365 | Displayset(0xFF,0xFF,0xF,0xFF); |
NamSom | 4:a61cdd4e7afa | 366 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 367 | Displayset(0xFF,0xFF,0xFF,0xF); |
NamSom | 4:a61cdd4e7afa | 368 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 369 | } |
NamSom | 4:a61cdd4e7afa | 370 | void DigitDisplay::A() |
NamSom | 4:a61cdd4e7afa | 371 | { |
NamSom | 4:a61cdd4e7afa | 372 | Displayset(0xA,0xA,0xA,0xA); |
NamSom | 4:a61cdd4e7afa | 373 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 374 | Displayset(0xA,0xA,0xA,0xA); |
NamSom | 4:a61cdd4e7afa | 375 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 376 | Displayset(0xA,0xA,0xA,0xA); |
NamSom | 4:a61cdd4e7afa | 377 | wait_ms(100); |
NamSom | 4:a61cdd4e7afa | 378 | Displayset(0xA,0xA,0xA,0xA); |
NamSom | 4:a61cdd4e7afa | 379 | wait_ms(100); |
NamSom | 1:ba7154d2d7ca | 380 | } |