receives notification of analog change; updates PWM out.
main.cpp
- Committer:
- wroberts@semtech.com
- Date:
- 2018-10-23
- Revision:
- 1:3bd43cbba033
- Parent:
- 0:f4932a6188ea
File content as of revision 1:3bd43cbba033:
#include "radio.h"
DigitalOut myled(LED1);
#if defined(SX127x_H) || defined(SX126x_H)
#define BW_KHZ 500
#define SPREADING_FACTOR 11
#define CF_HZ 910800000
#elif defined(SX128x_H)
#define BW_KHZ 200
#define SPREADING_FACTOR 11
#define CF_HZ 2487000000
#endif
PwmOut pwmA(PB_11); /* CN10-18 */
PwmOut pwmB(PA_15); /* CN7-17 */
PwmOut pwmC(PB_1); /* CN10-24 */
PwmOut pwmD(PC_6); /* CN10-4 */
PwmOut* pwmPtr;
uint8_t pwm_hz;
uint8_t pwm_duty;
Timeout to;
#define PIN_ASSERT_us 500000
#define CMD_PWM_A 0x02
#define CMD_PWM_B 0x03
#define CMD_PWM_C 0x04
#define CMD_PWM_D 0x05
/**********************************************************************/
void alarm_pin_clr()
{
pwmPtr->write(0);
}
void alarm_pin_set()
{
pwmPtr->period(1.0 / pwm_hz);
pwmPtr->write(pwm_duty / 255.0);
to.attach_us(&alarm_pin_clr, PIN_ASSERT_us);
printf("%uHz, duty:%.2f\r\n", pwm_hz, pwm_duty/255.0);
}
static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length )
{
// The CRC calculation follows CCITT
const uint16_t polynom = 0x1021;
// CRC initial value
uint16_t crc = 0x0000;
if( buffer == NULL )
{
return 0;
}
for( uint16_t i = 0; i < length; ++i )
{
crc ^= ( uint16_t ) buffer[i] << 8;
for( uint16_t j = 0; j < 8; ++j )
{
crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
}
}
return crc;
}
void get_alarm()
{
uint16_t rx_crc, crc = crc_ccitt(Radio::radio.rx_buf, 7);
rx_crc = Radio::radio.rx_buf[7];
rx_crc <<= 8;
rx_crc += Radio::radio.rx_buf[8];
//printf("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc);
if (crc == rx_crc) {
uint8_t c = Radio::radio.rx_buf[0];
if (c == CMD_PWM_A || c == CMD_PWM_B || c == CMD_PWM_C || c == CMD_PWM_D) {
pwm_hz = Radio::radio.rx_buf[1];
pwm_duty = Radio::radio.rx_buf[2];
unsigned delay;
delay = Radio::radio.rx_buf[3];
delay <<= 8;
delay += Radio::radio.rx_buf[4];
delay <<= 8;
delay += Radio::radio.rx_buf[5];
delay <<= 8;
delay += Radio::radio.rx_buf[6];
switch (c) {
case CMD_PWM_A: pwmPtr = &pwmA; break;
case CMD_PWM_B: pwmPtr = &pwmB; break;
case CMD_PWM_C: pwmPtr = &pwmC; break;
case CMD_PWM_D: pwmPtr = &pwmD; break;
}
to.attach_us(&alarm_pin_set, delay);
printf("delay:%u\r\n", delay);
} else
printf("cmd? %02x\r\n", Radio::radio.rx_buf[0]);
} else
printf("crc fail %04x, %04x\r\n", rx_crc, crc);
}
void txDoneCB()
{
}
void rxDoneCB(uint8_t size, float Rssi, float Snr)
{
get_alarm();
printf("%.1fdBm snr:%.1fdB ", Rssi, Snr);
}
const RadioEvents_t rev = {
/* Dio0_top_half */ NULL,
/* TxDone_topHalf */ NULL,
/* TxDone_botHalf */ txDoneCB,
/* TxTimeout */ NULL,
/* RxDone */ rxDoneCB,
/* RxTimeout */ NULL,
/* RxError */ NULL,
/* FhssChangeChannel */NULL,
/* CadDone */ NULL
};
int main()
{
printf("\r\nreset-rx\r\n");
Radio::Init(&rev);
Radio::Standby();
Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ
Radio::SetChannel(CF_HZ);
Radio::Rx(0);
for (;;) {
Radio::service();
}
}