LPC1114FN28 drive 8x8 LED Matrix. http://jiwashin.blogspot.jp/2016/07/my-lpc1114-device-lose-before-game.html

Dependencies:   mbed-rtos mbed

Revision:
1:badc825f9c7e
Parent:
0:070e4cb87478
Child:
2:adfdb6828250
--- a/main.cpp	Sat Jun 27 07:51:45 2015 +0000
+++ b/main.cpp	Sat Jun 27 22:02:07 2015 +0000
@@ -1,6 +1,15 @@
 #include "mbed.h"
 
 
+void setPNumLeftAndRight();
+
+void disableTicker();
+void enableTicker();
+
+void dynamicDriver();
+void count();
+
+
 char num0[] = {126,129,129,129,129,129,126,0};
 char num1[] = {0,1,65,255,1,1,0,0};
 char num2[] = {33,67,133,137,137,113,1,0};
@@ -11,7 +20,8 @@
 char num7[] = {192,128,128,143,144,160,192,0};
 char num8[] = {110,145,145,145,145,145,110,0};
 char num9[] = {114,137,137,137,137,137,126,0};
-
+char col[]   = {0, 0, 0, 36, 0, 0, 0, 0};
+char space[] = {0, 0, 0, 0, 0, 0, 0, 0}; 
 
 char *nums[] = {num0, num1, num2, num3, num4, num5, num6, num7, num8, num9};
 
@@ -20,13 +30,14 @@
 BusOut common( dp9,  dp2, dp11,  dp1, dp17, dp13, dp25, dp15);     // anode
 BusOut row(   dp28, dp26, dp16, dp10, dp14,  dp6,  dp4, dp18);     // casode
 
+I2CSlave slave(dp5, dp27);
 
 
 Ticker frameRate;
 Ticker dynamicRate;
 
 
-char *displayBuffer = "9876543210";
+char displayBuffer[100];
 int nDisplayBuffer;
 int  iDisplayBuffer = 0;
 char *pNumLeft;     // 出力中の数字ビットパターン、左側
@@ -36,16 +47,26 @@
 
 void setPNumLeftAndRight() {
     char left  = displayBuffer[iDisplayBuffer];
-    pNumLeft = nums[left  - '0'];
+    if (left == ' ') {
+        pNumLeft = space;
+    } else if (left == ':') {
+        pNumLeft = col;
+    } else {
+        pNumLeft = nums[left  - '0'];
+    }
     char right = (iDisplayBuffer+1 >= nDisplayBuffer) ? displayBuffer[0] : displayBuffer[iDisplayBuffer+1];
-    pNumRight = nums[right - '0'];
+    if (right == ' ') {
+        pNumRight = space;
+    } else if (right == ':') {
+        pNumRight = col;
+    } else {
+        pNumRight = nums[right - '0'];
+    }
 }
 
-void dynamicDriver();
-
 
 void count() {
-    dynamicRate.detach();
+    disableTicker();
     
     iShift++;
     if (iShift >= 8) {
@@ -57,8 +78,8 @@
         }
         setPNumLeftAndRight();
     }
-    
-    dynamicRate.attach(dynamicDriver, 0.0001);
+
+    enableTicker();
 }
 
 
@@ -83,19 +104,73 @@
     }
 }
 
+void disableTicker() {
+    frameRate.detach();
+    dynamicRate.detach();
+}
+
+void enableTicker() {
+    frameRate.attach(count, 0.05);
+    dynamicRate.attach(dynamicDriver, 0.0001);
+}
+
 
 int main() {
+    int deviceOffset = 4;
+    slave.address(0x10 + deviceOffset);
+    
+    strcpy(displayBuffer, " :3: ");
+    displayBuffer[2] = deviceOffset + '0';
     nDisplayBuffer = strlen(displayBuffer);
     
     iDisplayBuffer = 0;
     iShift         = 0;
     setPNumLeftAndRight();
     
-    frameRate.attach(count, 0.05);
-    dynamicRate.attach(dynamicDriver, 0.0001);
-
+    enableTicker();
+    
+    char buf[100];
+    char *msg = "ok";
     
-    while(1) {
-        wait(1);
-    }
+    while(1) {   
+        int status = slave.receive();
+        switch (status) {
+            case I2CSlave::ReadAddressed:
+                slave.write(msg, strlen(msg) + 1); // Includes null char
+                break;
+            
+            case I2CSlave::WriteGeneral:
+                disableTicker();
+                if (slave.read(buf, 2)) {
+                    int charPos  = buf[0] - '0' + deviceOffset;
+                    int shiftPos = buf[1] - '1';
+                    
+                    if (charPos >= 0 && charPos < nDisplayBuffer) {
+                        iDisplayBuffer = charPos;
+                    }
+                    if (shiftPos >= 0 && shiftPos < 8) {
+                        iShift = shiftPos;
+                    }
+                }
+                enableTicker();
+                break;
+            
+            case I2CSlave::WriteAddressed:
+                disableTicker();
+                if (slave.read(buf, 10)) {
+                    strcpy(displayBuffer, buf);
+                    nDisplayBuffer = strlen(displayBuffer);
+                    iDisplayBuffer = 0;
+                    iShift = 0;
+                    setPNumLeftAndRight();
+                    slave.stop();
+                }
+                enableTicker();
+                break;
+                
+            default: break;
+         }
+         
+     }
 }
+