![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Sensor Box version 090715 17:06
Dependencies: MMA8451Q mbed nRF24L01P
main.cpp@2:3779f28a6bf0, 2015-07-09 (annotated)
- Committer:
- eleim
- Date:
- Thu Jul 09 19:03:22 2015 +0000
- Revision:
- 2:3779f28a6bf0
- Parent:
- 1:00312af3c20e
- Child:
- 3:391141bdd69a
090715 20:03;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eleim | 0:84ecd53a6958 | 1 | #include "mbed.h" |
eleim | 0:84ecd53a6958 | 2 | #include "nRF24L01P.h" |
eleim | 0:84ecd53a6958 | 3 | #include "MMA8451Q.h" |
eleim | 1:00312af3c20e | 4 | #include <list> |
eleim | 0:84ecd53a6958 | 5 | #include <math.h> |
eleim | 1:00312af3c20e | 6 | #include <queue> |
eleim | 0:84ecd53a6958 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
eleim | 0:84ecd53a6958 | 8 | #define TRANSFER_SIZE 24 |
eleim | 0:84ecd53a6958 | 9 | |
eleim | 0:84ecd53a6958 | 10 | Serial pc(USBTX, USBRX); // tx, rx |
eleim | 0:84ecd53a6958 | 11 | |
eleim | 0:84ecd53a6958 | 12 | PinName const SDA = PTE25; |
eleim | 0:84ecd53a6958 | 13 | PinName const SCL = PTE24; |
eleim | 0:84ecd53a6958 | 14 | |
eleim | 0:84ecd53a6958 | 15 | AnalogIn light_ain(A0); |
eleim | 0:84ecd53a6958 | 16 | AnalogIn temp_ain(A1); |
eleim | 1:00312af3c20e | 17 | AnalogIn pir_ain(A2); |
eleim | 0:84ecd53a6958 | 18 | |
eleim | 0:84ecd53a6958 | 19 | nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTE1, PTE0, PTD0); // mosi, miso, sck, csn, ce, irq |
eleim | 0:84ecd53a6958 | 20 | |
eleim | 0:84ecd53a6958 | 21 | DigitalOut myled1(LED1); |
eleim | 0:84ecd53a6958 | 22 | DigitalOut myled2(LED2); |
eleim | 0:84ecd53a6958 | 23 | |
eleim | 1:00312af3c20e | 24 | float light; |
eleim | 1:00312af3c20e | 25 | float temp; |
eleim | 1:00312af3c20e | 26 | float pir; |
eleim | 0:84ecd53a6958 | 27 | |
eleim | 1:00312af3c20e | 28 | std::list<float> light_data; |
eleim | 1:00312af3c20e | 29 | std::list<float> temp_data; |
eleim | 1:00312af3c20e | 30 | std::list<float> pir_data; |
eleim | 0:84ecd53a6958 | 31 | |
eleim | 1:00312af3c20e | 32 | |
eleim | 1:00312af3c20e | 33 | void getdata() { |
eleim | 0:84ecd53a6958 | 34 | light = light_ain.read(); |
eleim | 0:84ecd53a6958 | 35 | temp = temp_ain.read(); |
eleim | 0:84ecd53a6958 | 36 | pir = pir_ain.read(); |
eleim | 1:00312af3c20e | 37 | |
eleim | 1:00312af3c20e | 38 | light_data.push_back(light); |
eleim | 1:00312af3c20e | 39 | temp_data.push_back(temp); |
eleim | 1:00312af3c20e | 40 | pir_data.push_back(pir); |
eleim | 0:84ecd53a6958 | 41 | |
eleim | 1:00312af3c20e | 42 | |
eleim | 1:00312af3c20e | 43 | light_data.pop_front(); |
eleim | 1:00312af3c20e | 44 | temp_data.pop_front(); |
eleim | 1:00312af3c20e | 45 | pir_data.pop_front(); |
eleim | 1:00312af3c20e | 46 | printf("Light reading: %f\n\r", light); |
eleim | 2:3779f28a6bf0 | 47 | printf("Light list element : %f\n\r", light_data.back()); |
eleim | 2:3779f28a6bf0 | 48 | |
eleim | 2:3779f28a6bf0 | 49 | printf("Temperature reading: %f\n\r", temp); |
eleim | 2:3779f28a6bf0 | 50 | printf("Temperature list element : %f\n\r", temp_data.back()); |
eleim | 2:3779f28a6bf0 | 51 | |
eleim | 2:3779f28a6bf0 | 52 | printf("Temperature reading: %f\n\r", light); |
eleim | 2:3779f28a6bf0 | 53 | printf("Pir reading: %f\n\r", pir_data.back()); |
eleim | 2:3779f28a6bf0 | 54 | |
eleim | 0:84ecd53a6958 | 55 | } |
eleim | 0:84ecd53a6958 | 56 | |
eleim | 0:84ecd53a6958 | 57 | |
eleim | 0:84ecd53a6958 | 58 | int main() |
eleim | 0:84ecd53a6958 | 59 | { |
eleim | 1:00312af3c20e | 60 | |
eleim | 1:00312af3c20e | 61 | light_data.assign(20,0); |
eleim | 1:00312af3c20e | 62 | temp_data.assign(20,0); |
eleim | 1:00312af3c20e | 63 | pir_data.assign(20,0); |
eleim | 0:84ecd53a6958 | 64 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
eleim | 0:84ecd53a6958 | 65 | |
eleim | 0:84ecd53a6958 | 66 | |
eleim | 0:84ecd53a6958 | 67 | |
eleim | 0:84ecd53a6958 | 68 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
eleim | 0:84ecd53a6958 | 69 | int txDataCnt = 0; |
eleim | 0:84ecd53a6958 | 70 | int rxDataCnt = 0; |
eleim | 0:84ecd53a6958 | 71 | |
eleim | 0:84ecd53a6958 | 72 | my_nrf24l01p.powerUp(); |
eleim | 0:84ecd53a6958 | 73 | |
eleim | 0:84ecd53a6958 | 74 | // Display the (default) setup of the nRF24L01+ chip |
eleim | 0:84ecd53a6958 | 75 | //pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
eleim | 0:84ecd53a6958 | 76 | //pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
eleim | 0:84ecd53a6958 | 77 | //pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
eleim | 0:84ecd53a6958 | 78 | //pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
eleim | 0:84ecd53a6958 | 79 | //pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
eleim | 0:84ecd53a6958 | 80 | |
eleim | 0:84ecd53a6958 | 81 | //pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
eleim | 0:84ecd53a6958 | 82 | |
eleim | 0:84ecd53a6958 | 83 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
eleim | 0:84ecd53a6958 | 84 | |
eleim | 0:84ecd53a6958 | 85 | my_nrf24l01p.setReceiveMode(); |
eleim | 0:84ecd53a6958 | 86 | my_nrf24l01p.enable(); |
eleim | 0:84ecd53a6958 | 87 | |
eleim | 0:84ecd53a6958 | 88 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
eleim | 0:84ecd53a6958 | 89 | |
eleim | 0:84ecd53a6958 | 90 | while (1) { |
eleim | 0:84ecd53a6958 | 91 | getdata(); |
eleim | 0:84ecd53a6958 | 92 | |
eleim | 0:84ecd53a6958 | 93 | // txDataCnt = sprintf(txData, " %1.3f %1.3f %1.3f\n", x,y,z); |
eleim | 0:84ecd53a6958 | 94 | //my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
eleim | 0:84ecd53a6958 | 95 | myled1 = !myled1; |
eleim | 0:84ecd53a6958 | 96 | wait(0.5); |
eleim | 0:84ecd53a6958 | 97 | } |
eleim | 0:84ecd53a6958 | 98 | |
eleim | 0:84ecd53a6958 | 99 | } |
eleim | 0:84ecd53a6958 | 100 | |
eleim | 0:84ecd53a6958 | 101 |