This library demonstrates how to use the sensors on the QW Shield.
Dependents: QW-TEMP_GPS-NMEA QW-Motiondetection QW-Closet-detection
Revision 1:acabcc8fed83, committed 2015-12-02
- Comitter:
- quicksand
- Date:
- Wed Dec 02 11:40:48 2015 +0000
- Parent:
- 0:4b56d28cc7e9
- Child:
- 2:98a0b0ea3457
- Commit message:
- This library demonstrates how to use the sensors on the QW Shield.
Changed in this revision
| 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/main.cpp Wed Dec 02 11:40:48 2015 +0000
@@ -0,0 +1,253 @@
+/* This program demonstrates how one can use all the sensors on the QW shield.
+ * Open a serial console to the board to get a readout of all the onboard sensors.
+ */
+
+#include "mbed.h"
+#include "math.h"
+#include "LinearTempSensor.h"
+#include "VCNL4010.h"
+
+#define SER_BUFFER_SIZE 32
+
+/* The 4 onboard LEDs */
+DigitalOut LED_0 (PB_6);
+DigitalOut LED_1 (PA_7);
+DigitalOut LED_2 (PA_6);
+DigitalOut LED_3 (PA_5);
+
+/* The 2 user buttons */
+InterruptIn SW1(PA_8);
+InterruptIn SW2(PB_10);
+
+/* Proximity and ambient light sensor*/
+VCNL40x0 VCNL4010(PB_9, PB_8, VCNL40x0_ADDRESS); // SDA, SCL pin and I2C address
+
+/*Temperature sensor */
+LinearTempSensor sensor(PA_0);
+
+/* Function prototypes */
+void sw1interrupt();
+void sw2interrupt();
+void sertmout();
+bool read_acc(int& x, int& y, int& z);
+bool modem_command_check_ok(char * command);
+void modem_setup();
+
+bool ser_timeout = false;
+
+/* Serial port over USB */
+Serial pc(USBTX, USBRX);
+
+/* Serial connection to sigfox modem */
+Serial modem(PA_9, PA_10);
+
+int main()
+{
+ /* Storage for VCNL4010 readout */
+ unsigned char ID=0, Current=0;
+ unsigned int ProxiValue=0, AmbiValue=0;
+
+ /* Variables that will store analog temperature sensor reading */
+ float Vout, Tav, To;
+
+ /* Setup TD120x */
+ wait(3);
+ modem_setup();
+
+ /* Turn off all LED */
+ LED_0 = 1;
+ LED_1 = 1;
+ LED_2 = 1;
+ LED_3 = 1;
+
+ /* Setup button interrupts */
+ SW2.fall(&sw2interrupt);
+ SW1.fall(&sw1interrupt);
+
+ /* Read VCNL40x0 product ID revision register */
+ VCNL4010.ReadID (&ID);
+ pc.printf("\nVCNL4010 Product ID Revision Register: %d", ID);
+
+ VCNL4010.SetCurrent (20); // Set current to 200mA
+ VCNL4010.ReadCurrent (&Current); // Read back IR LED current
+ pc.printf("\nVCNL4010 IR LED Current: %d\n\n", Current);
+
+ wait_ms(3000); // wait 3s (only for display)
+
+ while(1)
+ {
+ /* VCNL4010 reading */
+ VCNL4010.ReadProxiOnDemand (&ProxiValue); // read prox value on demand
+ VCNL4010.ReadAmbiOnDemand (&AmbiValue); // read ambi value on demand
+ /* MCP9700 reading */
+ Vout = sensor.Sense();
+ Tav = sensor.GetAverageTemp();
+ To = sensor.GetLatestTemp();
+
+
+ /* Fetch accelerometer reading from TD1204 */
+ int x = 0;
+ int y = 0;
+ int z = 0;
+
+ if(read_acc(x, y, z))
+ {
+ pc.printf("\n\rAccelerometer reading: %i X, %i Y, %i Z", x,y,z);
+ }
+ else
+ {
+ pc.printf("\n\rFailed to read accelerometer");
+ }
+
+ 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);
+ wait_ms(1000);
+ }
+}
+
+void modem_setup()
+{
+ /* Reset to factory defaults */
+ if(modem_command_check_ok("AT&F"))
+ {
+ pc.printf("Factory reset succesfull\r\n");
+ }
+ else
+ {
+ pc.printf("Factory reset TD120x failed\r\n");
+ }
+ /* Disable local echo */
+ modem.printf("ATE0\n");
+ if(modem_command_check_ok("ATE0"))
+ {
+ pc.printf("Local echo disabled\r\n");
+ }
+ /* Write to mem */
+ if(modem_command_check_ok("AT&W"))
+ {
+ pc.printf("Settings saved!\r\n");
+ }
+ /* 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 */
+ if(modem_command_check_ok("ATS650=1,0,3,2,0"))
+ {
+ pc.printf("Accellerometer monitoring started successfully\r\n");
+ }
+ else
+ {
+ pc.printf("Accellerometer monitoring start failed\r\n");
+ }
+}
+
+bool modem_command_check_ok(char * command)
+{
+ /* first clear serial data buffers */
+ while(modem.readable()) modem.getc();
+ /* Timeout for response of the modem */
+ Timeout tmout;
+ ser_timeout = false;
+ /* Buffer for incoming data */
+ char responsebuffer[6];
+ /* Flag to set when we get 'OK' response */
+ bool ok = false;
+ bool error = false;
+ /* Print command to TD120x */
+ modem.printf(command);
+ /* Newline to activate command */
+ modem.printf("\n");
+ /* Wait untill serial feedback, max 3 seconds before timeout */
+ tmout.attach(&sertmout, 3.0);
+ while(!modem.readable()&& ser_timeout == false);
+ while(!ok && !ser_timeout && !error)
+ {
+ if(modem.readable())
+ {
+ for(int i = 0; i < 5; i++)
+ {
+ responsebuffer[i] = responsebuffer[i+1];
+ }
+ responsebuffer[5] = modem.getc();
+ if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' )
+ {
+ ok = true;
+ }
+ else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' )
+ {
+ error = true;
+ }
+ }
+ }
+ tmout.detach();
+ return ok;
+}
+
+bool read_acc(int& x, int& y, int& z)
+{
+ /* first clear serial data buffers */
+ while(modem.readable()) modem.getc();
+ /* Timeout for response of the modem */
+ Timeout tmout;
+ ser_timeout = false;
+ /* counter to fill buffer */
+ int i = 0;
+ /* Buffer for incoming data */
+ char responsebuffer[SER_BUFFER_SIZE];
+ /* Flag to set when we get to the end of the data '/r' response */
+ bool end = false;
+ /* Wait untill serial feedback, max 3 seconds before timeout */
+ tmout.attach(&sertmout, 3.0);
+ do{
+ while(!modem.readable()&& ser_timeout == false);
+ /* Wait untill '\n' from last data*/
+ }while((modem.getc() != '\n') && ser_timeout == false);
+ while(end == false && ser_timeout == false && i < SER_BUFFER_SIZE)
+ {
+ if(modem.readable()) {
+ responsebuffer[i] = modem.getc();
+ if(responsebuffer[i] == '\n') {
+ end = true;
+ responsebuffer[i] = 0;
+ }
+ i++;
+ }
+
+ }
+ tmout.detach();
+ /* What is left now is getting the X,Y,Z coordinates out of the string */
+ if(end)
+ {
+ char * token;
+ token = strtok(responsebuffer, " ");
+ x = atoi(token);
+ token = strtok(NULL, " ");
+ token = strtok(NULL, " ");
+ y = atoi(token);
+ token = strtok(NULL, " ");
+ token = strtok(NULL, " ");
+ z = atoi(token);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+/* Button 1 ISR */
+void sw1interrupt()
+{
+ pc.printf("\n\rButton 1 pressed\n\r");
+}
+
+/* Button 2 ISR */
+void sw2interrupt()
+{
+ pc.printf("\n\rButton 2 pressed\n\r");
+}
+
+/* ISR for serial timeout */
+void sertmout()
+{
+ ser_timeout = true;
+}
+
+
+
QW SIGFOX Development Kit