MP3-capable chair with sensor-embedded weight scale.

Dependencies:   ACM1602 SDFileSystem VS1053 mbed ClockControl PowerControl

Revision:
1:ef257d63d970
Parent:
0:d9789f57fd9d
Child:
2:844bedc9dc63
--- a/main.cpp	Wed Mar 26 19:56:18 2014 +0000
+++ b/main.cpp	Sat Mar 29 16:14:11 2014 +0000
@@ -5,30 +5,29 @@
 #include "SDFileSystem.h"
 #include "VS1053.h"
 #include "ACM1602.h"
+#include "ClockControl.h"
+#include "EthernetPowerControl.h"
+#include "PowerControl.h"
 
 // Pin settings for LPC1768(LPCXpresso)
-SDFileSystem sd(/*MOSI*/ xp11, /*MISO*/ xp12, /*SCK*/ xp13, /*CS*/ xp53, /*Mountpoint*/ "sd");
-VS1053       mp3(/*MOSI*/ xp11, /*MISO*/ xp12, /*SCK*/ xp13,
-                 /*CS*/ xp52, /*BSYNC*/ xp51, /*DREQ*/ xp50, /*RST*/ xp49, /*SPI freq.*/ 6000000);
-HysteresisIn sens3(p17, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #3 (Outer)
-HysteresisIn sens2(p18, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #2
-HysteresisIn sens1(p19, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #1
-HysteresisIn sens0(p20, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #0 (Inner)
-ACM1602      lcd(/*SDA*/ xp9, /*SCL*/ xp10, /*Address*/ 0xa0);
-DigitalOut   led(LED0);
-DigitalOut   audioPower(xp38);
+SDFileSystem sd(/*MOSI*/ p11, /*MISO*/ p12, /*SCK*/ p13, /*CS*/ p21, /*Mountpoint*/ "sd");
+VS1053       mp3(/*MOSI*/ p11, /*MISO*/ p12, /*SCK*/ p13,
+                 /*CS*/ p22, /*BSYNC*/ p23, /*DREQ*/ p24, /*RST*/ p25, /*SPI freq.*/ 4000000);
+HysteresisIn sens3(p17, H_TO_L_THRES, L_TO_H_THRES, 1);  // Photo sensor #3 (Outer)
+HysteresisIn sens2(p18, H_TO_L_THRES, L_TO_H_THRES, 1);  // Photo sensor #2
+HysteresisIn sens1(p19, H_TO_L_THRES, L_TO_H_THRES, 1);  // Photo sensor #1
+HysteresisIn sens0(p20, H_TO_L_THRES, L_TO_H_THRES, 1);  // Photo sensor #0 (Inner)
+ACM1602      lcd(/*SDA*/ p9, /*SCL*/ p10, /*Address*/ 0xa0);
+DigitalOut   audioPower(p30);
+DigitalIn    test3(p17);
+DigitalIn    test2(p18);
+DigitalIn    test1(p19);
+DigitalIn    test0(p20);
+DigitalIn    inputSelector(p8);
+BusIn        __dummy(xp21, xp22, xp23, xp24, xp49, xp50, xp51, xp52, xp53);
 Ticker       tic;
-
-// Pin settings for LPC1114FN28 (SD card accessing does not work)
-// SDFileSystem sd(/*MOSI*/ dp2, /*MISO*/ dp1, /*SCK*/ dp6, /*CS*/ dp4, /*Mountpoint*/ "sd");
-// VS1053       mp3(/*MOSI*/ dp2, /*MISO*/ dp1, /*SCK*/ dp6,
-//                  /*CS*/ dp14, /*BSYNC*/ dp17, /*DREQ*/ dp18, /*RST*/ dp25, /*SPI freq.*/ 4000000);
-// HysteresisIn sens3(dp9,  HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #3 (Outer)
-// HysteresisIn sens2(dp10, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #2
-// HysteresisIn sens1(dp11, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #1
-// HysteresisIn sens0(dp13, HIGH_TO_LOW, LOW_TO_HIGH, 1);  // Photo sensor #0 (Inner)
-// ACM1602      lcd(/*SDA*/ dp5, /*SCL*/ dp27, /*Address*/ 0xa0);
-// Ticker       tic;
+bool         inputFrom;
+size_t       totalSizeSent;
 
 const char *fileNameList[] = {
     "/sd/Track1.mp3",
@@ -42,6 +41,20 @@
 
 /** Setup and initializations */
 void setup(void) {
+    // Reduce system clock frequency
+    setSystemFrequency(0x3, 0x1, 6, 1);
+    
+    // Power down Ethernet PHY
+    PHY_PowerDown();
+    
+    // Set pull-up mode
+    inputSelector.mode(PullUp);
+    test3.mode(PullUp);
+    test2.mode(PullUp);
+    test1.mode(PullUp);
+    test0.mode(PullUp);
+    wait(0.1);
+    
     // Initialize VS1053
     mp3.hardwareReset();
     mp3.clockUp();
@@ -53,8 +66,8 @@
     // Setup LCD
     lcd.init();
     
-    // Initialize LED
-    led = 0;
+    // Read input selector
+    inputFrom = inputSelector;
 }
 
 /** Read voltages from photo sensor pins and detect weightscale point code */
@@ -62,10 +75,17 @@
     uint8_t bitPattern;
     
     // Read all photo sensor inputs
-    bitPattern =   (sens3.read() << 3)
-                 | (sens2.read() << 2)
-                 | (sens1.read() << 1)
-                 |  sens0.read();
+    if (inputFrom) {
+        bitPattern =   (test3 << 3)
+                     | (test2 << 2)
+                     | (test1 << 1)
+                     |  test0;
+    } else {
+        bitPattern =   (sens3.read() << 3)
+                     | (sens2.read() << 2)
+                     | (sens1.read() << 1)
+                     |  sens0.read();
+    }
     
     switch (bitPattern) {
     // 1 when open, 0 when shut
@@ -111,10 +131,28 @@
     } else {
         holdTimes = 0;
     }
+    
+    // Print status to LCD:
+    // current state, photo sensor inputs, hold count,
+    // file size sent to VS1053
     lcd.locate(0, 0);
-    lcd.printf("%-15s", stateNameList[state + 2]);
+    lcd.printf("%-9s  in=%2d", stateNameList[state + 2], code);
     lcd.locate(0, 1);
-    lcd.printf("ps=%2d times=%2d", code, holdTimes);
+    lcd.printf("x%2d ", holdTimes);
+    switch (state) {
+    case PLAYING1: case PLAYING2: case PLAYING3: case PLAYING4:
+        if (totalSizeSent >= 1000000) {
+            lcd.printf("%9.2f MB", totalSizeSent / 1048576.0);
+        } else if (totalSizeSent >= 1000) {
+            lcd.printf("%9.2f KB", totalSizeSent / 1024.0);
+        } else {
+            lcd.printf("%6d bytes", totalSizeSent);
+        }
+        break;
+    default:
+        lcd.printf("   <No File>");
+        break;
+    }
     
     // Once the point is stable enough, make a request
     if (holdTimes == SETTLING_COUNT) {
@@ -136,10 +174,9 @@
 
 /** Player control in accordance with requests */
 void controlTrack() {
-    static FILE   *fp = NULL;
-    size_t        sizeRead = 0;
-    uint8_t       buf[BLOCK_SIZE];
-    static size_t totalSizeSent = 0;
+    static FILE *fp = NULL;
+    size_t      sizeRead = 0;
+    uint8_t     buf[BLOCK_SIZE];
     
     switch (state) {
     case READY:
@@ -148,20 +185,16 @@
             // Clear stop request
             request = NONE;
             break;
-        case PLAY1_REQUEST: case PLAY2_REQUEST: case PLAY3_REQUEST: case PLAY4_REQUEST:            
+        case PLAY1_REQUEST: case PLAY2_REQUEST: case PLAY3_REQUEST: case PLAY4_REQUEST:
             fp = fopen(fileNameList[request - 1], "rb");
             if (fp) {
+                clearerr(fp);
+                
                 // Power supply on
                 audioPower = 1;
-                led = 1;
                 
                 totalSizeSent = 0;
                 state = (State) request;
-                lcd.locate(15, 0);
-                lcd.printf("*");
-            } else {
-                lcd.locate(0, 1);
-                lcd.printf("File Open Error ");
             }
             // Clear play request
             request = NONE;
@@ -172,24 +205,23 @@
         break;
     case PLAYING1: case PLAYING2: case PLAYING3: case PLAYING4:
         if (request == NONE) {
-            sizeRead = fread(buf, sizeof(uint8_t), BLOCK_SIZE, fp);
-            if (sizeRead) {
-                totalSizeSent += mp3.sendDataBlock(buf, sizeRead);
-            } else {
-                // Close the track
+            // Continue playback
+            if (feof(fp)) {
+                // Close when the track reaches the end
                 fclose(fp);
-                lcd.locate(15, 0);
-                lcd.printf(" ");
+                fp = NULL;
                 
                 // Invoke play request again
                 request = (Request) state;
                 state = READY;
+            } else {
+                sizeRead = fread(buf, sizeof(char), BLOCK_SIZE, fp);
+                totalSizeSent += mp3.sendDataBlock(buf, sizeRead);
             }
         } else {
             // Cancel current track when something's requested
             fclose(fp);
-            lcd.locate(15, 0);
-            lcd.printf(" ");
+            fp = NULL;
             state = CANCELING;
         }
         break;
@@ -208,7 +240,6 @@
             
             // Power supply off
             audioPower = 0;
-            led = 0;
         }
         break;
     default:
@@ -276,12 +307,7 @@
     
     // Test files: Enter an infinite loop on failure
     if (testFiles()) {
-        while (1) {
-            led = 1;
-            wait(0.5);
-            led = 0;
-            wait(0.5);
-        }
+        while (1) {}
     }
     
     // Set Ticker interrupt routine