Sensor Data - first assignment CO838

Dependencies:   mbed C12832 FXOS8700Q LM75B eCompass_FPU_Lib

Sensor Data - Project developed by Jean-Paul Saysana (jls44)

First assignment for the Internet of Things and Mobile Devices CO838 module

University of Kent (2016-2017)

Functionalities:

- Temperature Sensor

- Compass

- Music box

- Potentiometer that changes LED colours

Libraries used: C12832, eCompass_FPU_Lib, FXOS8700Q, LM75B

Committer:
co838_jls44
Date:
Fri Feb 24 14:33:41 2017 +0000
Revision:
1:c54902f21aa8
Parent:
0:4b83b332b327
comment about the playnote method

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_jls44 0:4b83b332b327 1 /* Developed by Jean-Paul Saysana - jls44 - MSc Student in Computer Security */
co838_jls44 0:4b83b332b327 2 /* Internet of Things and Mobile Devices - CO838 University of Kent */
co838_jls44 0:4b83b332b327 3 /* Class SensorData which is the main Object of this project */
co838_jls44 0:4b83b332b327 4 /* It contains every objects and sensor of this project and combine it together*/
co838_jls44 0:4b83b332b327 5
co838_jls44 0:4b83b332b327 6 #include "SensorData.h"
co838_jls44 0:4b83b332b327 7
co838_jls44 0:4b83b332b327 8 /* Instantiate UP, DOWN, BACK(SW2), ENTER(SW3) buttons. */
co838_jls44 0:4b83b332b327 9 InterruptIn up(A2);
co838_jls44 0:4b83b332b327 10 InterruptIn down(A3);
co838_jls44 0:4b83b332b327 11 InterruptIn enter(D4);
co838_jls44 0:4b83b332b327 12 InterruptIn pause(SW2);
co838_jls44 0:4b83b332b327 13 InterruptIn back(SW3);
co838_jls44 0:4b83b332b327 14
co838_jls44 0:4b83b332b327 15 /* Instantiate the potentiometer and the serial host to communicate with a computer. */
co838_jls44 0:4b83b332b327 16 AnalogIn pot (A1);
co838_jls44 0:4b83b332b327 17 Serial host(USBTX, USBRX);
co838_jls44 0:4b83b332b327 18
co838_jls44 0:4b83b332b327 19 /* Initialize the object lcd, speaker, compass and led */
co838_jls44 0:4b83b332b327 20 SensorData::SensorData() : lcd(D11, D13, D12, D7, D10), speaker(D6),
co838_jls44 0:4b83b332b327 21 compass(FXOS8700CQ_SLAVE_ADDR1), led(D5, D8, D9) {
co838_jls44 0:4b83b332b327 22 /* Setting every value to default, the position of the selector at 0 */
co838_jls44 0:4b83b332b327 23 pos = 0;
co838_jls44 0:4b83b332b327 24 /* Point temperature */
co838_jls44 0:4b83b332b327 25 epage = TEMPERATURE;
co838_jls44 0:4b83b332b327 26 /* setting the wait time to 0.7s by default */
co838_jls44 0:4b83b332b327 27 delay = 0.7;
co838_jls44 0:4b83b332b327 28 /* if quit == 3, end the program */
co838_jls44 0:4b83b332b327 29 quit = 0;
co838_jls44 0:4b83b332b327 30 /* pause variable set to false */
co838_jls44 0:4b83b332b327 31 pause_sensor = false;
co838_jls44 0:4b83b332b327 32 }
co838_jls44 0:4b83b332b327 33
co838_jls44 0:4b83b332b327 34 /* Init the interruption commmand */
co838_jls44 0:4b83b332b327 35 void SensorData::InitCommand() {
co838_jls44 0:4b83b332b327 36 up.rise(callback(this, &SensorData::Up));
co838_jls44 0:4b83b332b327 37 down.rise(callback(this, &SensorData::Down));
co838_jls44 0:4b83b332b327 38 enter.rise(callback(this, &SensorData::Enter));
co838_jls44 0:4b83b332b327 39 pause.rise(callback(this, &SensorData::Pause));
co838_jls44 0:4b83b332b327 40 back.rise(callback(this, &SensorData::Back));
co838_jls44 0:4b83b332b327 41 }
co838_jls44 0:4b83b332b327 42
co838_jls44 0:4b83b332b327 43 /* Method that start the SensorData */
co838_jls44 0:4b83b332b327 44 void SensorData::Start() {
co838_jls44 0:4b83b332b327 45 /* Display the welcome animation when user start the mbed card. */
co838_jls44 0:4b83b332b327 46 lcd.Welcome();
co838_jls44 0:4b83b332b327 47 /* Initialize the buttons(interruptions) */
co838_jls44 0:4b83b332b327 48 InitCommand();
co838_jls44 0:4b83b332b327 49 /* Display the menu selection */
co838_jls44 0:4b83b332b327 50 MenuSelection();
co838_jls44 0:4b83b332b327 51 /* Loop indefinetly until user quit the program. */
co838_jls44 0:4b83b332b327 52 while(quit < 3) {
co838_jls44 0:4b83b332b327 53 /* Main loop checking the position of the user */
co838_jls44 0:4b83b332b327 54 MainLoop();
co838_jls44 0:4b83b332b327 55 /* Waiting delay time for user display */
co838_jls44 0:4b83b332b327 56 wait(delay);
co838_jls44 0:4b83b332b327 57 }
co838_jls44 0:4b83b332b327 58 lcd.PrintQuit();
co838_jls44 0:4b83b332b327 59 }
co838_jls44 0:4b83b332b327 60
co838_jls44 0:4b83b332b327 61 /* Change LED color depending on the degree of the potentiometer */
co838_jls44 0:4b83b332b327 62 void SensorData::ChangeLED() {
co838_jls44 0:4b83b332b327 63 if ((float)pot < 0.20f) {
co838_jls44 0:4b83b332b327 64 return led.SwitchOffAll();
co838_jls44 0:4b83b332b327 65 }
co838_jls44 0:4b83b332b327 66 else if ((float)pot > 0.80f) {
co838_jls44 0:4b83b332b327 67 return led.SwitchOn(LED::BLUE);
co838_jls44 0:4b83b332b327 68 }
co838_jls44 0:4b83b332b327 69 else if ((float)pot > 0.60f) {
co838_jls44 0:4b83b332b327 70 return led.SwitchOn(LED::GREEN);
co838_jls44 0:4b83b332b327 71 }
co838_jls44 0:4b83b332b327 72 else if ((float)pot > 0.40f) {
co838_jls44 0:4b83b332b327 73 return led.SwitchOn(LED::RED);
co838_jls44 0:4b83b332b327 74 }
co838_jls44 0:4b83b332b327 75 }
co838_jls44 0:4b83b332b327 76
co838_jls44 0:4b83b332b327 77 /* Main loop that check the page of the player */
co838_jls44 0:4b83b332b327 78 void SensorData::MainLoop() {
co838_jls44 0:4b83b332b327 79 switch (epage) {
co838_jls44 0:4b83b332b327 80 /* if the player clicked on "Current temperature", it displays the temperature */
co838_jls44 0:4b83b332b327 81 case ENTER_TEMP:
co838_jls44 0:4b83b332b327 82 if (!pause_sensor) {
co838_jls44 0:4b83b332b327 83 DisplayTemperature();
co838_jls44 0:4b83b332b327 84 }
co838_jls44 0:4b83b332b327 85 break;
co838_jls44 0:4b83b332b327 86 /* if the player clicked on "Compass", it displays the compass */
co838_jls44 0:4b83b332b327 87 case ENTER_COMPASS:
co838_jls44 0:4b83b332b327 88 if (!pause_sensor) {
co838_jls44 0:4b83b332b327 89 DisplayCompass();
co838_jls44 0:4b83b332b327 90 }
co838_jls44 0:4b83b332b327 91 break;
co838_jls44 0:4b83b332b327 92 default:
co838_jls44 0:4b83b332b327 93 break;
co838_jls44 0:4b83b332b327 94 }
co838_jls44 0:4b83b332b327 95 /* Checking */
co838_jls44 0:4b83b332b327 96 ChangeLED();
co838_jls44 0:4b83b332b327 97 }
co838_jls44 0:4b83b332b327 98
co838_jls44 0:4b83b332b327 99 /* Display the temperature in the LCD screen */
co838_jls44 0:4b83b332b327 100 void SensorData::DisplayTemperature() {
co838_jls44 0:4b83b332b327 101 lcd.Clear();
co838_jls44 0:4b83b332b327 102 /* Get the temperature unit. If it's in fahrenheit, unit character is 'F'
co838_jls44 0:4b83b332b327 103 Otherwise, it's set to 'C'. */
co838_jls44 0:4b83b332b327 104 char t_unit = (ts.IsFahrenheit() == true ? 'F' : 'C');
co838_jls44 0:4b83b332b327 105 /* Print in the LCD screen. */
co838_jls44 0:4b83b332b327 106 lcd.PrintDelay(delay);
co838_jls44 0:4b83b332b327 107 lcd.PrintTemperature(ts.GetTemperature(), t_unit);
co838_jls44 0:4b83b332b327 108 /* Report temperature over USB Serial link*/
co838_jls44 0:4b83b332b327 109 host.printf("Temperature is %.2f %c \r\n", ts.GetTemperature(), t_unit);
co838_jls44 0:4b83b332b327 110 }
co838_jls44 0:4b83b332b327 111
co838_jls44 0:4b83b332b327 112 /* Display compass */
co838_jls44 0:4b83b332b327 113 void SensorData::DisplayCompass() {
co838_jls44 0:4b83b332b327 114 lcd.Clear();
co838_jls44 0:4b83b332b327 115 /* Get compass angle */
co838_jls44 0:4b83b332b327 116 float angle = compass.GetCompass();
co838_jls44 0:4b83b332b327 117 /* If the angle is below or between certains values. */
co838_jls44 0:4b83b332b327 118 /* the dir variable will store the direction. */
co838_jls44 0:4b83b332b327 119 std::string dir = "";
co838_jls44 0:4b83b332b327 120 if (angle > 45 && angle <= 135) { dir = "East"; }
co838_jls44 0:4b83b332b327 121 else if (angle > 135 && angle <= 225) { dir = "South"; }
co838_jls44 0:4b83b332b327 122 else if (angle > 225 && angle <= 315) { dir = "West"; }
co838_jls44 0:4b83b332b327 123 else { dir = "North"; }
co838_jls44 0:4b83b332b327 124 lcd.PrintDelay(delay);
co838_jls44 0:4b83b332b327 125 lcd.PrintCompass(angle, dir);
co838_jls44 0:4b83b332b327 126 host.printf("Compass angle = %.2f - Direction = %s \r\n", compass.GetCompass(), dir.c_str());
co838_jls44 0:4b83b332b327 127 }
co838_jls44 0:4b83b332b327 128
co838_jls44 0:4b83b332b327 129 /* Interruption with enter button */
co838_jls44 0:4b83b332b327 130 void SensorData::Enter() {
co838_jls44 0:4b83b332b327 131 // set quit variable at 0
co838_jls44 0:4b83b332b327 132 quit = 0;
co838_jls44 0:4b83b332b327 133 /* play sound, enter to the page, switch mode for the temperature. */
co838_jls44 0:4b83b332b327 134 speaker.Fire();
co838_jls44 0:4b83b332b327 135 switch (epage) {
co838_jls44 0:4b83b332b327 136 case TEMPERATURE:
co838_jls44 0:4b83b332b327 137 epage = ENTER_TEMP;
co838_jls44 0:4b83b332b327 138 break;
co838_jls44 0:4b83b332b327 139 case COMPASS:
co838_jls44 0:4b83b332b327 140 epage = ENTER_COMPASS;
co838_jls44 0:4b83b332b327 141 break;
co838_jls44 0:4b83b332b327 142 case MUSIC:
co838_jls44 0:4b83b332b327 143 speaker.PlayMusic();
co838_jls44 0:4b83b332b327 144 break;
co838_jls44 0:4b83b332b327 145 case ENTER_TEMP:
co838_jls44 0:4b83b332b327 146 ts.SwitchMode();
co838_jls44 0:4b83b332b327 147 break;
co838_jls44 0:4b83b332b327 148 case ENTER_COMPASS:
co838_jls44 0:4b83b332b327 149 break;
co838_jls44 0:4b83b332b327 150 default:
co838_jls44 0:4b83b332b327 151 break;
co838_jls44 0:4b83b332b327 152 }
co838_jls44 0:4b83b332b327 153 }
co838_jls44 0:4b83b332b327 154
co838_jls44 0:4b83b332b327 155 void SensorData::SwitchPause() {
co838_jls44 0:4b83b332b327 156 pause_sensor = (pause_sensor == false ? true : false);
co838_jls44 0:4b83b332b327 157 }
co838_jls44 0:4b83b332b327 158
co838_jls44 0:4b83b332b327 159 /* Press back, show back menu selection */
co838_jls44 0:4b83b332b327 160 void SensorData::Pause() {
co838_jls44 0:4b83b332b327 161 speaker.Fire();
co838_jls44 0:4b83b332b327 162 switch (epage) {
co838_jls44 0:4b83b332b327 163 case ENTER_COMPASS:
co838_jls44 0:4b83b332b327 164 SwitchPause();
co838_jls44 0:4b83b332b327 165 break;
co838_jls44 0:4b83b332b327 166 case ENTER_TEMP:
co838_jls44 0:4b83b332b327 167 SwitchPause();
co838_jls44 0:4b83b332b327 168 break;
co838_jls44 0:4b83b332b327 169 default:
co838_jls44 0:4b83b332b327 170 break;
co838_jls44 0:4b83b332b327 171 }
co838_jls44 0:4b83b332b327 172 }
co838_jls44 0:4b83b332b327 173
co838_jls44 0:4b83b332b327 174 /* Press back, show back menu selection */
co838_jls44 0:4b83b332b327 175 void SensorData::Back() {
co838_jls44 0:4b83b332b327 176 pause_sensor = false;
co838_jls44 0:4b83b332b327 177 speaker.Fire();
co838_jls44 0:4b83b332b327 178 switch (epage) {
co838_jls44 0:4b83b332b327 179 case ENTER_COMPASS:
co838_jls44 0:4b83b332b327 180 epage = COMPASS;
co838_jls44 0:4b83b332b327 181 break;
co838_jls44 0:4b83b332b327 182 case ENTER_TEMP:
co838_jls44 0:4b83b332b327 183 epage = TEMPERATURE;
co838_jls44 0:4b83b332b327 184 break;
co838_jls44 0:4b83b332b327 185 default:
co838_jls44 0:4b83b332b327 186 ++quit;
co838_jls44 0:4b83b332b327 187 break;
co838_jls44 0:4b83b332b327 188 }
co838_jls44 0:4b83b332b327 189 MenuSelection();
co838_jls44 0:4b83b332b327 190 }
co838_jls44 0:4b83b332b327 191
co838_jls44 0:4b83b332b327 192 /* Press up button, change delay if entered somewhere else change selector position */
co838_jls44 0:4b83b332b327 193 void SensorData::Up() {
co838_jls44 0:4b83b332b327 194 // set variable quit at 0
co838_jls44 0:4b83b332b327 195 quit = 0;
co838_jls44 0:4b83b332b327 196 switch (epage) {
co838_jls44 0:4b83b332b327 197 case ENTER_TEMP:
co838_jls44 0:4b83b332b327 198 if (delay < 2) {
co838_jls44 0:4b83b332b327 199 delay += 0.1f;
co838_jls44 0:4b83b332b327 200 }
co838_jls44 0:4b83b332b327 201 break;
co838_jls44 0:4b83b332b327 202 case ENTER_COMPASS:
co838_jls44 0:4b83b332b327 203 if (delay < 2) {
co838_jls44 0:4b83b332b327 204 delay += 0.1f;
co838_jls44 0:4b83b332b327 205 }
co838_jls44 0:4b83b332b327 206 break;
co838_jls44 0:4b83b332b327 207 default:
co838_jls44 0:4b83b332b327 208 if (pos > 0) {
co838_jls44 0:4b83b332b327 209 --pos;
co838_jls44 0:4b83b332b327 210 ChangePosition();
co838_jls44 0:4b83b332b327 211 }
co838_jls44 0:4b83b332b327 212 break;
co838_jls44 0:4b83b332b327 213 }
co838_jls44 0:4b83b332b327 214 }
co838_jls44 0:4b83b332b327 215
co838_jls44 0:4b83b332b327 216 /* press down and change position*/
co838_jls44 0:4b83b332b327 217 void SensorData::Down() {
co838_jls44 0:4b83b332b327 218 // set variable quit at 0
co838_jls44 0:4b83b332b327 219 quit = 0;
co838_jls44 0:4b83b332b327 220 switch (epage) {
co838_jls44 0:4b83b332b327 221 case ENTER_TEMP:
co838_jls44 0:4b83b332b327 222 if (delay > 0.4f) {
co838_jls44 0:4b83b332b327 223 delay -= 0.1f;
co838_jls44 0:4b83b332b327 224 }
co838_jls44 0:4b83b332b327 225 break;
co838_jls44 0:4b83b332b327 226 case ENTER_COMPASS:
co838_jls44 0:4b83b332b327 227 if (delay > 0.4f) {
co838_jls44 0:4b83b332b327 228 delay -= 0.1f;
co838_jls44 0:4b83b332b327 229 }
co838_jls44 0:4b83b332b327 230 break;
co838_jls44 0:4b83b332b327 231 default:
co838_jls44 0:4b83b332b327 232 if (pos < MUSIC) {
co838_jls44 0:4b83b332b327 233 ++pos;
co838_jls44 0:4b83b332b327 234 ChangePosition();
co838_jls44 0:4b83b332b327 235 }
co838_jls44 0:4b83b332b327 236 break;
co838_jls44 0:4b83b332b327 237 }
co838_jls44 0:4b83b332b327 238 }
co838_jls44 0:4b83b332b327 239
co838_jls44 0:4b83b332b327 240 /* change epage position */
co838_jls44 0:4b83b332b327 241 void SensorData::ChangePosition() {
co838_jls44 0:4b83b332b327 242 switch (pos) {
co838_jls44 0:4b83b332b327 243 case TEMPERATURE:
co838_jls44 0:4b83b332b327 244 epage = TEMPERATURE;
co838_jls44 0:4b83b332b327 245 break;
co838_jls44 0:4b83b332b327 246 case COMPASS:
co838_jls44 0:4b83b332b327 247 epage = COMPASS;
co838_jls44 0:4b83b332b327 248 break;
co838_jls44 0:4b83b332b327 249 case MUSIC:
co838_jls44 0:4b83b332b327 250 epage = MUSIC;
co838_jls44 0:4b83b332b327 251 break;
co838_jls44 0:4b83b332b327 252 default:
co838_jls44 0:4b83b332b327 253 break;
co838_jls44 0:4b83b332b327 254 }
co838_jls44 0:4b83b332b327 255 MenuSelection();
co838_jls44 0:4b83b332b327 256 speaker.Move();
co838_jls44 0:4b83b332b327 257 }
co838_jls44 0:4b83b332b327 258
co838_jls44 0:4b83b332b327 259 /* Print menu selection */
co838_jls44 0:4b83b332b327 260 void SensorData::MenuSelection() {
co838_jls44 0:4b83b332b327 261 wait(0.1);
co838_jls44 0:4b83b332b327 262 lcd.Clear();
co838_jls44 0:4b83b332b327 263 lcd.PrintMenu(pos);
co838_jls44 0:4b83b332b327 264 }