Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 7:e51d0fbb1a25, committed 2017-01-05
- Comitter:
- pannaannap
- Date:
- Thu Jan 05 13:13:28 2017 +0000
- Parent:
- 6:98401b545e0c
- Child:
- 8:1861d0eef60a
- Commit message:
- one main file
Changed in this revision
--- a/common.h Thu Jan 05 11:25:21 2017 +0000
+++ b/common.h Thu Jan 05 13:13:28 2017 +0000
@@ -24,6 +24,14 @@
SOUND = 4,
};
+struct SensorDescription {
+ SensorDescription(int _sensor_id, unsigned long long _rx_address, unsigned long long _tx_address)
+ : sensor_id(_sensor_id), rx_address(_rx_address), tx_address(_tx_address) {}
+ int sensor_id;
+ unsigned long long rx_address;
+ unsigned long long tx_address;
+};
+
struct Data {
Data(uint8_t type_, uint8_t counter_)
: type(type_), counter(counter_) {}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Jan 05 13:13:28 2017 +0000
@@ -0,0 +1,23 @@
+#include "master.h"
+#include "sensor_main.h"
+#include "common.h"
+
+#define MODE MASTER
+#define MASTER 0
+#define SLAVE1 1
+
+int main() {
+ switch(MODE) {
+ case MASTER:
+ master_loop();
+ break;
+ case SLAVE1:
+ // TODO vector of description for multiple sensors
+ // TODO sensor class
+ SensorDescription description(PIR1, PIR1_ADDRESS, MASTER_ADDRESS);
+ sensor_loop(description);
+ break;
+ }
+
+ return 0;
+}
\ No newline at end of file
--- a/master.cpp Thu Jan 05 11:25:21 2017 +0000
+++ b/master.cpp Thu Jan 05 13:13:28 2017 +0000
@@ -1,12 +1,12 @@
#include "common.h"
-
-const unsigned long long RX_ADDRESS = MASTER_ADDRESS;
-const unsigned long long TX_ADDRESS = PIR1_ADDRESS;
+#include "master.h"
-Serial pc(USBTX, USBRX); // tx, rx
-nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2); // mosi, miso, sck, csn, ce, irq
-
-int main() {
+int master_loop() {
+ Serial pc(USBTX, USBRX); // tx, rx
+ nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2); // mosi, miso, sck, csn, ce, irq
+
+ const unsigned long long RX_ADDRESS = MASTER_ADDRESS;
+ const unsigned long long TX_ADDRESS = PIR1_ADDRESS;
char rxData[TRANSFER_SIZE];
@@ -23,7 +23,7 @@
pc.printf( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
while (1) {
- int rx_bytes= 0;
+ int rx_bytes = 0;
if(radio.readable(NRF24L01P_PIPE_P1)){
rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, sizeof(rxData));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/master.h Thu Jan 05 13:13:28 2017 +0000 @@ -0,0 +1,1 @@ +int master_loop(); \ No newline at end of file
--- a/pir1_sensor.cpp Thu Jan 05 11:25:21 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,78 +0,0 @@
-#include "common.h"
-
-// ***** SETTINGS START *****
-
-const unsigned long long RX_ADDRESS = PIR1_ADDRESS;
-const unsigned long long TX_ADDRESS = MASTER_ADDRESS;
-const double SEND_INTERVAL = 5.0;
-const uint8_t SENSOR_ID = PIR1;
-
-// ***** SETTINGS END *****
-
-Serial pc(USBTX, USBRX); // tx, rx
-nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2); // mosi, miso, sck, csn, ce, irq
-DigitalIn sensor(PA_10);
-
-Ticker sender;
-bool send_acc_results = false;
-
-void set_send() {
- send_acc_results = true;
-}
-
-void init() {
- sensor.mode(PullDown);
- pc.baud(115200);
- sender.attach(&set_send, SEND_INTERVAL);
- radio_init(&radio, RX_ADDRESS, TX_ADDRESS);
-}
-
-int main() {
- init();
-
- // Display the (default) setup of the nRF24L01+ chip
- pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() );
- pc.printf( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() );
- pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() );
- pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() );
- pc.printf( "nRF24L01+ RX0 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
- pc.printf( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
-
- uint8_t count_changes;
- int last_state;
-
- while (1) {
- int state;
- if (sensor.read()) {
- state = 1;
- } else {
- state = 0;
- }
-
- if (last_state != state) {
- last_state = state;
- count_changes++;
- }
-
- if (send_acc_results) {
- Data data(SENSOR_ID, count_changes);
- std::string serialized_data = data.serialize();
- pc.printf("string data '%s', len %d\r\n", serialized_data.c_str(), serialized_data.size());
-
- char message[TRANSFER_SIZE];
- memcpy(message, serialized_data.c_str(), 2);
- int tx_bytes = radio.write(NRF24L01P_PIPE_P0, message, TRANSFER_SIZE);
-
- if (tx_bytes > 0) {
- pc.printf("Counter: %d\r\n", count_changes);
- pc.printf("RETR: %d\r\n", radio.getRetrCount());
- count_changes = 0;
- send_acc_results = false;
- }
- if(tx_bytes < 0)
- pc.printf("TX ERROR\r\n");
- }
-
- }
-}
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sensor_main.cpp Thu Jan 05 13:13:28 2017 +0000
@@ -0,0 +1,75 @@
+#include "sensor_main.h"
+
+const double SEND_INTERVAL = 5.0;
+
+Serial pc(USBTX, USBRX); // tx, rx
+nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2); // mosi, miso, sck, csn, ce, irq
+DigitalIn sensor(PA_10);
+
+Ticker sender;
+bool send_acc_results = false;
+
+void set_send() {
+ send_acc_results = true;
+}
+
+void init(unsigned long long rx_address, unsigned long long tx_address) {
+ sensor.mode(PullDown);
+ pc.baud(115200);
+ sender.attach(&set_send, SEND_INTERVAL);
+ radio_init(&radio, rx_address, tx_address);
+}
+
+int sensor_loop(SensorDescription description) {
+ const unsigned long long RX_ADDRESS = description.rx_address;
+ const unsigned long long TX_ADDRESS = description.tx_address;
+ const uint8_t SENSOR_ID = description.sensor_id;
+
+ init(RX_ADDRESS, TX_ADDRESS);
+
+ // Display the (default) setup of the nRF24L01+ chip
+ pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() );
+ pc.printf( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() );
+ pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() );
+ pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() );
+ pc.printf( "nRF24L01+ RX0 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
+ pc.printf( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
+
+ uint8_t count_changes;
+ int last_state;
+
+ while (1) {
+ int state;
+ if (sensor.read()) {
+ state = 1;
+ } else {
+ state = 0;
+ }
+
+ if (last_state != state) {
+ last_state = state;
+ count_changes++;
+ }
+
+ if (send_acc_results) {
+ Data data(SENSOR_ID, count_changes);
+ std::string serialized_data = data.serialize();
+ pc.printf("string data '%s', len %d\r\n", serialized_data.c_str(), serialized_data.size());
+
+ char message[TRANSFER_SIZE];
+ memcpy(message, serialized_data.c_str(), 2);
+ int tx_bytes = radio.write(NRF24L01P_PIPE_P0, message, TRANSFER_SIZE);
+
+ if (tx_bytes > 0) {
+ pc.printf("Counter: %d\r\n", count_changes);
+ pc.printf("RETR: %d\r\n", radio.getRetrCount());
+ count_changes = 0;
+ send_acc_results = false;
+ }
+ if(tx_bytes < 0)
+ pc.printf("TX ERROR\r\n");
+ }
+
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensor_main.h Thu Jan 05 13:13:28 2017 +0000 @@ -0,0 +1,3 @@ +#include "common.h" + +int sensor_loop(SensorDescription description); \ No newline at end of file