MP3-capable chair with sensor-embedded weight scale.

Dependencies:   ACM1602 SDFileSystem VS1053 mbed ClockControl PowerControl

Files at this revision

API Documentation at this revision

Comitter:
kayekss
Date:
Sat Mar 29 16:14:11 2014 +0000
Parent:
0:d9789f57fd9d
Child:
2:844bedc9dc63
Commit message:
(1) Changed the target MCU to mbed LPC1768. (2) Added power-saving controls. (3) Changed content to print to display.

Changed in this revision

ClockControl.lib Show annotated file Show diff for this revision Revisions of this file
PowerControl.lib Show annotated file Show diff for this revision Revisions of this file
defs.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ClockControl.lib	Sat Mar 29 16:14:11 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/JST2011/code/ClockControl/#a6d100de3aee
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PowerControl.lib	Sat Mar 29 16:14:11 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/JST2011/code/PowerControl/#d0fa2aeb02a4
--- a/defs.h	Wed Mar 26 19:56:18 2014 +0000
+++ b/defs.h	Sat Mar 29 16:14:11 2014 +0000
@@ -2,8 +2,8 @@
 #define ISU_PROJECT_DEFS_H_
 
 // Input thresholds
-#define HIGH_TO_LOW        0.25
-#define LOW_TO_HIGH        0.75
+#define H_TO_L_THRES       0.25
+#define L_TO_H_THRES       0.75
 
 // Delay count until settling
 #define SETTLING_COUNT        5
@@ -12,7 +12,7 @@
 #define POLL_INTERVAL_SEC  0.50
 
 // Block size of read/write (byte)
-#define BLOCK_SIZE         1024
+#define BLOCK_SIZE           64
 
 typedef enum {
     CANCELING = -1,
@@ -33,26 +33,26 @@
     PLAY4_REQUEST =  4
 } Request;
 
-PinName const xp5  = P0_9;   // SPI1 + MOSI
-PinName const xp6  = P0_8;   //      | MISO
-PinName const xp7  = P0_7;   //      | SCK
-PinName const xp8  = P0_6;   //      | SSEL#
-PinName const xp9  = P0_0;   // UART3+ Tx     I2C1 + SDA
-PinName const xp10 = P0_1;   //      | Rx          | SCL
-PinName const xp11 = P0_18;  //               SPI0 + MOSI
-PinName const xp12 = P0_17;  //                    | MISO
-PinName const xp13 = P0_15;  // UART1+ Tx          | SCK
-PinName const xp14 = P0_16;  //      | Rx          | SSEL#
-PinName const xp15 = P0_23;  // AnalogIn
-PinName const xp16 = P0_24;  // AnalogIn
-PinName const xp17 = P0_25;  // AnalogIn
-PinName const xp18 = P0_26;  // AnalogIn  AnalogOut
-PinName const xp19 = P1_30;  // AnalogIn
-PinName const xp20 = P1_31;  // AnalogIn
+PinName const xp5  = P0_9;   // (p5)   SPI1 + MOSI
+PinName const xp6  = P0_8;   // (p6)        | MISO
+PinName const xp7  = P0_7;   // (p7)        | SCK
+PinName const xp8  = P0_6;   // (p8)        | SSEL#
+PinName const xp9  = P0_0;   // (p9)   UART3+ Tx     I2C1 + SDA
+PinName const xp10 = P0_1;   // (p10)       | Rx          | SCL
+PinName const xp11 = P0_18;  // (p11)                SPI0 + MOSI
+PinName const xp12 = P0_17;  // (p12)                     | MISO
+PinName const xp13 = P0_15;  // (p13)  UART1+ Tx          | SCK
+PinName const xp14 = P0_16;  // (p14)       | Rx          | SSEL#
+PinName const xp15 = P0_23;  // (p15)  AnalogIn
+PinName const xp16 = P0_24;  // (p16)  AnalogIn
+PinName const xp17 = P0_25;  // (p17)  AnalogIn
+PinName const xp18 = P0_26;  // (p18)  AnalogIn  AnalogOut
+PinName const xp19 = P1_30;  // (p19)  AnalogIn
+PinName const xp20 = P1_31;  // (p20)  AnalogIn
 PinName const xp21 = P0_2;
 PinName const xp22 = P0_3;
 PinName const xp23 = P0_21;
-PinName const xp24 = P0_22;
+PinName const xp24 = P0_22;  // **Onboard LED
 PinName const xp25 = P0_27;  // **Open drain
 PinName const xp26 = P0_28;  // **Open drain
 PinName const xp27 = P2_13;
@@ -62,16 +62,16 @@
 PinName const xp50 = P2_8;
 PinName const xp49 = P2_7;
 PinName const xp48 = P2_6;
-PinName const xp47 = P2_5;   // PwmOut
-PinName const xp46 = P2_4;   // PwmOut
-PinName const xp45 = P2_3;   // PwmOut
-PinName const xp44 = P2_2;   // PwmOut
-PinName const xp43 = P2_1;   // PwmOut
-PinName const xp42 = P2_0;   // PwmOut
-PinName const xp41 = P0_11;  // UART2+ Rx     I2C2 + SCL
-PinName const xp40 = P0_10;  //      | Tx          | SDA
-PinName const xp39 = P0_5;   // CAN  + TD
-PinName const xp38 = P0_4;   //      | RD
+PinName const xp47 = P2_5;   // (p21)  PwmOut
+PinName const xp46 = P2_4;   // (p22)  PwmOut
+PinName const xp45 = P2_3;   // (p23)  PwmOut
+PinName const xp44 = P2_2;   // (p24)  PwmOut
+PinName const xp43 = P2_1;   // (p25)  PwmOut
+PinName const xp42 = P2_0;   // (p26)  PwmOut
+PinName const xp41 = P0_11;  // (p27)  UART2+ Rx     I2C2 + SCL
+PinName const xp40 = P0_10;  // (p28)       | Tx          | SDA
+PinName const xp39 = P0_5;   // (p29)  CAN  + TD
+PinName const xp38 = P0_4;   // (p30)       | RD
 PinName const LED0 = P0_22;
 
 #endif
--- 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