Alarm generator, transmitter side

Dependencies:   sx12xx_hal

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This is transmitter project; Receiver side is Alarm Slave project.
This project generates alarm (transmits) from grounding certain pins.
To find the actual pins used, refer to the code where pin[A-D] is declared as DigitalIn.

Alarm message is sent N times with future timestamp indicating when alarm is to occur. This accommodates dropped packets, resulting in alarm occurring when as few as 1 packet is received.

Revision:
1:3199506bc2e5
Parent:
0:cb38da4f4b04
Child:
2:0b7620bda2c9
--- a/main.cpp	Tue Aug 22 10:06:40 2017 -0700
+++ b/main.cpp	Thu Aug 31 23:15:04 2017 +0000
@@ -17,9 +17,15 @@
 }
 /**********************************************************************/
 
-DigitalIn user_button(USER_BUTTON);
+DigitalIn pc3(PC_3);
+DigitalIn pc2(PC_2);
+DigitalIn pc6(PC_6);
+DigitalIn pc8(PC_8);
 Timer t;
-#define CMD_ALARM       0x01
+#define CMD_PC2       0x02
+#define CMD_PC3       0x03
+#define CMD_PC6       0x06
+#define CMD_PC8       0x08
 
 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length )
 {
@@ -45,12 +51,12 @@
     return crc;
 }
 
-void transmit(unsigned target)
+void transmit(unsigned target, uint8_t cmd)
 {
     unsigned t_diff;
     uint16_t crc;
 
-    radio.tx_buf[0] = CMD_ALARM;
+    radio.tx_buf[0] = cmd;
     t_diff = target - t.read_us();
     radio.tx_buf[1] = t_diff >> 24;
     radio.tx_buf[2] = t_diff >> 16;
@@ -63,29 +69,54 @@
 
     lora.start_tx(lora.RegPayloadLength);   /* begin transmission */
 
-    while (lora.service() != SERVICE_TX_DONE)   /* wait for transmission to complete */
-        ;
+    while (lora.service() != SERVICE_TX_DONE) {  /* wait for transmission to complete */
+    }
 
     printf("t_diff:%u crc:%04x\r\n", t_diff, crc);
 }
 
 #define TARGET_LATENCY      2000000
-void send_alarm()
+void send_alarm(uint8_t cmd)
 {
     int i;
     unsigned target = t.read_us() + TARGET_LATENCY;
     printf("send_alarm() %u\n", target);
 
-    for (i = 0; i < 3; i++) {
-        transmit(target);
+    for (i = 0; i < 5; i++) {
+        transmit(target, cmd);
         wait(0.1);
     }
 }
+
+void debounce(DigitalIn* pin, uint8_t cmd)
+{
+    if (!pin->read()) {
+        int i;
+        for (i = 0; i < 5; i++) {
+            wait(0.01);
+            if (pin->read()) {
+                printf("trans\r\n");
+                break;
+            }
+        }
+        if (i == 5)
+            send_alarm(cmd);
+
+        while (!pin->read())
+            ;
+    }
+}
  
 int main()
 {
     printf("\r\nreset-tx\r\n");
     t.start();
+
+    pc3.mode(PullUp);
+    pc2.mode(PullUp);
+    pc6.mode(PullUp);
+    pc8.mode(PullUp);
+
     radio.rf_switch = rfsw_callback;
     
     radio.set_frf_MHz(910.8);
@@ -117,21 +148,9 @@
     radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
 
     for (;;) {       
-        if (!user_button.read()) {
-            int i;
-            for (i = 0; i < 5; i++) {
-                wait(0.01);
-                if (user_button.read()) {
-                    printf("trans\r\n");
-                    break;
-                }
-            }
-            if (i == 5)
-                send_alarm();
-
-            while (!user_button.read())
-                ;
-        }
-
+        debounce(&pc3, CMD_PC3);
+        debounce(&pc2, CMD_PC2);
+        debounce(&pc6, CMD_PC6);
+        debounce(&pc8, CMD_PC8);
     } // ..for (;;)
 }