point-2-point demo

Dependencies:   sx12xx_hal

radio chip selection

Radio chip driver is not included, because these options are available.
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.

TX trigger selection

Edit main.h to define DIGITAL_TRIGGER or ANALOG_TRIGGER to chose whether transmit is initiated by digital pin (button/jumper) or analog pin(s) level change.

This project is intended to be used on two LoRa shields.

Each board sits in continuous RX mode, waiting for request packet.
If the received packet has good CRC, the packet is acknowledged along with read of ADC sample from the replying device.
The original request packet also contains instruction to set level of output pin.

Both sides of the link are running the same code, and each can initiate a transmission at any time.
No addressing is used, so only two nodes can operate on the radio channel.

Revision:
3:cd312fc32558
Parent:
2:5131512b4eb9
Child:
4:b5dd459ac390
--- a/main.cpp	Fri Nov 10 17:42:50 2017 +0000
+++ b/main.cpp	Wed May 30 10:20:36 2018 -0700
@@ -1,6 +1,8 @@
 #include "sx127x_lora.h"
 #include "uart_cmds.h"
 
+//#define ANALOG_TX_TRIGGER
+
 DigitalOut myled(LED1);
 
 SPI spi(D11, D12, D13); // mosi, miso, sclk
@@ -14,10 +16,14 @@
 Timer t;
 Ticker ticker;
 AnalogIn a1(A1);
+#define AIN_REST_THRESHOLD      96  // 12bit left justified
 DigitalOut pc6_out(PC_6);
 
+#ifndef ANALOG_TX_TRIGGER
 DigitalOut jumper_out(PC_10);
 InterruptIn jumper_in(PC_12);
+volatile bool start_tx;
+#endif /* !ANALOG_TX_TRIGGER */
 
 void rfsw_callback()
 {
@@ -141,8 +147,7 @@
     } // ..for()
 }
 
-volatile bool start_tx;
-
+#ifndef ANALOG_TX_TRIGGER
 void button_isr()
 {
     if (!jumper_in.read())
@@ -156,11 +161,16 @@
     else
         ticker.detach();
 }
+#endif /* !ANALOG_TX_TRIGGER */
 
 int main()
 {
+#ifdef ANALOG_TX_TRIGGER
+    uint16_t prev_ain;
+    int8_t ain_movement;
+    bool ain_sent;
+#else
     bool jin = false;
-    printf("\r\n2reset\r\n");
 
     jumper_out = 1;
     jumper_in.mode(PullDown);
@@ -170,6 +180,8 @@
         wait(0.01);
     }
     user_button.fall(&button_isr);
+#endif /* !ANALOG_TX_TRIGGER */
+    printf("\r\n2reset\r\n");
     t.start();
 
     radio.rf_switch = rfsw_callback;
@@ -200,7 +212,47 @@
                 
     lora.start_rx(RF_OPMODE_RECEIVER);
 
+#ifdef ANALOG_TX_TRIGGER
+    prev_ain = a1.read_u16();
+    ain_movement = 0;
+    ain_sent = false;
+#endif /* ANALOG_TX_TRIGGER */
     for (;;) {
+#ifdef ANALOG_TX_TRIGGER
+        uint16_t ain = a1.read_u16();
+        uint16_t diff = abs(ain-prev_ain);
+        if (diff > AIN_REST_THRESHOLD) {
+            ain_sent = false;
+            if (ain_movement < 1)
+                ain_movement = 1;
+            else {
+                if (++ain_movement > 16)
+                    ain_movement = 16;
+            }
+        } else {
+            /* steady state */
+            if (ain_movement > 0)
+                ain_movement = 0;
+            else {
+                if (--ain_movement < -16) {
+                    ain_movement = -16;
+                    if (!ain_sent) {
+                        uint8_t buf[4];
+                        printf("## %02x ##\r\n", ain >> 8);
+                        buf[0] = CMD_PWM;
+                        buf[1] = 120;   // Hz
+                        buf[2] = ain >> 8;  // duty
+                        radio_tx(buf, 3);
+                        ain_sent = true;
+                    }
+                }
+            }
+        }
+        //printf("%05u  diff:%04u  move:%d\r\n", ain, diff, ain_movement);
+        prev_ain = ain;
+        wait_us(5000);
+
+#else
 
         if (jumper_in.read()) {
             if (!jin) {
@@ -221,6 +273,7 @@
             printf("start_tx...\r\n");
             radio_tx(buf, 2);
         }
+#endif /* !ANALOG_TX_TRIGGER */
         if (lora.service() == SERVICE_READ_FIFO) {
             uint16_t crc, rx_crc;
             int i;