wayne roberts
/
alarm_analog_master
broadcasts repeated notification upon change of analog input
main.cpp@1:b5b1352d36d1, 2018-10-23 (annotated)
- Committer:
- wroberts@semtech.com
- Date:
- Tue Oct 23 06:56:13 2018 -0700
- Revision:
- 1:b5b1352d36d1
- Parent:
- 0:a25661fcf9b2
remove radio driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wayne Roberts |
0:a25661fcf9b2 | 1 | #include "radio.h" |
Wayne Roberts |
0:a25661fcf9b2 | 2 | |
Wayne Roberts |
0:a25661fcf9b2 | 3 | #define NUM_ANALOG_IN 4 |
Wayne Roberts |
0:a25661fcf9b2 | 4 | |
Wayne Roberts |
0:a25661fcf9b2 | 5 | #if defined(SX127x_H) || defined(SX126x_H) |
Wayne Roberts |
0:a25661fcf9b2 | 6 | #define BW_KHZ 500 |
Wayne Roberts |
0:a25661fcf9b2 | 7 | #define SPREADING_FACTOR 11 |
Wayne Roberts |
0:a25661fcf9b2 | 8 | #define CF_HZ 910800000 |
Wayne Roberts |
0:a25661fcf9b2 | 9 | #if defined(SX126x_H) |
Wayne Roberts |
0:a25661fcf9b2 | 10 | #define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14) |
Wayne Roberts |
0:a25661fcf9b2 | 11 | #else |
Wayne Roberts |
0:a25661fcf9b2 | 12 | #define TX_DBM 17 |
Wayne Roberts |
0:a25661fcf9b2 | 13 | #endif |
Wayne Roberts |
0:a25661fcf9b2 | 14 | #elif defined(SX128x_H) |
Wayne Roberts |
0:a25661fcf9b2 | 15 | #define BW_KHZ 200 |
Wayne Roberts |
0:a25661fcf9b2 | 16 | #define SPREADING_FACTOR 11 |
Wayne Roberts |
0:a25661fcf9b2 | 17 | #define CF_HZ 2487000000 |
Wayne Roberts |
0:a25661fcf9b2 | 18 | |
Wayne Roberts |
0:a25661fcf9b2 | 19 | #define TX_DBM 5 |
Wayne Roberts |
0:a25661fcf9b2 | 20 | #endif |
Wayne Roberts |
0:a25661fcf9b2 | 21 | |
Wayne Roberts |
0:a25661fcf9b2 | 22 | #define PWM_HZ 120 |
Wayne Roberts |
0:a25661fcf9b2 | 23 | |
Wayne Roberts |
0:a25661fcf9b2 | 24 | #if defined(TARGET_FF_MORPHO) && defined(TARGET_FAMILY_STM32) |
Wayne Roberts |
0:a25661fcf9b2 | 25 | PinName pin_names[NUM_ANALOG_IN] = { |
Wayne Roberts |
0:a25661fcf9b2 | 26 | PC_2, /* CN7-35 */ |
Wayne Roberts |
0:a25661fcf9b2 | 27 | PC_3, /* CN7-37 */ |
Wayne Roberts |
0:a25661fcf9b2 | 28 | PC_4, /* CN10-34 */ |
Wayne Roberts |
0:a25661fcf9b2 | 29 | PC_5 /* CN10-6 */ |
Wayne Roberts |
0:a25661fcf9b2 | 30 | }; |
Wayne Roberts |
0:a25661fcf9b2 | 31 | #endif |
Wayne Roberts |
0:a25661fcf9b2 | 32 | |
Wayne Roberts |
0:a25661fcf9b2 | 33 | #define CMD_PWM_A 0x02 |
Wayne Roberts |
0:a25661fcf9b2 | 34 | #define CMD_PWM_B 0x03 |
Wayne Roberts |
0:a25661fcf9b2 | 35 | #define CMD_PWM_C 0x04 |
Wayne Roberts |
0:a25661fcf9b2 | 36 | #define CMD_PWM_D 0x05 |
Wayne Roberts |
0:a25661fcf9b2 | 37 | |
Wayne Roberts |
0:a25661fcf9b2 | 38 | const uint8_t rfCmds[NUM_ANALOG_IN] = { |
Wayne Roberts |
0:a25661fcf9b2 | 39 | CMD_PWM_A, |
Wayne Roberts |
0:a25661fcf9b2 | 40 | CMD_PWM_B, |
Wayne Roberts |
0:a25661fcf9b2 | 41 | CMD_PWM_C, |
Wayne Roberts |
0:a25661fcf9b2 | 42 | CMD_PWM_D |
Wayne Roberts |
0:a25661fcf9b2 | 43 | }; |
Wayne Roberts |
0:a25661fcf9b2 | 44 | |
Wayne Roberts |
0:a25661fcf9b2 | 45 | typedef struct { |
Wayne Roberts |
0:a25661fcf9b2 | 46 | AnalogIn* ain; |
Wayne Roberts |
0:a25661fcf9b2 | 47 | uint16_t prev; |
Wayne Roberts |
0:a25661fcf9b2 | 48 | int8_t movement; |
Wayne Roberts |
0:a25661fcf9b2 | 49 | bool sent; |
Wayne Roberts |
0:a25661fcf9b2 | 50 | } analog_t; |
Wayne Roberts |
0:a25661fcf9b2 | 51 | |
Wayne Roberts |
0:a25661fcf9b2 | 52 | analog_t _a_[NUM_ANALOG_IN]; |
Wayne Roberts |
0:a25661fcf9b2 | 53 | |
Wayne Roberts |
0:a25661fcf9b2 | 54 | Timer t; |
Wayne Roberts |
0:a25661fcf9b2 | 55 | |
Wayne Roberts |
0:a25661fcf9b2 | 56 | volatile bool tx_done; |
Wayne Roberts |
0:a25661fcf9b2 | 57 | |
Wayne Roberts |
0:a25661fcf9b2 | 58 | static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length ) |
Wayne Roberts |
0:a25661fcf9b2 | 59 | { |
Wayne Roberts |
0:a25661fcf9b2 | 60 | // The CRC calculation follows CCITT |
Wayne Roberts |
0:a25661fcf9b2 | 61 | const uint16_t polynom = 0x1021; |
Wayne Roberts |
0:a25661fcf9b2 | 62 | // CRC initial value |
Wayne Roberts |
0:a25661fcf9b2 | 63 | uint16_t crc = 0x0000; |
Wayne Roberts |
0:a25661fcf9b2 | 64 | |
Wayne Roberts |
0:a25661fcf9b2 | 65 | if( buffer == NULL ) |
Wayne Roberts |
0:a25661fcf9b2 | 66 | { |
Wayne Roberts |
0:a25661fcf9b2 | 67 | return 0; |
Wayne Roberts |
0:a25661fcf9b2 | 68 | } |
Wayne Roberts |
0:a25661fcf9b2 | 69 | |
Wayne Roberts |
0:a25661fcf9b2 | 70 | for( uint16_t i = 0; i < length; ++i ) |
Wayne Roberts |
0:a25661fcf9b2 | 71 | { |
Wayne Roberts |
0:a25661fcf9b2 | 72 | crc ^= ( uint16_t ) buffer[i] << 8; |
Wayne Roberts |
0:a25661fcf9b2 | 73 | for( uint16_t j = 0; j < 8; ++j ) |
Wayne Roberts |
0:a25661fcf9b2 | 74 | { |
Wayne Roberts |
0:a25661fcf9b2 | 75 | crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 ); |
Wayne Roberts |
0:a25661fcf9b2 | 76 | } |
Wayne Roberts |
0:a25661fcf9b2 | 77 | } |
Wayne Roberts |
0:a25661fcf9b2 | 78 | |
Wayne Roberts |
0:a25661fcf9b2 | 79 | return crc; |
Wayne Roberts |
0:a25661fcf9b2 | 80 | } |
Wayne Roberts |
0:a25661fcf9b2 | 81 | |
Wayne Roberts |
0:a25661fcf9b2 | 82 | void transmit(unsigned target, uint8_t cmd, uint16_t ain) |
Wayne Roberts |
0:a25661fcf9b2 | 83 | { |
Wayne Roberts |
0:a25661fcf9b2 | 84 | unsigned t_diff; |
Wayne Roberts |
0:a25661fcf9b2 | 85 | uint16_t crc; |
Wayne Roberts |
0:a25661fcf9b2 | 86 | |
Wayne Roberts |
0:a25661fcf9b2 | 87 | Radio::radio.tx_buf[0] = cmd; |
Wayne Roberts |
0:a25661fcf9b2 | 88 | Radio::radio.tx_buf[1] = PWM_HZ; |
Wayne Roberts |
0:a25661fcf9b2 | 89 | Radio::radio.tx_buf[2] = ain >> 8; // pwm duty |
Wayne Roberts |
0:a25661fcf9b2 | 90 | t_diff = target - t.read_us(); |
Wayne Roberts |
0:a25661fcf9b2 | 91 | Radio::radio.tx_buf[3] = t_diff >> 24; |
Wayne Roberts |
0:a25661fcf9b2 | 92 | Radio::radio.tx_buf[4] = t_diff >> 16; |
Wayne Roberts |
0:a25661fcf9b2 | 93 | Radio::radio.tx_buf[5] = t_diff >> 8; |
Wayne Roberts |
0:a25661fcf9b2 | 94 | Radio::radio.tx_buf[6] = t_diff & 0xff; |
Wayne Roberts |
0:a25661fcf9b2 | 95 | crc = crc_ccitt(Radio::radio.tx_buf, 7); |
Wayne Roberts |
0:a25661fcf9b2 | 96 | Radio::radio.tx_buf[7] = crc >> 8; |
Wayne Roberts |
0:a25661fcf9b2 | 97 | Radio::radio.tx_buf[8] = crc & 0xff; |
Wayne Roberts |
0:a25661fcf9b2 | 98 | |
Wayne Roberts |
0:a25661fcf9b2 | 99 | Radio::Send(9, 0, 0, 0); |
Wayne Roberts |
0:a25661fcf9b2 | 100 | |
Wayne Roberts |
0:a25661fcf9b2 | 101 | for (tx_done = false; !tx_done; ) |
Wayne Roberts |
0:a25661fcf9b2 | 102 | Radio::service(); |
Wayne Roberts |
0:a25661fcf9b2 | 103 | |
Wayne Roberts |
0:a25661fcf9b2 | 104 | printf("t_diff:%u crc:%04x\r\n", t_diff, crc); |
Wayne Roberts |
0:a25661fcf9b2 | 105 | } |
Wayne Roberts |
0:a25661fcf9b2 | 106 | |
Wayne Roberts |
0:a25661fcf9b2 | 107 | #define TARGET_LATENCY 2000000 |
Wayne Roberts |
0:a25661fcf9b2 | 108 | void send_alarm(uint8_t cmd, uint16_t ain) |
Wayne Roberts |
0:a25661fcf9b2 | 109 | { |
Wayne Roberts |
0:a25661fcf9b2 | 110 | int i; |
Wayne Roberts |
0:a25661fcf9b2 | 111 | unsigned target = t.read_us() + TARGET_LATENCY; |
Wayne Roberts |
0:a25661fcf9b2 | 112 | printf("send_alarm() %u\n", target); |
Wayne Roberts |
0:a25661fcf9b2 | 113 | |
Wayne Roberts |
0:a25661fcf9b2 | 114 | for (i = 0; i < 5; i++) { |
Wayne Roberts |
0:a25661fcf9b2 | 115 | transmit(target, cmd, ain); |
Wayne Roberts |
0:a25661fcf9b2 | 116 | wait(0.1); |
Wayne Roberts |
0:a25661fcf9b2 | 117 | } |
Wayne Roberts |
0:a25661fcf9b2 | 118 | } |
Wayne Roberts |
0:a25661fcf9b2 | 119 | |
Wayne Roberts |
0:a25661fcf9b2 | 120 | void txDoneCB() |
Wayne Roberts |
0:a25661fcf9b2 | 121 | { |
Wayne Roberts |
0:a25661fcf9b2 | 122 | tx_done = true; |
Wayne Roberts |
0:a25661fcf9b2 | 123 | } |
Wayne Roberts |
0:a25661fcf9b2 | 124 | |
Wayne Roberts |
0:a25661fcf9b2 | 125 | void rxDoneCB(uint8_t size, float Rssi, float Snr) |
Wayne Roberts |
0:a25661fcf9b2 | 126 | { |
Wayne Roberts |
0:a25661fcf9b2 | 127 | } |
Wayne Roberts |
0:a25661fcf9b2 | 128 | |
Wayne Roberts |
0:a25661fcf9b2 | 129 | #define AIN_REST_THRESHOLD 96 // 12bit left justified |
Wayne Roberts |
0:a25661fcf9b2 | 130 | |
Wayne Roberts |
0:a25661fcf9b2 | 131 | void analog_mainloop(analog_t* ana, uint8_t rfCmd) |
Wayne Roberts |
0:a25661fcf9b2 | 132 | { |
Wayne Roberts |
0:a25661fcf9b2 | 133 | uint16_t ain = ana->ain->read_u16(); |
Wayne Roberts |
0:a25661fcf9b2 | 134 | uint16_t diff = abs(ain-ana->prev); |
Wayne Roberts |
0:a25661fcf9b2 | 135 | if (diff > AIN_REST_THRESHOLD) { |
Wayne Roberts |
0:a25661fcf9b2 | 136 | ana->sent = false; |
Wayne Roberts |
0:a25661fcf9b2 | 137 | if (ana->movement < 1) |
Wayne Roberts |
0:a25661fcf9b2 | 138 | ana->movement = 1; |
Wayne Roberts |
0:a25661fcf9b2 | 139 | else { |
Wayne Roberts |
0:a25661fcf9b2 | 140 | if (++ana->movement > 16) |
Wayne Roberts |
0:a25661fcf9b2 | 141 | ana->movement = 16; |
Wayne Roberts |
0:a25661fcf9b2 | 142 | } |
Wayne Roberts |
0:a25661fcf9b2 | 143 | } else { |
Wayne Roberts |
0:a25661fcf9b2 | 144 | /* steady state */ |
Wayne Roberts |
0:a25661fcf9b2 | 145 | if (ana->movement > 0) |
Wayne Roberts |
0:a25661fcf9b2 | 146 | ana->movement = 0; |
Wayne Roberts |
0:a25661fcf9b2 | 147 | else { |
Wayne Roberts |
0:a25661fcf9b2 | 148 | if (--ana->movement < -16) { |
Wayne Roberts |
0:a25661fcf9b2 | 149 | ana->movement = -16; |
Wayne Roberts |
0:a25661fcf9b2 | 150 | if (!ana->sent) { |
Wayne Roberts |
0:a25661fcf9b2 | 151 | printf("## %02x ##\r\n", ain >> 8); |
Wayne Roberts |
0:a25661fcf9b2 | 152 | send_alarm(rfCmd, ain); |
Wayne Roberts |
0:a25661fcf9b2 | 153 | ana->sent = true; |
Wayne Roberts |
0:a25661fcf9b2 | 154 | } |
Wayne Roberts |
0:a25661fcf9b2 | 155 | } |
Wayne Roberts |
0:a25661fcf9b2 | 156 | } |
Wayne Roberts |
0:a25661fcf9b2 | 157 | } |
Wayne Roberts |
0:a25661fcf9b2 | 158 | //printf("%05u diff:%04u move:%d\r\n", ain, diff, ain_movement); |
Wayne Roberts |
0:a25661fcf9b2 | 159 | ana->prev = ain; |
Wayne Roberts |
0:a25661fcf9b2 | 160 | } |
Wayne Roberts |
0:a25661fcf9b2 | 161 | |
Wayne Roberts |
0:a25661fcf9b2 | 162 | void trigger_init() |
Wayne Roberts |
0:a25661fcf9b2 | 163 | { |
Wayne Roberts |
0:a25661fcf9b2 | 164 | unsigned n; |
Wayne Roberts |
0:a25661fcf9b2 | 165 | for (n = 0; n < NUM_ANALOG_IN; n++) { |
Wayne Roberts |
0:a25661fcf9b2 | 166 | _a_[n].ain = new AnalogIn(pin_names[n]); |
Wayne Roberts |
0:a25661fcf9b2 | 167 | _a_[n].prev = _a_[n].ain->read_u16(); |
Wayne Roberts |
0:a25661fcf9b2 | 168 | _a_[n].movement = 0; |
Wayne Roberts |
0:a25661fcf9b2 | 169 | _a_[n].sent = false; |
Wayne Roberts |
0:a25661fcf9b2 | 170 | } |
Wayne Roberts |
0:a25661fcf9b2 | 171 | } |
Wayne Roberts |
0:a25661fcf9b2 | 172 | |
Wayne Roberts |
0:a25661fcf9b2 | 173 | const RadioEvents_t rev = { |
Wayne Roberts |
0:a25661fcf9b2 | 174 | /* Dio0_top_half */ NULL, |
Wayne Roberts |
0:a25661fcf9b2 | 175 | /* TxDone_topHalf */ NULL, |
Wayne Roberts |
0:a25661fcf9b2 | 176 | /* TxDone_botHalf */ txDoneCB, |
Wayne Roberts |
0:a25661fcf9b2 | 177 | /* TxTimeout */ NULL, |
Wayne Roberts |
0:a25661fcf9b2 | 178 | /* RxDone */ rxDoneCB, |
Wayne Roberts |
0:a25661fcf9b2 | 179 | /* RxTimeout */ NULL, |
Wayne Roberts |
0:a25661fcf9b2 | 180 | /* RxError */ NULL, |
Wayne Roberts |
0:a25661fcf9b2 | 181 | /* FhssChangeChannel */NULL, |
Wayne Roberts |
0:a25661fcf9b2 | 182 | /* CadDone */ NULL |
Wayne Roberts |
0:a25661fcf9b2 | 183 | }; |
Wayne Roberts |
0:a25661fcf9b2 | 184 | |
Wayne Roberts |
0:a25661fcf9b2 | 185 | int main() |
Wayne Roberts |
0:a25661fcf9b2 | 186 | { |
Wayne Roberts |
0:a25661fcf9b2 | 187 | printf("\r\nreset-tx\r\n"); |
Wayne Roberts |
0:a25661fcf9b2 | 188 | |
Wayne Roberts |
0:a25661fcf9b2 | 189 | trigger_init(); |
Wayne Roberts |
0:a25661fcf9b2 | 190 | |
Wayne Roberts |
0:a25661fcf9b2 | 191 | t.start(); |
Wayne Roberts |
0:a25661fcf9b2 | 192 | |
Wayne Roberts |
0:a25661fcf9b2 | 193 | Radio::Init(&rev); |
Wayne Roberts |
0:a25661fcf9b2 | 194 | |
Wayne Roberts |
0:a25661fcf9b2 | 195 | Radio::Standby(); |
Wayne Roberts |
0:a25661fcf9b2 | 196 | Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); |
Wayne Roberts |
0:a25661fcf9b2 | 197 | Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ |
Wayne Roberts |
0:a25661fcf9b2 | 198 | Radio::SetChannel(CF_HZ); |
Wayne Roberts |
0:a25661fcf9b2 | 199 | |
Wayne Roberts |
0:a25661fcf9b2 | 200 | Radio::set_tx_dbm(TX_DBM); |
Wayne Roberts |
0:a25661fcf9b2 | 201 | |
Wayne Roberts |
0:a25661fcf9b2 | 202 | for (;;) { |
Wayne Roberts |
0:a25661fcf9b2 | 203 | unsigned n; |
Wayne Roberts |
0:a25661fcf9b2 | 204 | for (n = 0; n < NUM_ANALOG_IN; n++) { |
Wayne Roberts |
0:a25661fcf9b2 | 205 | analog_mainloop(&_a_[n], rfCmds[n]); |
Wayne Roberts |
0:a25661fcf9b2 | 206 | } |
Wayne Roberts |
0:a25661fcf9b2 | 207 | wait_us(5000); |
Wayne Roberts |
0:a25661fcf9b2 | 208 | } // ..for (;;) |
Wayne Roberts |
0:a25661fcf9b2 | 209 | } |