Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sample_hardware.cpp Source File

sample_hardware.cpp

00001 #include "mbed.h"
00002 #include "sample_hardware.hpp"
00003 #include "LCD.hpp"
00004 
00005 #define RED_DONE 1
00006 #define YELLOW_DONE 2
00007 
00008 //Digital outputs
00009 DigitalOut onBoardLED(LED1);
00010 DigitalOut redLED(PE_15);
00011 DigitalOut yellowLED(PB_10);
00012 DigitalOut greenLED(PB_11);
00013 
00014 //Inputs
00015 DigitalIn  onBoardSwitch(USER_BUTTON);
00016 InterruptIn  SW1(PE_12);
00017 InterruptIn  SW2(PE_14);
00018 //Serial pc(USBTX, USBRX);
00019 AnalogIn adcIn(PA_0);
00020 
00021 //Environmental Sensor driver
00022 #ifdef BME
00023 BME280 sensor(D14, D15);
00024 #else
00025 BMP280 sensor(D14, D15);
00026 #endif
00027 
00028 //SD Card
00029 SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs 
00030 
00031 //POWER ON SELF TEST
00032 void post() 
00033 {
00034     //POWER ON TEST (POT)
00035     puts("**********STARTING POWER ON SELF TEST (POST)**********");
00036     
00037     //Test LEDs
00038     puts("ALL LEDs should be blinking");
00039     for (unsigned int n=0; n<10; n++) {
00040         redLED    = 1;
00041         yellowLED = 1;
00042         greenLED  = 1;
00043         wait(0.05);
00044         redLED    = 0;
00045         yellowLED = 0;
00046         greenLED  = 0;     
00047         wait(0.05);         
00048     } 
00049     
00050     //Output the switch states (hold them down to test)
00051     printf("SW1: %d\tSW2: %d\n\r", SW1.read(), SW2.read());    
00052     printf("USER: %d\n\r", onBoardSwitch.read()); 
00053     
00054     //Output the ADC
00055     printf("ADC: %f\n\r", adcIn.read());
00056     
00057     //Read Sensors (I2C)
00058     float temp = sensor.getTemperature();
00059     float pressure = sensor.getPressure();
00060     #ifdef BME
00061     float humidity = sensor.getHumidity();
00062     #endif
00063    
00064     //Display in PuTTY
00065     printf("Temperature: %5.1f\n", temp);
00066     printf("Pressure: %5.1f\n", pressure);
00067     #ifdef BME
00068     printf("Pressure: %5.1f\n", humidity);
00069     #endif
00070     
00071     //Display on LCD
00072     redLED = 1;
00073     //LCD.Display_Clear();
00074     //LCD.DDRAM_Address(0x00);
00075     //LCD.Write_String("LCD TEST...");
00076     wait(0.5);
00077     redLED = 0;
00078     
00079     //Network test (if BOTH switches are held down)
00080     //networktest();
00081     NetworkWaitTime = NetworkWait;
00082         //Initialise the SD card (this needs to move)
00083     if ( sd.init() != 0) {
00084         printf("SD Init failed \n");
00085         LCD.Display_Clear();
00086         LCD.Write_String("CANNOT INIT SD");        //Change me
00087         errorCode(FATAL);
00088     }
00089     //Create a filing system for SD Card
00090     FATFileSystem fs("sd", &sd);     
00091 
00092     //Open to WRITE
00093     
00094     FILE* fp = fopen("/sd/test.csv","w");
00095     if (fp == NULL) {
00096         error("Could not open file for write\n");
00097         LCD.Display_Clear();
00098         LCD.Write_String("CANNOT OPEN FILE");
00099         errorCode(FATAL);
00100     }
00101     //Close File
00102     fclose(fp);
00103     
00104     int network_temp;
00105     network_temp = Network_Init();
00106     if(network_temp == 1)//Sets up the network and checks if the network cable is not pluged in
00107     {
00108         error("Could not access network");
00109         LCD.Display_Clear();
00110         LCD.Write_String("Could not access network");
00111         errorCode(NETWORK_FATAL);   
00112     }
00113     //Last message before sampling begins
00114     LCD.Display_Clear();
00115     LCD.Write_String("READY     PLAYER");
00116     LCD.DDRAM_Address(0x40);
00117     LCD.Write_String("      One     ");
00118     LCD.DDRAM_Address(0x00);
00119     puts("**********POST END**********\n");
00120  
00121 }
00122 
00123 void errorCode(ELEC350_ERROR_CODE err)
00124 {
00125     switch (err) {
00126       case OK:
00127         greenLED = 1;
00128         wait(1.0);
00129         greenLED = 0;
00130         return;
00131     case NETWORK_FATAL:
00132         while(1) {
00133             pc.printf("NETWOK FATAL\n");
00134         }
00135     case SD_CARD_REMOVED:
00136             wait(0.1);
00137             LCD.Display_Clear();
00138             LCD.DDRAM_Address(0x00);
00139             LCD.Write_String("SD CARD");
00140             LCD.DDRAM_Address(0x40);
00141             LCD.Write_String("Removed");
00142             
00143             wait(5);
00144             pc.printf("SD CARD REMOVED\n");
00145             while(1) 
00146             {
00147                 if(SD_CARD_DETECT.read() == 0)
00148                 {
00149                     LCD.Display_Clear();
00150                     LCD.DDRAM_Address(0x00);
00151                     LCD.Write_String("SD CARD");
00152                     LCD.DDRAM_Address(0x40);
00153                     LCD.Write_String("Inserted");
00154                     pc.printf("SD CARD INSERTED\n");
00155                     SD_Write = 1;
00156                     return;    
00157                 }   
00158             }                   
00159     case FATAL:
00160         while(1) {
00161             pc.printf("FATAL\n");               
00162         }
00163     };
00164 }