Clone of the Quicksand QW Sensors library

Fork of QW_Sensors by Quicksand micro-electronics

Committer:
quicksand
Date:
Wed Dec 02 11:49:27 2015 +0000
Revision:
2:98a0b0ea3457
Parent:
1:acabcc8fed83
Minor changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quicksand 1:acabcc8fed83 1 /* This program demonstrates how one can use all the sensors on the QW shield.
quicksand 1:acabcc8fed83 2 * Open a serial console to the board to get a readout of all the onboard sensors.
quicksand 1:acabcc8fed83 3 */
quicksand 1:acabcc8fed83 4
quicksand 1:acabcc8fed83 5 #include "mbed.h"
quicksand 1:acabcc8fed83 6 #include "math.h"
quicksand 1:acabcc8fed83 7 #include "LinearTempSensor.h"
quicksand 1:acabcc8fed83 8 #include "VCNL4010.h"
quicksand 1:acabcc8fed83 9
quicksand 1:acabcc8fed83 10 #define SER_BUFFER_SIZE 32
quicksand 1:acabcc8fed83 11
quicksand 1:acabcc8fed83 12 /* The 4 onboard LEDs */
quicksand 1:acabcc8fed83 13 DigitalOut LED_0 (PB_6);
quicksand 1:acabcc8fed83 14 DigitalOut LED_1 (PA_7);
quicksand 1:acabcc8fed83 15 DigitalOut LED_2 (PA_6);
quicksand 1:acabcc8fed83 16 DigitalOut LED_3 (PA_5);
quicksand 1:acabcc8fed83 17
quicksand 1:acabcc8fed83 18 /* The 2 user buttons */
quicksand 1:acabcc8fed83 19 InterruptIn SW1(PA_8);
quicksand 1:acabcc8fed83 20 InterruptIn SW2(PB_10);
quicksand 1:acabcc8fed83 21
quicksand 1:acabcc8fed83 22 /* Proximity and ambient light sensor*/
quicksand 1:acabcc8fed83 23 VCNL40x0 VCNL4010(PB_9, PB_8, VCNL40x0_ADDRESS); // SDA, SCL pin and I2C address
quicksand 1:acabcc8fed83 24
quicksand 1:acabcc8fed83 25 /*Temperature sensor */
quicksand 1:acabcc8fed83 26 LinearTempSensor sensor(PA_0);
quicksand 1:acabcc8fed83 27
quicksand 1:acabcc8fed83 28 /* Function prototypes */
quicksand 1:acabcc8fed83 29 void sw1interrupt();
quicksand 1:acabcc8fed83 30 void sw2interrupt();
quicksand 1:acabcc8fed83 31 void sertmout();
quicksand 1:acabcc8fed83 32 bool read_acc(int& x, int& y, int& z);
quicksand 1:acabcc8fed83 33 bool modem_command_check_ok(char * command);
quicksand 1:acabcc8fed83 34 void modem_setup();
quicksand 1:acabcc8fed83 35
quicksand 1:acabcc8fed83 36 bool ser_timeout = false;
quicksand 1:acabcc8fed83 37
quicksand 1:acabcc8fed83 38 /* Serial port over USB */
quicksand 1:acabcc8fed83 39 Serial pc(USBTX, USBRX);
quicksand 1:acabcc8fed83 40
quicksand 1:acabcc8fed83 41 /* Serial connection to sigfox modem */
quicksand 1:acabcc8fed83 42 Serial modem(PA_9, PA_10);
quicksand 1:acabcc8fed83 43
quicksand 1:acabcc8fed83 44 int main()
quicksand 1:acabcc8fed83 45 {
quicksand 1:acabcc8fed83 46 /* Storage for VCNL4010 readout */
quicksand 1:acabcc8fed83 47 unsigned char ID=0, Current=0;
quicksand 1:acabcc8fed83 48 unsigned int ProxiValue=0, AmbiValue=0;
quicksand 1:acabcc8fed83 49
quicksand 1:acabcc8fed83 50 /* Variables that will store analog temperature sensor reading */
quicksand 1:acabcc8fed83 51 float Vout, Tav, To;
quicksand 1:acabcc8fed83 52
quicksand 1:acabcc8fed83 53 /* Setup TD120x */
quicksand 1:acabcc8fed83 54 wait(3);
quicksand 1:acabcc8fed83 55 modem_setup();
quicksand 1:acabcc8fed83 56
quicksand 1:acabcc8fed83 57 /* Turn off all LED */
quicksand 1:acabcc8fed83 58 LED_0 = 1;
quicksand 1:acabcc8fed83 59 LED_1 = 1;
quicksand 1:acabcc8fed83 60 LED_2 = 1;
quicksand 1:acabcc8fed83 61 LED_3 = 1;
quicksand 1:acabcc8fed83 62
quicksand 1:acabcc8fed83 63 /* Setup button interrupts */
quicksand 1:acabcc8fed83 64 SW2.fall(&sw2interrupt);
quicksand 1:acabcc8fed83 65 SW1.fall(&sw1interrupt);
quicksand 1:acabcc8fed83 66
quicksand 1:acabcc8fed83 67 /* Read VCNL40x0 product ID revision register */
quicksand 1:acabcc8fed83 68 VCNL4010.ReadID (&ID);
quicksand 1:acabcc8fed83 69 pc.printf("\nVCNL4010 Product ID Revision Register: %d", ID);
quicksand 1:acabcc8fed83 70
quicksand 1:acabcc8fed83 71 VCNL4010.SetCurrent (20); // Set current to 200mA
quicksand 1:acabcc8fed83 72 VCNL4010.ReadCurrent (&Current); // Read back IR LED current
quicksand 1:acabcc8fed83 73 pc.printf("\nVCNL4010 IR LED Current: %d\n\n", Current);
quicksand 1:acabcc8fed83 74
quicksand 1:acabcc8fed83 75 wait_ms(3000); // wait 3s (only for display)
quicksand 1:acabcc8fed83 76
quicksand 1:acabcc8fed83 77 while(1)
quicksand 1:acabcc8fed83 78 {
quicksand 1:acabcc8fed83 79 /* VCNL4010 reading */
quicksand 1:acabcc8fed83 80 VCNL4010.ReadProxiOnDemand (&ProxiValue); // read prox value on demand
quicksand 1:acabcc8fed83 81 VCNL4010.ReadAmbiOnDemand (&AmbiValue); // read ambi value on demand
quicksand 1:acabcc8fed83 82 /* MCP9700 reading */
quicksand 1:acabcc8fed83 83 Vout = sensor.Sense();
quicksand 1:acabcc8fed83 84 Tav = sensor.GetAverageTemp();
quicksand 1:acabcc8fed83 85 To = sensor.GetLatestTemp();
quicksand 1:acabcc8fed83 86
quicksand 1:acabcc8fed83 87
quicksand 1:acabcc8fed83 88 /* Fetch accelerometer reading from TD1204 */
quicksand 1:acabcc8fed83 89 int x = 0;
quicksand 1:acabcc8fed83 90 int y = 0;
quicksand 1:acabcc8fed83 91 int z = 0;
quicksand 1:acabcc8fed83 92
quicksand 1:acabcc8fed83 93 if(read_acc(x, y, z))
quicksand 1:acabcc8fed83 94 {
quicksand 1:acabcc8fed83 95 pc.printf("\n\rAccelerometer reading: %i X, %i Y, %i Z", x,y,z);
quicksand 1:acabcc8fed83 96 }
quicksand 1:acabcc8fed83 97 else
quicksand 1:acabcc8fed83 98 {
quicksand 1:acabcc8fed83 99 pc.printf("\n\rFailed to read accelerometer");
quicksand 1:acabcc8fed83 100 }
quicksand 1:acabcc8fed83 101
quicksand 1:acabcc8fed83 102 pc.printf("\n\rVCNL4010 reading: Proximity: %5.0i cts, Ambient light: %5.0i cts, Illuminance: %7.2f lx\n\rMCP9700 reading: Vout: %.2f mV, Average Temp: %.2f %cC, Latest Temp: %.2f %cC\n\r", ProxiValue, AmbiValue, AmbiValue/4.0, Vout, Tav, 176, To, 176);
quicksand 1:acabcc8fed83 103 wait_ms(1000);
quicksand 1:acabcc8fed83 104 }
quicksand 1:acabcc8fed83 105 }
quicksand 1:acabcc8fed83 106
quicksand 1:acabcc8fed83 107 void modem_setup()
quicksand 1:acabcc8fed83 108 {
quicksand 1:acabcc8fed83 109 /* Reset to factory defaults */
quicksand 1:acabcc8fed83 110 if(modem_command_check_ok("AT&F"))
quicksand 1:acabcc8fed83 111 {
quicksand 1:acabcc8fed83 112 pc.printf("Factory reset succesfull\r\n");
quicksand 1:acabcc8fed83 113 }
quicksand 1:acabcc8fed83 114 else
quicksand 1:acabcc8fed83 115 {
quicksand 1:acabcc8fed83 116 pc.printf("Factory reset TD120x failed\r\n");
quicksand 1:acabcc8fed83 117 }
quicksand 1:acabcc8fed83 118 /* Disable local echo */
quicksand 1:acabcc8fed83 119 modem.printf("ATE0\n");
quicksand 1:acabcc8fed83 120 if(modem_command_check_ok("ATE0"))
quicksand 1:acabcc8fed83 121 {
quicksand 1:acabcc8fed83 122 pc.printf("Local echo disabled\r\n");
quicksand 1:acabcc8fed83 123 }
quicksand 1:acabcc8fed83 124 /* Write to mem */
quicksand 1:acabcc8fed83 125 if(modem_command_check_ok("AT&W"))
quicksand 1:acabcc8fed83 126 {
quicksand 1:acabcc8fed83 127 pc.printf("Settings saved!\r\n");
quicksand 1:acabcc8fed83 128 }
quicksand 1:acabcc8fed83 129 /* Start accelerometer reading - Data monitoring - Normal power, high resolution, 25 Hz rate, +/-2G full scale, no high pass filter. Ouptput data format is X Y Z */
quicksand 1:acabcc8fed83 130 if(modem_command_check_ok("ATS650=1,0,3,2,0"))
quicksand 1:acabcc8fed83 131 {
quicksand 2:98a0b0ea3457 132 pc.printf("accelerometer monitoring started successfully\r\n");
quicksand 1:acabcc8fed83 133 }
quicksand 1:acabcc8fed83 134 else
quicksand 1:acabcc8fed83 135 {
quicksand 2:98a0b0ea3457 136 pc.printf("accelerometer monitoring start failed\r\n");
quicksand 1:acabcc8fed83 137 }
quicksand 1:acabcc8fed83 138 }
quicksand 1:acabcc8fed83 139
quicksand 1:acabcc8fed83 140 bool modem_command_check_ok(char * command)
quicksand 1:acabcc8fed83 141 {
quicksand 1:acabcc8fed83 142 /* first clear serial data buffers */
quicksand 1:acabcc8fed83 143 while(modem.readable()) modem.getc();
quicksand 1:acabcc8fed83 144 /* Timeout for response of the modem */
quicksand 1:acabcc8fed83 145 Timeout tmout;
quicksand 1:acabcc8fed83 146 ser_timeout = false;
quicksand 1:acabcc8fed83 147 /* Buffer for incoming data */
quicksand 1:acabcc8fed83 148 char responsebuffer[6];
quicksand 1:acabcc8fed83 149 /* Flag to set when we get 'OK' response */
quicksand 1:acabcc8fed83 150 bool ok = false;
quicksand 1:acabcc8fed83 151 bool error = false;
quicksand 1:acabcc8fed83 152 /* Print command to TD120x */
quicksand 1:acabcc8fed83 153 modem.printf(command);
quicksand 1:acabcc8fed83 154 /* Newline to activate command */
quicksand 1:acabcc8fed83 155 modem.printf("\n");
quicksand 1:acabcc8fed83 156 /* Wait untill serial feedback, max 3 seconds before timeout */
quicksand 1:acabcc8fed83 157 tmout.attach(&sertmout, 3.0);
quicksand 1:acabcc8fed83 158 while(!modem.readable()&& ser_timeout == false);
quicksand 1:acabcc8fed83 159 while(!ok && !ser_timeout && !error)
quicksand 1:acabcc8fed83 160 {
quicksand 1:acabcc8fed83 161 if(modem.readable())
quicksand 1:acabcc8fed83 162 {
quicksand 1:acabcc8fed83 163 for(int i = 0; i < 5; i++)
quicksand 1:acabcc8fed83 164 {
quicksand 1:acabcc8fed83 165 responsebuffer[i] = responsebuffer[i+1];
quicksand 1:acabcc8fed83 166 }
quicksand 1:acabcc8fed83 167 responsebuffer[5] = modem.getc();
quicksand 1:acabcc8fed83 168 if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' )
quicksand 1:acabcc8fed83 169 {
quicksand 1:acabcc8fed83 170 ok = true;
quicksand 1:acabcc8fed83 171 }
quicksand 1:acabcc8fed83 172 else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' )
quicksand 1:acabcc8fed83 173 {
quicksand 1:acabcc8fed83 174 error = true;
quicksand 1:acabcc8fed83 175 }
quicksand 1:acabcc8fed83 176 }
quicksand 1:acabcc8fed83 177 }
quicksand 1:acabcc8fed83 178 tmout.detach();
quicksand 1:acabcc8fed83 179 return ok;
quicksand 1:acabcc8fed83 180 }
quicksand 1:acabcc8fed83 181
quicksand 1:acabcc8fed83 182 bool read_acc(int& x, int& y, int& z)
quicksand 1:acabcc8fed83 183 {
quicksand 1:acabcc8fed83 184 /* first clear serial data buffers */
quicksand 1:acabcc8fed83 185 while(modem.readable()) modem.getc();
quicksand 1:acabcc8fed83 186 /* Timeout for response of the modem */
quicksand 1:acabcc8fed83 187 Timeout tmout;
quicksand 1:acabcc8fed83 188 ser_timeout = false;
quicksand 1:acabcc8fed83 189 /* counter to fill buffer */
quicksand 1:acabcc8fed83 190 int i = 0;
quicksand 1:acabcc8fed83 191 /* Buffer for incoming data */
quicksand 1:acabcc8fed83 192 char responsebuffer[SER_BUFFER_SIZE];
quicksand 1:acabcc8fed83 193 /* Flag to set when we get to the end of the data '/r' response */
quicksand 1:acabcc8fed83 194 bool end = false;
quicksand 1:acabcc8fed83 195 /* Wait untill serial feedback, max 3 seconds before timeout */
quicksand 1:acabcc8fed83 196 tmout.attach(&sertmout, 3.0);
quicksand 1:acabcc8fed83 197 do{
quicksand 1:acabcc8fed83 198 while(!modem.readable()&& ser_timeout == false);
quicksand 1:acabcc8fed83 199 /* Wait untill '\n' from last data*/
quicksand 1:acabcc8fed83 200 }while((modem.getc() != '\n') && ser_timeout == false);
quicksand 1:acabcc8fed83 201 while(end == false && ser_timeout == false && i < SER_BUFFER_SIZE)
quicksand 1:acabcc8fed83 202 {
quicksand 1:acabcc8fed83 203 if(modem.readable()) {
quicksand 1:acabcc8fed83 204 responsebuffer[i] = modem.getc();
quicksand 1:acabcc8fed83 205 if(responsebuffer[i] == '\n') {
quicksand 1:acabcc8fed83 206 end = true;
quicksand 1:acabcc8fed83 207 responsebuffer[i] = 0;
quicksand 1:acabcc8fed83 208 }
quicksand 1:acabcc8fed83 209 i++;
quicksand 1:acabcc8fed83 210 }
quicksand 1:acabcc8fed83 211
quicksand 1:acabcc8fed83 212 }
quicksand 1:acabcc8fed83 213 tmout.detach();
quicksand 1:acabcc8fed83 214 /* What is left now is getting the X,Y,Z coordinates out of the string */
quicksand 1:acabcc8fed83 215 if(end)
quicksand 1:acabcc8fed83 216 {
quicksand 1:acabcc8fed83 217 char * token;
quicksand 1:acabcc8fed83 218 token = strtok(responsebuffer, " ");
quicksand 1:acabcc8fed83 219 x = atoi(token);
quicksand 1:acabcc8fed83 220 token = strtok(NULL, " ");
quicksand 1:acabcc8fed83 221 token = strtok(NULL, " ");
quicksand 1:acabcc8fed83 222 y = atoi(token);
quicksand 1:acabcc8fed83 223 token = strtok(NULL, " ");
quicksand 1:acabcc8fed83 224 token = strtok(NULL, " ");
quicksand 1:acabcc8fed83 225 z = atoi(token);
quicksand 1:acabcc8fed83 226 return true;
quicksand 1:acabcc8fed83 227 }
quicksand 1:acabcc8fed83 228 else
quicksand 1:acabcc8fed83 229 {
quicksand 1:acabcc8fed83 230 return false;
quicksand 1:acabcc8fed83 231 }
quicksand 1:acabcc8fed83 232 }
quicksand 1:acabcc8fed83 233
quicksand 1:acabcc8fed83 234 /* Button 1 ISR */
quicksand 1:acabcc8fed83 235 void sw1interrupt()
quicksand 1:acabcc8fed83 236 {
quicksand 1:acabcc8fed83 237 pc.printf("\n\rButton 1 pressed\n\r");
quicksand 1:acabcc8fed83 238 }
quicksand 1:acabcc8fed83 239
quicksand 1:acabcc8fed83 240 /* Button 2 ISR */
quicksand 1:acabcc8fed83 241 void sw2interrupt()
quicksand 1:acabcc8fed83 242 {
quicksand 1:acabcc8fed83 243 pc.printf("\n\rButton 2 pressed\n\r");
quicksand 1:acabcc8fed83 244 }
quicksand 1:acabcc8fed83 245
quicksand 1:acabcc8fed83 246 /* ISR for serial timeout */
quicksand 1:acabcc8fed83 247 void sertmout()
quicksand 1:acabcc8fed83 248 {
quicksand 1:acabcc8fed83 249 ser_timeout = true;
quicksand 1:acabcc8fed83 250 }
quicksand 1:acabcc8fed83 251
quicksand 1:acabcc8fed83 252
quicksand 1:acabcc8fed83 253