Time: 17:33 Date: 10/12/2017 Description: Task 1,7,8 Currently Functioning
Dependencies: BME280 BMP280 TextLCD
Working Repository
sample_hardware.cpp@49:d51f96a46cc3, 2018-01-09 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Jan 09 11:41:07 2018 +0000
- Revision:
- 49:d51f96a46cc3
- Parent:
- 48:244d6d81bb52
SD Card Working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 37:7c4d7f206039 | 1 | #include "mbed.h" |
thomasmorris | 37:7c4d7f206039 | 2 | #include "sample_hardware.hpp" |
thomasmorris | 37:7c4d7f206039 | 3 | #include "LCD.hpp" |
thomasmorris | 49:d51f96a46cc3 | 4 | |
thomasmorris | 37:7c4d7f206039 | 5 | #define RED_DONE 1 |
thomasmorris | 37:7c4d7f206039 | 6 | #define YELLOW_DONE 2 |
thomasmorris | 37:7c4d7f206039 | 7 | |
thomasmorris | 37:7c4d7f206039 | 8 | //Digital outputs |
thomasmorris | 37:7c4d7f206039 | 9 | DigitalOut onBoardLED(LED1); |
thomasmorris | 37:7c4d7f206039 | 10 | DigitalOut redLED(PE_15); |
thomasmorris | 37:7c4d7f206039 | 11 | DigitalOut yellowLED(PB_10); |
thomasmorris | 37:7c4d7f206039 | 12 | DigitalOut greenLED(PB_11); |
thomasmorris | 37:7c4d7f206039 | 13 | |
thomasmorris | 37:7c4d7f206039 | 14 | //Inputs |
thomasmorris | 37:7c4d7f206039 | 15 | DigitalIn onBoardSwitch(USER_BUTTON); |
thomasmorris | 37:7c4d7f206039 | 16 | InterruptIn SW1(PE_12); |
thomasmorris | 37:7c4d7f206039 | 17 | InterruptIn SW2(PE_14); |
thomasmorris | 37:7c4d7f206039 | 18 | //Serial pc(USBTX, USBRX); |
thomasmorris | 37:7c4d7f206039 | 19 | AnalogIn adcIn(PA_0); |
thomasmorris | 37:7c4d7f206039 | 20 | |
thomasmorris | 37:7c4d7f206039 | 21 | //Environmental Sensor driver |
thomasmorris | 37:7c4d7f206039 | 22 | #ifdef BME |
thomasmorris | 37:7c4d7f206039 | 23 | BME280 sensor(D14, D15); |
thomasmorris | 37:7c4d7f206039 | 24 | #else |
thomasmorris | 37:7c4d7f206039 | 25 | BMP280 sensor(D14, D15); |
thomasmorris | 37:7c4d7f206039 | 26 | #endif |
thomasmorris | 37:7c4d7f206039 | 27 | |
thomasmorris | 37:7c4d7f206039 | 28 | //SD Card |
thomasmorris | 37:7c4d7f206039 | 29 | SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs |
thomasmorris | 37:7c4d7f206039 | 30 | |
thomasmorris | 37:7c4d7f206039 | 31 | //POWER ON SELF TEST |
thomasmorris | 37:7c4d7f206039 | 32 | void post() |
thomasmorris | 37:7c4d7f206039 | 33 | { |
thomasmorris | 37:7c4d7f206039 | 34 | //POWER ON TEST (POT) |
thomasmorris | 37:7c4d7f206039 | 35 | puts("**********STARTING POWER ON SELF TEST (POST)**********"); |
thomasmorris | 37:7c4d7f206039 | 36 | |
thomasmorris | 37:7c4d7f206039 | 37 | //Test LEDs |
thomasmorris | 37:7c4d7f206039 | 38 | puts("ALL LEDs should be blinking"); |
thomasmorris | 37:7c4d7f206039 | 39 | for (unsigned int n=0; n<10; n++) { |
thomasmorris | 37:7c4d7f206039 | 40 | redLED = 1; |
thomasmorris | 37:7c4d7f206039 | 41 | yellowLED = 1; |
thomasmorris | 37:7c4d7f206039 | 42 | greenLED = 1; |
thomasmorris | 37:7c4d7f206039 | 43 | wait(0.05); |
thomasmorris | 37:7c4d7f206039 | 44 | redLED = 0; |
thomasmorris | 37:7c4d7f206039 | 45 | yellowLED = 0; |
thomasmorris | 37:7c4d7f206039 | 46 | greenLED = 0; |
thomasmorris | 37:7c4d7f206039 | 47 | wait(0.05); |
thomasmorris | 37:7c4d7f206039 | 48 | } |
thomasmorris | 37:7c4d7f206039 | 49 | |
thomasmorris | 37:7c4d7f206039 | 50 | //Output the switch states (hold them down to test) |
thomasmorris | 37:7c4d7f206039 | 51 | printf("SW1: %d\tSW2: %d\n\r", SW1.read(), SW2.read()); |
thomasmorris | 37:7c4d7f206039 | 52 | printf("USER: %d\n\r", onBoardSwitch.read()); |
thomasmorris | 37:7c4d7f206039 | 53 | |
thomasmorris | 37:7c4d7f206039 | 54 | //Output the ADC |
thomasmorris | 37:7c4d7f206039 | 55 | printf("ADC: %f\n\r", adcIn.read()); |
thomasmorris | 37:7c4d7f206039 | 56 | |
thomasmorris | 37:7c4d7f206039 | 57 | //Read Sensors (I2C) |
thomasmorris | 37:7c4d7f206039 | 58 | float temp = sensor.getTemperature(); |
thomasmorris | 37:7c4d7f206039 | 59 | float pressure = sensor.getPressure(); |
thomasmorris | 37:7c4d7f206039 | 60 | #ifdef BME |
thomasmorris | 37:7c4d7f206039 | 61 | float humidity = sensor.getHumidity(); |
thomasmorris | 37:7c4d7f206039 | 62 | #endif |
thomasmorris | 37:7c4d7f206039 | 63 | |
thomasmorris | 37:7c4d7f206039 | 64 | //Display in PuTTY |
thomasmorris | 37:7c4d7f206039 | 65 | printf("Temperature: %5.1f\n", temp); |
thomasmorris | 37:7c4d7f206039 | 66 | printf("Pressure: %5.1f\n", pressure); |
thomasmorris | 37:7c4d7f206039 | 67 | #ifdef BME |
thomasmorris | 37:7c4d7f206039 | 68 | printf("Pressure: %5.1f\n", humidity); |
thomasmorris | 37:7c4d7f206039 | 69 | #endif |
thomasmorris | 37:7c4d7f206039 | 70 | |
thomasmorris | 37:7c4d7f206039 | 71 | //Display on LCD |
thomasmorris | 37:7c4d7f206039 | 72 | redLED = 1; |
thomasmorris | 37:7c4d7f206039 | 73 | //LCD.Display_Clear(); |
thomasmorris | 37:7c4d7f206039 | 74 | //LCD.DDRAM_Address(0x00); |
thomasmorris | 37:7c4d7f206039 | 75 | //LCD.Write_String("LCD TEST..."); |
thomasmorris | 37:7c4d7f206039 | 76 | wait(0.5); |
thomasmorris | 37:7c4d7f206039 | 77 | redLED = 0; |
thomasmorris | 37:7c4d7f206039 | 78 | |
thomasmorris | 37:7c4d7f206039 | 79 | //Network test (if BOTH switches are held down) |
thomasmorris | 37:7c4d7f206039 | 80 | //networktest(); |
thomasmorris | 48:244d6d81bb52 | 81 | NetworkWaitTime = NetworkWait; |
thomasmorris | 48:244d6d81bb52 | 82 | //Initialise the SD card (this needs to move) |
thomasmorris | 48:244d6d81bb52 | 83 | if ( sd.init() != 0) { |
thomasmorris | 48:244d6d81bb52 | 84 | printf("SD Init failed \n"); |
thomasmorris | 48:244d6d81bb52 | 85 | LCD.Display_Clear(); |
thomasmorris | 48:244d6d81bb52 | 86 | LCD.Write_String("CANNOT INIT SD"); //Change me |
thomasmorris | 48:244d6d81bb52 | 87 | errorCode(FATAL); |
thomasmorris | 48:244d6d81bb52 | 88 | } |
thomasmorris | 48:244d6d81bb52 | 89 | //Create a filing system for SD Card |
thomasmorris | 49:d51f96a46cc3 | 90 | FATFileSystem fs("sd", &sd); |
thomasmorris | 48:244d6d81bb52 | 91 | |
thomasmorris | 48:244d6d81bb52 | 92 | //Open to WRITE |
thomasmorris | 49:d51f96a46cc3 | 93 | |
thomasmorris | 49:d51f96a46cc3 | 94 | FILE* fp = fopen("/sd/test.csv","w"); |
thomasmorris | 48:244d6d81bb52 | 95 | if (fp == NULL) { |
thomasmorris | 48:244d6d81bb52 | 96 | error("Could not open file for write\n"); |
thomasmorris | 48:244d6d81bb52 | 97 | LCD.Display_Clear(); |
thomasmorris | 48:244d6d81bb52 | 98 | LCD.Write_String("CANNOT OPEN FILE"); |
thomasmorris | 48:244d6d81bb52 | 99 | errorCode(FATAL); |
thomasmorris | 48:244d6d81bb52 | 100 | } |
thomasmorris | 48:244d6d81bb52 | 101 | //Close File |
thomasmorris | 48:244d6d81bb52 | 102 | fclose(fp); |
thomasmorris | 49:d51f96a46cc3 | 103 | |
thomasmorris | 48:244d6d81bb52 | 104 | int network_temp; |
thomasmorris | 48:244d6d81bb52 | 105 | network_temp = Network_Init(); |
thomasmorris | 48:244d6d81bb52 | 106 | if(network_temp == 1)//Sets up the network and checks if the network cable is not pluged in |
thomasmorris | 48:244d6d81bb52 | 107 | { |
thomasmorris | 48:244d6d81bb52 | 108 | error("Could not access network"); |
thomasmorris | 48:244d6d81bb52 | 109 | LCD.Display_Clear(); |
thomasmorris | 48:244d6d81bb52 | 110 | LCD.Write_String("Could not access network"); |
thomasmorris | 48:244d6d81bb52 | 111 | errorCode(NETWORK_FATAL); |
thomasmorris | 48:244d6d81bb52 | 112 | } |
thomasmorris | 48:244d6d81bb52 | 113 | //Last message before sampling begins |
thomasmorris | 48:244d6d81bb52 | 114 | LCD.Display_Clear(); |
thomasmorris | 48:244d6d81bb52 | 115 | LCD.Write_String("READY PLAYER"); |
thomasmorris | 48:244d6d81bb52 | 116 | LCD.DDRAM_Address(0x40); |
thomasmorris | 48:244d6d81bb52 | 117 | LCD.Write_String(" One "); |
thomasmorris | 48:244d6d81bb52 | 118 | LCD.DDRAM_Address(0x00); |
thomasmorris | 48:244d6d81bb52 | 119 | puts("**********POST END**********\n"); |
thomasmorris | 37:7c4d7f206039 | 120 | |
thomasmorris | 37:7c4d7f206039 | 121 | } |
thomasmorris | 37:7c4d7f206039 | 122 | |
thomasmorris | 37:7c4d7f206039 | 123 | void errorCode(ELEC350_ERROR_CODE err) |
thomasmorris | 37:7c4d7f206039 | 124 | { |
thomasmorris | 37:7c4d7f206039 | 125 | switch (err) { |
thomasmorris | 37:7c4d7f206039 | 126 | case OK: |
thomasmorris | 37:7c4d7f206039 | 127 | greenLED = 1; |
thomasmorris | 37:7c4d7f206039 | 128 | wait(1.0); |
thomasmorris | 37:7c4d7f206039 | 129 | greenLED = 0; |
thomasmorris | 41:859b5e1e3d9a | 130 | return; |
thomasmorris | 41:859b5e1e3d9a | 131 | case NETWORK_FATAL: |
thomasmorris | 41:859b5e1e3d9a | 132 | while(1) { |
thomasmorris | 49:d51f96a46cc3 | 133 | pc.printf("NETWOK FATAL\n"); |
thomasmorris | 49:d51f96a46cc3 | 134 | } |
thomasmorris | 49:d51f96a46cc3 | 135 | case SD_CARD_REMOVED: |
thomasmorris | 41:859b5e1e3d9a | 136 | wait(0.1); |
thomasmorris | 49:d51f96a46cc3 | 137 | LCD.Display_Clear(); |
thomasmorris | 49:d51f96a46cc3 | 138 | LCD.DDRAM_Address(0x00); |
thomasmorris | 49:d51f96a46cc3 | 139 | LCD.Write_String("SD CARD"); |
thomasmorris | 49:d51f96a46cc3 | 140 | LCD.DDRAM_Address(0x40); |
thomasmorris | 49:d51f96a46cc3 | 141 | LCD.Write_String("Removed"); |
thomasmorris | 49:d51f96a46cc3 | 142 | |
thomasmorris | 49:d51f96a46cc3 | 143 | wait(5); |
thomasmorris | 49:d51f96a46cc3 | 144 | pc.printf("SD CARD REMOVED\n"); |
thomasmorris | 49:d51f96a46cc3 | 145 | while(1) |
thomasmorris | 49:d51f96a46cc3 | 146 | { |
thomasmorris | 49:d51f96a46cc3 | 147 | if(SD_CARD_DETECT.read() == 0) |
thomasmorris | 49:d51f96a46cc3 | 148 | { |
thomasmorris | 49:d51f96a46cc3 | 149 | LCD.Display_Clear(); |
thomasmorris | 49:d51f96a46cc3 | 150 | LCD.DDRAM_Address(0x00); |
thomasmorris | 49:d51f96a46cc3 | 151 | LCD.Write_String("SD CARD"); |
thomasmorris | 49:d51f96a46cc3 | 152 | LCD.DDRAM_Address(0x40); |
thomasmorris | 49:d51f96a46cc3 | 153 | LCD.Write_String("Inserted"); |
thomasmorris | 49:d51f96a46cc3 | 154 | pc.printf("SD CARD INSERTED\n"); |
thomasmorris | 49:d51f96a46cc3 | 155 | SD_Write = 1; |
thomasmorris | 49:d51f96a46cc3 | 156 | return; |
thomasmorris | 49:d51f96a46cc3 | 157 | } |
thomasmorris | 49:d51f96a46cc3 | 158 | } |
thomasmorris | 49:d51f96a46cc3 | 159 | case FATAL: |
thomasmorris | 37:7c4d7f206039 | 160 | while(1) { |
thomasmorris | 49:d51f96a46cc3 | 161 | pc.printf("FATAL\n"); |
thomasmorris | 37:7c4d7f206039 | 162 | } |
thomasmorris | 37:7c4d7f206039 | 163 | }; |
thomasmorris | 37:7c4d7f206039 | 164 | } |