LPC1114FN28 drive 8x8 LED Matrix. http://jiwashin.blogspot.jp/2016/07/my-lpc1114-device-lose-before-game.html
main.cpp@5:212841923abb, 2016-05-06 (annotated)
- Committer:
- TareObjects
- Date:
- Fri May 06 19:57:01 2016 +0000
- Revision:
- 5:212841923abb
- Parent:
- 3:ee5be57a4110
- Child:
- 6:8485cf53cd79
ver 0.2???
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 | 5:212841923abb | 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 | 3:ee5be57a4110 | 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 | 3:ee5be57a4110 | 79 | //int second = 0; |
TareObjects | 3:ee5be57a4110 | 80 | //int minute = 0; |
TareObjects | 3:ee5be57a4110 | 81 | //int hour = 0; |
TareObjects | 3:ee5be57a4110 | 82 | // |
TareObjects | 3:ee5be57a4110 | 83 | //// if -1 the display is shift automatically |
TareObjects | 3:ee5be57a4110 | 84 | //int autoShiftWindow = -1; |
TareObjects | 3:ee5be57a4110 | 85 | // |
TareObjects | 3:ee5be57a4110 | 86 | //void secondVector() |
TareObjects | 3:ee5be57a4110 | 87 | //{ |
TareObjects | 3:ee5be57a4110 | 88 | // second++; |
TareObjects | 3:ee5be57a4110 | 89 | // if (second >= 60) { |
TareObjects | 3:ee5be57a4110 | 90 | // second = 0; |
TareObjects | 3:ee5be57a4110 | 91 | // minute ++; |
TareObjects | 3:ee5be57a4110 | 92 | // if (minute >= 60) { |
TareObjects | 3:ee5be57a4110 | 93 | // minute = 0; |
TareObjects | 3:ee5be57a4110 | 94 | // hour++; |
TareObjects | 3:ee5be57a4110 | 95 | // if (hour >= 24) { |
TareObjects | 3:ee5be57a4110 | 96 | // hour = 0; |
TareObjects | 3:ee5be57a4110 | 97 | // } |
TareObjects | 3:ee5be57a4110 | 98 | // } |
TareObjects | 3:ee5be57a4110 | 99 | // } |
TareObjects | 3:ee5be57a4110 | 100 | // |
TareObjects | 3:ee5be57a4110 | 101 | // if (autoShiftWindow != -1) { |
TareObjects | 3:ee5be57a4110 | 102 | // autoShiftWindow++; |
TareObjects | 3:ee5be57a4110 | 103 | // } |
TareObjects | 3:ee5be57a4110 | 104 | //} |
TareObjects | 2:adfdb6828250 | 105 | |
TareObjects | 2:adfdb6828250 | 106 | |
TareObjects | 3:ee5be57a4110 | 107 | void shiftVector(const void* arg) |
TareObjects | 2:adfdb6828250 | 108 | { |
TareObjects | 3:ee5be57a4110 | 109 | iShift++; |
TareObjects | 3:ee5be57a4110 | 110 | if (iShift >= 8) { |
TareObjects | 3:ee5be57a4110 | 111 | iShift = 0; |
TareObjects | 3:ee5be57a4110 | 112 | |
TareObjects | 3:ee5be57a4110 | 113 | iDisplayBuffer++; |
TareObjects | 3:ee5be57a4110 | 114 | if (iDisplayBuffer >= nDisplayBuffer) { |
TareObjects | 3:ee5be57a4110 | 115 | iDisplayBuffer = 0; |
TareObjects | 2:adfdb6828250 | 116 | } |
TareObjects | 3:ee5be57a4110 | 117 | setPNumLeftAndRight(); |
TareObjects | 1:badc825f9c7e | 118 | } |
TareObjects | 0:070e4cb87478 | 119 | } |
TareObjects | 0:070e4cb87478 | 120 | |
TareObjects | 0:070e4cb87478 | 121 | |
TareObjects | 3:ee5be57a4110 | 122 | static int iYoko = 0; |
TareObjects | 3:ee5be57a4110 | 123 | static int pYoko = 1; |
TareObjects | 0:070e4cb87478 | 124 | |
TareObjects | 0:070e4cb87478 | 125 | |
TareObjects | 3:ee5be57a4110 | 126 | void dynamicVector(void const* arg) |
TareObjects | 2:adfdb6828250 | 127 | { |
TareObjects | 0:070e4cb87478 | 128 | int i = iYoko + iShift; |
TareObjects | 2:adfdb6828250 | 129 | |
TareObjects | 0:070e4cb87478 | 130 | common = 0x00; |
TareObjects | 2:adfdb6828250 | 131 | char rowData = (i < 8) ? pNumLeft[i] : pNumRight[i % 8]; |
TareObjects | 0:070e4cb87478 | 132 | row = (~rowData) & 0x00ff; |
TareObjects | 0:070e4cb87478 | 133 | common = (pYoko) & 0x00ff; |
TareObjects | 0:070e4cb87478 | 134 | iYoko++; |
TareObjects | 0:070e4cb87478 | 135 | pYoko *= 2; |
TareObjects | 0:070e4cb87478 | 136 | if (iYoko == 8) { |
TareObjects | 0:070e4cb87478 | 137 | iYoko = 0; |
TareObjects | 0:070e4cb87478 | 138 | pYoko = 1; |
TareObjects | 0:070e4cb87478 | 139 | } |
TareObjects | 0:070e4cb87478 | 140 | } |
TareObjects | 0:070e4cb87478 | 141 | |
TareObjects | 2:adfdb6828250 | 142 | |
TareObjects | 3:ee5be57a4110 | 143 | //void disableTicker() |
TareObjects | 3:ee5be57a4110 | 144 | //{ |
TareObjects | 3:ee5be57a4110 | 145 | // secondRate.detach(); |
TareObjects | 3:ee5be57a4110 | 146 | // frameRate.detach(); |
TareObjects | 3:ee5be57a4110 | 147 | // dynamicRate.detach(); |
TareObjects | 3:ee5be57a4110 | 148 | //} |
TareObjects | 1:badc825f9c7e | 149 | |
TareObjects | 3:ee5be57a4110 | 150 | //void enableTicker() |
TareObjects | 3:ee5be57a4110 | 151 | //{ |
TareObjects | 3:ee5be57a4110 | 152 | // secondRate.attach(secondVector, 1.0000); |
TareObjects | 3:ee5be57a4110 | 153 | // frameRate.attach(shiftVector, 0.0500); |
TareObjects | 3:ee5be57a4110 | 154 | // dynamicRate.attach(dynamicVector, 0.0001); |
TareObjects | 3:ee5be57a4110 | 155 | //} |
TareObjects | 1:badc825f9c7e | 156 | |
TareObjects | 2:adfdb6828250 | 157 | static char buf[MaxReceiveBufferSize]; |
TareObjects | 0:070e4cb87478 | 158 | |
TareObjects | 2:adfdb6828250 | 159 | int main() |
TareObjects | 2:adfdb6828250 | 160 | { |
TareObjects | 1:badc825f9c7e | 161 | slave.address(0x10 + deviceOffset); |
TareObjects | 2:adfdb6828250 | 162 | |
TareObjects | 3:ee5be57a4110 | 163 | strcpy(displayBuffer, "x "); |
TareObjects | 3:ee5be57a4110 | 164 | displayBuffer[0] = deviceOffset + '0'; |
TareObjects | 0:070e4cb87478 | 165 | nDisplayBuffer = strlen(displayBuffer); |
TareObjects | 2:adfdb6828250 | 166 | |
TareObjects | 0:070e4cb87478 | 167 | iDisplayBuffer = 0; |
TareObjects | 0:070e4cb87478 | 168 | iShift = 0; |
TareObjects | 0:070e4cb87478 | 169 | setPNumLeftAndRight(); |
TareObjects | 2:adfdb6828250 | 170 | |
TareObjects | 3:ee5be57a4110 | 171 | RtosTimer dynamicTimer(dynamicVector, osTimerPeriodic); |
TareObjects | 3:ee5be57a4110 | 172 | dynamicTimer.start(1); |
TareObjects | 3:ee5be57a4110 | 173 | |
TareObjects | 3:ee5be57a4110 | 174 | RtosTimer frameRate(shiftVector, osTimerPeriodic); |
TareObjects | 3:ee5be57a4110 | 175 | frameRate.start(50); |
TareObjects | 3:ee5be57a4110 | 176 | int demoMode = true; |
TareObjects | 3:ee5be57a4110 | 177 | |
TareObjects | 2:adfdb6828250 | 178 | while(1) { |
TareObjects | 1:badc825f9c7e | 179 | int status = slave.receive(); |
TareObjects | 2:adfdb6828250 | 180 | int charPos, shiftPos; |
TareObjects | 1:badc825f9c7e | 181 | switch (status) { |
TareObjects | 2:adfdb6828250 | 182 | // case I2CSlave::ReadAddressed: |
TareObjects | 2:adfdb6828250 | 183 | // slave.write(msg, strlen(msg) + 1); // Includes null char |
TareObjects | 2:adfdb6828250 | 184 | // break; |
TareObjects | 2:adfdb6828250 | 185 | |
TareObjects | 3:ee5be57a4110 | 186 | case I2CSlave::WriteGeneral: |
TareObjects | 2:adfdb6828250 | 187 | case I2CSlave::WriteAddressed: |
TareObjects | 3:ee5be57a4110 | 188 | if (demoMode) { |
TareObjects | 3:ee5be57a4110 | 189 | frameRate.stop(); |
TareObjects | 3:ee5be57a4110 | 190 | demoMode = false; |
TareObjects | 3:ee5be57a4110 | 191 | } |
TareObjects | 2:adfdb6828250 | 192 | if (slave.read(buf, MaxReceiveBufferSize)) { |
TareObjects | 2:adfdb6828250 | 193 | if (buf[0] == 'x') { |
TareObjects | 2:adfdb6828250 | 194 | charPos = buf[1] - '0'; |
TareObjects | 2:adfdb6828250 | 195 | shiftPos = buf[2] - '0'; |
TareObjects | 2:adfdb6828250 | 196 | |
TareObjects | 2:adfdb6828250 | 197 | if (charPos >= 0 && charPos < MaxReceiveBufferSize) { |
TareObjects | 2:adfdb6828250 | 198 | iDisplayBuffer = charPos; |
TareObjects | 2:adfdb6828250 | 199 | } |
TareObjects | 2:adfdb6828250 | 200 | if (shiftPos >= 0 && shiftPos < 8) { |
TareObjects | 2:adfdb6828250 | 201 | iShift = shiftPos; |
TareObjects | 2:adfdb6828250 | 202 | } |
TareObjects | 2:adfdb6828250 | 203 | } else { |
TareObjects | 2:adfdb6828250 | 204 | strncpy(displayBuffer, buf, MaxReceiveBufferSize); |
TareObjects | 2:adfdb6828250 | 205 | displayBuffer[MaxReceiveBufferSize-1] = 0; |
TareObjects | 2:adfdb6828250 | 206 | nDisplayBuffer = strlen(displayBuffer); |
TareObjects | 2:adfdb6828250 | 207 | if (iDisplayBuffer >= nDisplayBuffer) iDisplayBuffer = 0; |
TareObjects | 1:badc825f9c7e | 208 | } |
TareObjects | 3:ee5be57a4110 | 209 | setPNumLeftAndRight(); |
TareObjects | 1:badc825f9c7e | 210 | } |
TareObjects | 1:badc825f9c7e | 211 | break; |
TareObjects | 2:adfdb6828250 | 212 | |
TareObjects | 2:adfdb6828250 | 213 | default: |
TareObjects | 3:ee5be57a4110 | 214 | Thread::wait(1); |
TareObjects | 1:badc825f9c7e | 215 | break; |
TareObjects | 2:adfdb6828250 | 216 | } |
TareObjects | 2:adfdb6828250 | 217 | } |
TareObjects | 0:070e4cb87478 | 218 | } |
TareObjects | 1:badc825f9c7e | 219 |