LPC1114FN28 drive 8x8 LED Matrix. http://jiwashin.blogspot.jp/2016/07/my-lpc1114-device-lose-before-game.html
main.cpp@2:adfdb6828250, 2015-07-09 (annotated)
- Committer:
- TareObjects
- Date:
- Thu Jul 09 22:24:28 2015 +0000
- Revision:
- 2:adfdb6828250
- Parent:
- 1:badc825f9c7e
- Child:
- 3:ee5be57a4110
?????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TareObjects | 0:070e4cb87478 | 1 | #include "mbed.h" |
TareObjects | 2:adfdb6828250 | 2 | #include "rtos.h" |
TareObjects | 2:adfdb6828250 | 3 | |
TareObjects | 2:adfdb6828250 | 4 | const int deviceOffset = 0; |
TareObjects | 2:adfdb6828250 | 5 | |
TareObjects | 2:adfdb6828250 | 6 | |
TareObjects | 2:adfdb6828250 | 7 | #define MaxReceiveBufferSize 100 |
TareObjects | 0:070e4cb87478 | 8 | |
TareObjects | 0:070e4cb87478 | 9 | |
TareObjects | 1:badc825f9c7e | 10 | void setPNumLeftAndRight(); |
TareObjects | 1:badc825f9c7e | 11 | |
TareObjects | 1:badc825f9c7e | 12 | void disableTicker(); |
TareObjects | 1:badc825f9c7e | 13 | void enableTicker(); |
TareObjects | 1:badc825f9c7e | 14 | |
TareObjects | 1:badc825f9c7e | 15 | void dynamicDriver(); |
TareObjects | 1:badc825f9c7e | 16 | void count(); |
TareObjects | 1:badc825f9c7e | 17 | |
TareObjects | 1:badc825f9c7e | 18 | |
TareObjects | 0:070e4cb87478 | 19 | char num0[] = {126,129,129,129,129,129,126,0}; |
TareObjects | 0:070e4cb87478 | 20 | char num1[] = {0,1,65,255,1,1,0,0}; |
TareObjects | 0:070e4cb87478 | 21 | char num2[] = {33,67,133,137,137,113,1,0}; |
TareObjects | 0:070e4cb87478 | 22 | char num3[] = {66,129,145,145,145,145,110,0}; |
TareObjects | 0:070e4cb87478 | 23 | char num4[] = {24,40,72,136,8,255,8,0}; |
TareObjects | 0:070e4cb87478 | 24 | char num5[] = {242,145,145,145,145,145,142,0}; |
TareObjects | 0:070e4cb87478 | 25 | char num6[] = {126,145,145,145,145,145,78,0}; |
TareObjects | 0:070e4cb87478 | 26 | char num7[] = {192,128,128,143,144,160,192,0}; |
TareObjects | 0:070e4cb87478 | 27 | char num8[] = {110,145,145,145,145,145,110,0}; |
TareObjects | 0:070e4cb87478 | 28 | char num9[] = {114,137,137,137,137,137,126,0}; |
TareObjects | 1:badc825f9c7e | 29 | char col[] = {0, 0, 0, 36, 0, 0, 0, 0}; |
TareObjects | 2:adfdb6828250 | 30 | char space[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
TareObjects | 0:070e4cb87478 | 31 | |
TareObjects | 0:070e4cb87478 | 32 | char *nums[] = {num0, num1, num2, num3, num4, num5, num6, num7, num8, num9}; |
TareObjects | 0:070e4cb87478 | 33 | |
TareObjects | 0:070e4cb87478 | 34 | BusOut common( dp9, dp2, dp11, dp1, dp17, dp13, dp25, dp15); // anode |
TareObjects | 0:070e4cb87478 | 35 | BusOut row( dp28, dp26, dp16, dp10, dp14, dp6, dp4, dp18); // casode |
TareObjects | 0:070e4cb87478 | 36 | |
TareObjects | 1:badc825f9c7e | 37 | I2CSlave slave(dp5, dp27); |
TareObjects | 0:070e4cb87478 | 38 | |
TareObjects | 0:070e4cb87478 | 39 | |
TareObjects | 2:adfdb6828250 | 40 | Ticker secondRate; |
TareObjects | 0:070e4cb87478 | 41 | Ticker frameRate; |
TareObjects | 0:070e4cb87478 | 42 | Ticker dynamicRate; |
TareObjects | 0:070e4cb87478 | 43 | |
TareObjects | 0:070e4cb87478 | 44 | |
TareObjects | 2:adfdb6828250 | 45 | int iDisplayBuffer = 0; |
TareObjects | 0:070e4cb87478 | 46 | int nDisplayBuffer; |
TareObjects | 2:adfdb6828250 | 47 | |
TareObjects | 2:adfdb6828250 | 48 | char displayBuffer[MaxReceiveBufferSize]; |
TareObjects | 0:070e4cb87478 | 49 | char *pNumLeft; // 出力中の数字ビットパターン、左側 |
TareObjects | 0:070e4cb87478 | 50 | char *pNumRight; // 出力中の数字ビットパターン、右側 |
TareObjects | 0:070e4cb87478 | 51 | int iShift = 0; // 0 - 7 ... 現在何ビットずれているか |
TareObjects | 0:070e4cb87478 | 52 | |
TareObjects | 0:070e4cb87478 | 53 | |
TareObjects | 2:adfdb6828250 | 54 | char * getBitPatternWithPosition(int inPos) |
TareObjects | 2:adfdb6828250 | 55 | { |
TareObjects | 2:adfdb6828250 | 56 | char *result = space; |
TareObjects | 2:adfdb6828250 | 57 | |
TareObjects | 2:adfdb6828250 | 58 | int pos = (iDisplayBuffer + deviceOffset + inPos) % nDisplayBuffer; |
TareObjects | 2:adfdb6828250 | 59 | char c = displayBuffer[pos]; |
TareObjects | 2:adfdb6828250 | 60 | if (c == ' ') { |
TareObjects | 2:adfdb6828250 | 61 | result = space; |
TareObjects | 2:adfdb6828250 | 62 | } else if (c == ':') { |
TareObjects | 2:adfdb6828250 | 63 | result = col; |
TareObjects | 1:badc825f9c7e | 64 | } else { |
TareObjects | 2:adfdb6828250 | 65 | result = nums[c - '0']; |
TareObjects | 1:badc825f9c7e | 66 | } |
TareObjects | 2:adfdb6828250 | 67 | |
TareObjects | 2:adfdb6828250 | 68 | return result; |
TareObjects | 2:adfdb6828250 | 69 | } |
TareObjects | 2:adfdb6828250 | 70 | |
TareObjects | 2:adfdb6828250 | 71 | |
TareObjects | 2:adfdb6828250 | 72 | void setPNumLeftAndRight() |
TareObjects | 2:adfdb6828250 | 73 | { |
TareObjects | 2:adfdb6828250 | 74 | pNumLeft = getBitPatternWithPosition(0); |
TareObjects | 2:adfdb6828250 | 75 | pNumRight = getBitPatternWithPosition(1); |
TareObjects | 2:adfdb6828250 | 76 | } |
TareObjects | 2:adfdb6828250 | 77 | |
TareObjects | 2:adfdb6828250 | 78 | |
TareObjects | 2:adfdb6828250 | 79 | int second = 0; |
TareObjects | 2:adfdb6828250 | 80 | int minute = 0; |
TareObjects | 2:adfdb6828250 | 81 | int hour = 0; |
TareObjects | 2:adfdb6828250 | 82 | |
TareObjects | 2:adfdb6828250 | 83 | // if -1 the display is shift automatically |
TareObjects | 2:adfdb6828250 | 84 | int autoShiftWindow = -1; |
TareObjects | 2:adfdb6828250 | 85 | |
TareObjects | 2:adfdb6828250 | 86 | void secondVector() |
TareObjects | 2:adfdb6828250 | 87 | { |
TareObjects | 2:adfdb6828250 | 88 | second++; |
TareObjects | 2:adfdb6828250 | 89 | if (second >= 60) { |
TareObjects | 2:adfdb6828250 | 90 | second = 0; |
TareObjects | 2:adfdb6828250 | 91 | minute ++; |
TareObjects | 2:adfdb6828250 | 92 | if (minute >= 60) { |
TareObjects | 2:adfdb6828250 | 93 | minute = 0; |
TareObjects | 2:adfdb6828250 | 94 | hour++; |
TareObjects | 2:adfdb6828250 | 95 | if (hour >= 24) { |
TareObjects | 2:adfdb6828250 | 96 | hour = 0; |
TareObjects | 2:adfdb6828250 | 97 | } |
TareObjects | 2:adfdb6828250 | 98 | } |
TareObjects | 2:adfdb6828250 | 99 | } |
TareObjects | 2:adfdb6828250 | 100 | |
TareObjects | 2:adfdb6828250 | 101 | if (autoShiftWindow != -1) { |
TareObjects | 2:adfdb6828250 | 102 | autoShiftWindow++; |
TareObjects | 1:badc825f9c7e | 103 | } |
TareObjects | 0:070e4cb87478 | 104 | } |
TareObjects | 0:070e4cb87478 | 105 | |
TareObjects | 0:070e4cb87478 | 106 | |
TareObjects | 2:adfdb6828250 | 107 | void shiftVector() |
TareObjects | 2:adfdb6828250 | 108 | { |
TareObjects | 1:badc825f9c7e | 109 | disableTicker(); |
TareObjects | 2:adfdb6828250 | 110 | |
TareObjects | 2:adfdb6828250 | 111 | if (autoShiftWindow == -1) { |
TareObjects | 2:adfdb6828250 | 112 | iShift++; |
TareObjects | 2:adfdb6828250 | 113 | if (iShift >= 8) { |
TareObjects | 2:adfdb6828250 | 114 | iShift = 0; |
TareObjects | 2:adfdb6828250 | 115 | |
TareObjects | 2:adfdb6828250 | 116 | iDisplayBuffer++; |
TareObjects | 2:adfdb6828250 | 117 | if (iDisplayBuffer >= nDisplayBuffer) { |
TareObjects | 2:adfdb6828250 | 118 | iDisplayBuffer = 0; |
TareObjects | 2:adfdb6828250 | 119 | } |
TareObjects | 2:adfdb6828250 | 120 | setPNumLeftAndRight(); |
TareObjects | 0:070e4cb87478 | 121 | } |
TareObjects | 2:adfdb6828250 | 122 | } else { |
TareObjects | 2:adfdb6828250 | 123 | if (autoShiftWindow >= 60) { |
TareObjects | 2:adfdb6828250 | 124 | autoShiftWindow = 0; |
TareObjects | 2:adfdb6828250 | 125 | } |
TareObjects | 0:070e4cb87478 | 126 | } |
TareObjects | 1:badc825f9c7e | 127 | enableTicker(); |
TareObjects | 0:070e4cb87478 | 128 | } |
TareObjects | 0:070e4cb87478 | 129 | |
TareObjects | 0:070e4cb87478 | 130 | |
TareObjects | 0:070e4cb87478 | 131 | int iYoko = 0; |
TareObjects | 0:070e4cb87478 | 132 | int pYoko = 1; |
TareObjects | 0:070e4cb87478 | 133 | |
TareObjects | 0:070e4cb87478 | 134 | |
TareObjects | 2:adfdb6828250 | 135 | void dynamicVector() |
TareObjects | 2:adfdb6828250 | 136 | { |
TareObjects | 0:070e4cb87478 | 137 | int i = iYoko + iShift; |
TareObjects | 2:adfdb6828250 | 138 | |
TareObjects | 0:070e4cb87478 | 139 | common = 0x00; |
TareObjects | 2:adfdb6828250 | 140 | char rowData = (i < 8) ? pNumLeft[i] : pNumRight[i % 8]; |
TareObjects | 0:070e4cb87478 | 141 | row = (~rowData) & 0x00ff; |
TareObjects | 0:070e4cb87478 | 142 | common = (pYoko) & 0x00ff; |
TareObjects | 0:070e4cb87478 | 143 | iYoko++; |
TareObjects | 0:070e4cb87478 | 144 | pYoko *= 2; |
TareObjects | 0:070e4cb87478 | 145 | if (iYoko == 8) { |
TareObjects | 0:070e4cb87478 | 146 | iYoko = 0; |
TareObjects | 0:070e4cb87478 | 147 | pYoko = 1; |
TareObjects | 0:070e4cb87478 | 148 | } |
TareObjects | 0:070e4cb87478 | 149 | } |
TareObjects | 0:070e4cb87478 | 150 | |
TareObjects | 2:adfdb6828250 | 151 | |
TareObjects | 2:adfdb6828250 | 152 | void disableTicker() |
TareObjects | 2:adfdb6828250 | 153 | { |
TareObjects | 2:adfdb6828250 | 154 | secondRate.detach(); |
TareObjects | 1:badc825f9c7e | 155 | frameRate.detach(); |
TareObjects | 1:badc825f9c7e | 156 | dynamicRate.detach(); |
TareObjects | 1:badc825f9c7e | 157 | } |
TareObjects | 1:badc825f9c7e | 158 | |
TareObjects | 2:adfdb6828250 | 159 | void enableTicker() |
TareObjects | 2:adfdb6828250 | 160 | { |
TareObjects | 2:adfdb6828250 | 161 | secondRate.attach(secondVector, 1.0000); |
TareObjects | 2:adfdb6828250 | 162 | frameRate.attach(shiftVector, 0.0500); |
TareObjects | 2:adfdb6828250 | 163 | dynamicRate.attach(dynamicVector, 0.0001); |
TareObjects | 1:badc825f9c7e | 164 | } |
TareObjects | 1:badc825f9c7e | 165 | |
TareObjects | 2:adfdb6828250 | 166 | static char buf[MaxReceiveBufferSize]; |
TareObjects | 0:070e4cb87478 | 167 | |
TareObjects | 2:adfdb6828250 | 168 | int main() |
TareObjects | 2:adfdb6828250 | 169 | { |
TareObjects | 1:badc825f9c7e | 170 | slave.address(0x10 + deviceOffset); |
TareObjects | 2:adfdb6828250 | 171 | |
TareObjects | 1:badc825f9c7e | 172 | strcpy(displayBuffer, " :3: "); |
TareObjects | 1:badc825f9c7e | 173 | displayBuffer[2] = deviceOffset + '0'; |
TareObjects | 0:070e4cb87478 | 174 | nDisplayBuffer = strlen(displayBuffer); |
TareObjects | 2:adfdb6828250 | 175 | |
TareObjects | 0:070e4cb87478 | 176 | iDisplayBuffer = 0; |
TareObjects | 0:070e4cb87478 | 177 | iShift = 0; |
TareObjects | 0:070e4cb87478 | 178 | setPNumLeftAndRight(); |
TareObjects | 2:adfdb6828250 | 179 | |
TareObjects | 1:badc825f9c7e | 180 | enableTicker(); |
TareObjects | 2:adfdb6828250 | 181 | |
TareObjects | 2:adfdb6828250 | 182 | // char buf[100]; |
TareObjects | 2:adfdb6828250 | 183 | // char *msg = "ok"; |
TareObjects | 2:adfdb6828250 | 184 | |
TareObjects | 2:adfdb6828250 | 185 | while(1) { |
TareObjects | 1:badc825f9c7e | 186 | int status = slave.receive(); |
TareObjects | 2:adfdb6828250 | 187 | int charPos, shiftPos; |
TareObjects | 1:badc825f9c7e | 188 | switch (status) { |
TareObjects | 2:adfdb6828250 | 189 | // case I2CSlave::ReadAddressed: |
TareObjects | 2:adfdb6828250 | 190 | // slave.write(msg, strlen(msg) + 1); // Includes null char |
TareObjects | 2:adfdb6828250 | 191 | // break; |
TareObjects | 2:adfdb6828250 | 192 | |
TareObjects | 2:adfdb6828250 | 193 | // case I2CSlave::WriteGeneral: |
TareObjects | 2:adfdb6828250 | 194 | // disableTicker(); |
TareObjects | 2:adfdb6828250 | 195 | // if (slave.read(buf, 2)) { |
TareObjects | 2:adfdb6828250 | 196 | // autoShiftWindow = 0; // stop automatic shift |
TareObjects | 2:adfdb6828250 | 197 | // |
TareObjects | 2:adfdb6828250 | 198 | // charPos = buf[0] - '0'; |
TareObjects | 2:adfdb6828250 | 199 | // shiftPos = buf[1] - '0'; |
TareObjects | 2:adfdb6828250 | 200 | // |
TareObjects | 2:adfdb6828250 | 201 | // if (charPos >= 0 && charPos < nDisplayBuffer) { |
TareObjects | 2:adfdb6828250 | 202 | // iDisplayBuffer = charPos; |
TareObjects | 2:adfdb6828250 | 203 | // } |
TareObjects | 2:adfdb6828250 | 204 | // if (shiftPos >= 0 && shiftPos < 8) { |
TareObjects | 2:adfdb6828250 | 205 | // iShift = shiftPos; |
TareObjects | 2:adfdb6828250 | 206 | // } |
TareObjects | 2:adfdb6828250 | 207 | // } |
TareObjects | 2:adfdb6828250 | 208 | // enableTicker(); |
TareObjects | 2:adfdb6828250 | 209 | // break; |
TareObjects | 2:adfdb6828250 | 210 | |
TareObjects | 2:adfdb6828250 | 211 | case I2CSlave::WriteAddressed: |
TareObjects | 1:badc825f9c7e | 212 | disableTicker(); |
TareObjects | 2:adfdb6828250 | 213 | if (slave.read(buf, MaxReceiveBufferSize)) { |
TareObjects | 2:adfdb6828250 | 214 | if (buf[0] == 'x') { |
TareObjects | 2:adfdb6828250 | 215 | autoShiftWindow = 0; // stop automatic shift |
TareObjects | 2:adfdb6828250 | 216 | charPos = buf[1] - '0'; |
TareObjects | 2:adfdb6828250 | 217 | shiftPos = buf[2] - '0'; |
TareObjects | 2:adfdb6828250 | 218 | |
TareObjects | 2:adfdb6828250 | 219 | if (charPos >= 0 && charPos < MaxReceiveBufferSize) { |
TareObjects | 2:adfdb6828250 | 220 | iDisplayBuffer = charPos; |
TareObjects | 2:adfdb6828250 | 221 | } |
TareObjects | 2:adfdb6828250 | 222 | if (shiftPos >= 0 && shiftPos < 8) { |
TareObjects | 2:adfdb6828250 | 223 | iShift = shiftPos; |
TareObjects | 2:adfdb6828250 | 224 | } |
TareObjects | 2:adfdb6828250 | 225 | } else { |
TareObjects | 2:adfdb6828250 | 226 | strncpy(displayBuffer, buf, MaxReceiveBufferSize); |
TareObjects | 2:adfdb6828250 | 227 | displayBuffer[MaxReceiveBufferSize-1] = 0; |
TareObjects | 2:adfdb6828250 | 228 | nDisplayBuffer = strlen(displayBuffer); |
TareObjects | 2:adfdb6828250 | 229 | if (iDisplayBuffer >= nDisplayBuffer) iDisplayBuffer = 0; |
TareObjects | 2:adfdb6828250 | 230 | setPNumLeftAndRight(); |
TareObjects | 1:badc825f9c7e | 231 | } |
TareObjects | 1:badc825f9c7e | 232 | } |
TareObjects | 1:badc825f9c7e | 233 | enableTicker(); |
TareObjects | 1:badc825f9c7e | 234 | break; |
TareObjects | 2:adfdb6828250 | 235 | |
TareObjects | 2:adfdb6828250 | 236 | default: |
TareObjects | 1:badc825f9c7e | 237 | break; |
TareObjects | 2:adfdb6828250 | 238 | } |
TareObjects | 2:adfdb6828250 | 239 | for (int i = 0; i < MaxReceiveBufferSize; i++) buf[i] = 0; |
TareObjects | 2:adfdb6828250 | 240 | } |
TareObjects | 0:070e4cb87478 | 241 | } |
TareObjects | 1:badc825f9c7e | 242 |