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.

trigger_analog.cpp

Committer:
Wayne Roberts
Date:
2018-08-01
Revision:
5:e35b1b281466

File content as of revision 5:e35b1b281466:

#include "main.h"
#ifdef ANALOG_TRIGGER

#define NUM_ANALOG_IN       4

#if defined(TARGET_FF_MORPHO) && defined(TARGET_FAMILY_STM32)
PinName pin_names[NUM_ANALOG_IN] = {
    PC_2, /* CN7-35 */
    PC_3, /* CN7-37 */
    PC_4, /* CN10-34 */
    PC_5  /* CN10-6 */
};
#endif

const uint8_t rfCmds[NUM_ANALOG_IN] = {
    CMD_PWM_A,
    CMD_PWM_B,
    CMD_PWM_C,
    CMD_PWM_D
};

typedef struct {
    AnalogIn* ain;
    uint16_t prev;
    int8_t movement;
    bool sent;
} analog_t;

analog_t _a_[NUM_ANALOG_IN];

void trigger_init()
{
    unsigned n;
    for (n = 0; n < NUM_ANALOG_IN; n++) {
        _a_[n].ain = new AnalogIn(pin_names[n]);
        _a_[n].prev = _a_[n].ain->read_u16();
        _a_[n].movement = 0;
        _a_[n].sent = false;
    }
}

#define AIN_REST_THRESHOLD      96  // 12bit left justified

void analog_mainloop(analog_t* ana, uint8_t rfCmd)
{
    uint16_t ain = ana->ain->read_u16();
    uint16_t diff = abs(ain-ana->prev);
    if (diff > AIN_REST_THRESHOLD) {
        ana->sent = false;
        if (ana->movement < 1)
            ana->movement = 1;
        else {
            if (++ana->movement > 16)
                ana->movement = 16;
        }
    } else {
        /* steady state */
        if (ana->movement > 0)
            ana->movement = 0;
        else {
            if (--ana->movement < -16) {
                ana->movement = -16;
                if (!ana->sent) {
                    uint8_t buf[4];
                    printf("## %02x ##\r\n", ain >> 8);
                    buf[0] = rfCmd;
                    buf[1] = 120;   // Hz
                    buf[2] = ain >> 8;  // duty
                    radio_tx(buf, 3);
                    ana->sent = true;
                }
            }
        }
    }
    //printf("%05u  diff:%04u  move:%d\r\n", ain, diff, ain_movement);
    ana->prev = ain;
}

void trigger_mainloop()
{
    unsigned n;
    for (n = 0; n < NUM_ANALOG_IN; n++) {
        analog_mainloop(&_a_[n], rfCmds[n]);
    }
    wait_us(5000);
}

#endif /* ANALOG_TRIGGER */