OOK sender for 433 MHz remotes: PT2262, EV1527, rc-switch protocols 1-7, Intertechno self-learning and raw Manchester. The carrier is keyed with the PA enable line through a new fast BK4819 GPIO write (~25 us instead of ~300 us), and every edge is scheduled on an absolute SysTick clock. F+EXIT never opened the OOK sender: APP_ProcessKey cancels the F state for EXIT before the screen handler runs. Open it there instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
342 lines
12 KiB
C
342 lines
12 KiB
C
//
|
|
// Created by bruno on 3/30/25.
|
|
//
|
|
// OOK remote code sender. The carrier is generated in TX mode with the audio
|
|
// muted and keyed with the PA enable line (fast SPI write, ~25 us). Every edge
|
|
// is scheduled on an absolute SysTick clock, so the write latency is the same
|
|
// for every edge and does not change pulse widths, and nothing drifts.
|
|
//
|
|
|
|
#include "ooksender.h"
|
|
#include <stddef.h>
|
|
#include "ARMCM0.h"
|
|
#include "audio.h"
|
|
#include "driver/bk4819.h"
|
|
#include "driver/system.h"
|
|
#include "frequencies.h"
|
|
#include "misc.h"
|
|
#include "radio.h"
|
|
#include "ui/ooksender.h"
|
|
#include "ui/ui.h"
|
|
|
|
uint8_t gOOKSenderState = OOKSENDER_PROTO_CHOOSE;
|
|
uint8_t gOOKSenderProtocol = OOKSENDER_PROTO_EV1527;
|
|
uint8_t gOOKSenderField = OOK_FIELD_CODE;
|
|
uint32_t gOOKSenderValue[OOK_FIELD_COUNT];
|
|
|
|
#define PT2262_MAX_TRITS 12U
|
|
|
|
// rc-switch protocols: pulse length, then (high, low) in pulses for sync, 0 and 1.
|
|
// Data bits are sent MSB first, followed by the sync (rc-switch order).
|
|
typedef struct {
|
|
uint16_t pulse_us;
|
|
uint8_t sync[2];
|
|
uint8_t zero[2];
|
|
uint8_t one[2];
|
|
bool inverted;
|
|
} OOK_RcProtocol_t;
|
|
|
|
static const OOK_RcProtocol_t rcProtocols[7] = {
|
|
{350, {1, 31}, {1, 3}, {3, 1}, false}, // 1: PT2262, EV1527, most cheap remotes
|
|
{650, {1, 10}, {1, 2}, {2, 1}, false}, // 2
|
|
{100, {30, 71}, {4, 11}, {9, 6}, false}, // 3
|
|
{380, {1, 6}, {1, 3}, {3, 1}, false}, // 4
|
|
{500, {6, 14}, {1, 2}, {2, 1}, false}, // 5
|
|
{450, {23, 1}, {1, 2}, {2, 1}, true}, // 6: HT6P20B
|
|
{150, {2, 62}, {1, 6}, {6, 1}, false}, // 7: HS2303-PT
|
|
};
|
|
|
|
static const char *const fieldLabels[OOKSENDER_PROTO_END][OOK_FIELD_COUNT] = {
|
|
[OOKSENDER_PROTO_PT2262] = {"Code", NULL, NULL, "Pulse", "Repeat"},
|
|
[OOKSENDER_PROTO_EV1527] = {"ID", "Key", NULL, "Pulse", "Repeat"},
|
|
[OOKSENDER_PROTO_RCSWITCH] = {"Code", "Bits", "Proto", "Pulse", "Repeat"},
|
|
[OOKSENDER_PROTO_INTERTECHNO] = {"ID", "Unit", "On", "Pulse", "Repeat"},
|
|
[OOKSENDER_PROTO_MANCHESTER] = {"Code", "Bits", NULL, "Half", "Repeat"},
|
|
};
|
|
|
|
static const uint32_t fieldMax[OOKSENDER_PROTO_END][OOK_FIELD_COUNT] = {
|
|
[OOKSENDER_PROTO_PT2262] = {0x00FFFFFF, PT2262_MAX_TRITS, 0, 5000, 100},
|
|
[OOKSENDER_PROTO_EV1527] = {0x000FFFFF, 15, 0, 5000, 100},
|
|
[OOKSENDER_PROTO_RCSWITCH] = {0xFFFFFFFF, 32, 7, 5000, 100},
|
|
[OOKSENDER_PROTO_INTERTECHNO] = {0x03FFFFFF, 15, 1, 5000, 100},
|
|
[OOKSENDER_PROTO_MANCHESTER] = {0xFFFFFFFF, 32, 0, 5000, 100},
|
|
};
|
|
|
|
const char *OOKSENDER_FieldLabel(const uint8_t protocol, const uint8_t field)
|
|
{
|
|
return fieldLabels[protocol][field];
|
|
}
|
|
|
|
static void OOK_SetDefaults(void)
|
|
{
|
|
static const uint16_t pulse[OOKSENDER_PROTO_END] = {350, 350, 350, 260, 500};
|
|
static const uint8_t arg[OOKSENDER_PROTO_END] = {0, 0, 24, 0, 16};
|
|
|
|
gOOKSenderValue[OOK_FIELD_CODE] = 0;
|
|
gOOKSenderValue[OOK_FIELD_ARG] = arg[gOOKSenderProtocol];
|
|
gOOKSenderValue[OOK_FIELD_EXTRA] = 1; // rc-switch protocol 1 / Intertechno "on"
|
|
gOOKSenderValue[OOK_FIELD_PULSE] = pulse[gOOKSenderProtocol];
|
|
gOOKSenderValue[OOK_FIELD_REPEAT] = 10;
|
|
gOOKSenderField = OOK_FIELD_CODE;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- timing
|
|
|
|
#define OOK_TICKS_PER_US 48U // SysTick runs on the 48 MHz core clock
|
|
|
|
static uint32_t ookLastVal;
|
|
static uint32_t ookNow;
|
|
static uint32_t ookDue;
|
|
|
|
// Monotonic 48 MHz tick count. SysTick counts down and reloads every 10 ms, so this
|
|
// must be called more often than that (the busy waits below do).
|
|
static uint32_t OOK_Ticks(void)
|
|
{
|
|
const uint32_t value = SysTick->VAL;
|
|
ookNow += (ookLastVal >= value) ? ookLastVal - value : ookLastVal + SysTick->LOAD + 1U - value;
|
|
ookLastVal = value;
|
|
return ookNow;
|
|
}
|
|
|
|
// Switch the carrier at the scheduled time, then schedule the next edge.
|
|
static void OOK_Level(const bool on, const uint32_t us)
|
|
{
|
|
while ((int32_t) (OOK_Ticks() - ookDue) < 0) {
|
|
}
|
|
BK4819_ToggleGpioOutFast(BK4819_GPIO1_PIN29_PA_ENABLE, on);
|
|
ookDue += us * OOK_TICKS_PER_US;
|
|
}
|
|
|
|
static void OOK_Pair(const uint8_t ratio[2], const uint32_t pulse, const bool inverted)
|
|
{
|
|
OOK_Level(!inverted, ratio[0] * pulse);
|
|
OOK_Level(inverted, ratio[1] * pulse);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- encoders
|
|
|
|
static void OOK_SendRcBits(const OOK_RcProtocol_t *p, const uint32_t code, const uint8_t bits,
|
|
const uint32_t pulse)
|
|
{
|
|
for (uint8_t i = bits; i-- > 0;)
|
|
OOK_Pair(((code >> i) & 1U) ? p->one : p->zero, pulse, p->inverted);
|
|
OOK_Pair(p->sync, pulse, p->inverted);
|
|
}
|
|
|
|
static void OOK_SendFrame(void)
|
|
{
|
|
static const uint8_t itShort[2] = {1, 1}, itLong[2] = {1, 5};
|
|
static const uint8_t itStart[2] = {1, 10}, itEnd[2] = {1, 40};
|
|
const uint32_t code = gOOKSenderValue[OOK_FIELD_CODE];
|
|
const uint32_t arg = gOOKSenderValue[OOK_FIELD_ARG];
|
|
const uint32_t pulse = gOOKSenderValue[OOK_FIELD_PULSE];
|
|
const OOK_RcProtocol_t *p1 = &rcProtocols[0];
|
|
|
|
switch (gOOKSenderProtocol) {
|
|
case OOKSENDER_PROTO_PT2262:
|
|
// trit 0 = "0 0", trit 1 = "1 1", trit F = "0 1" (2 bits per trit in code)
|
|
for (uint32_t i = arg; i-- > 0;) {
|
|
const uint32_t trit = (code >> (2U * i)) & 3U;
|
|
OOK_Pair(trit == 1U ? p1->one : p1->zero, pulse, false);
|
|
OOK_Pair(trit == 0U ? p1->zero : p1->one, pulse, false);
|
|
}
|
|
OOK_Pair(p1->sync, pulse, false);
|
|
break;
|
|
|
|
case OOKSENDER_PROTO_EV1527:
|
|
OOK_SendRcBits(p1, (code << 4) | (arg & 0x0FU), 24, pulse);
|
|
break;
|
|
|
|
case OOKSENDER_PROTO_RCSWITCH:
|
|
OOK_SendRcBits(&rcProtocols[gOOKSenderValue[OOK_FIELD_EXTRA] - 1U], code, (uint8_t) arg, pulse);
|
|
break;
|
|
|
|
case OOKSENDER_PROTO_INTERTECHNO: {
|
|
// 26 bit ID, group bit (0), on/off, 4 bit unit; bit 0 = short+long, 1 = long+short
|
|
const uint32_t word = (code << 6) | ((gOOKSenderValue[OOK_FIELD_EXTRA] & 1U) << 4) | (arg & 0x0FU);
|
|
OOK_Pair(itStart, pulse, false);
|
|
for (uint8_t i = 32; i-- > 0;) {
|
|
const bool one = (word >> i) & 1U;
|
|
OOK_Pair(one ? itLong : itShort, pulse, false);
|
|
OOK_Pair(one ? itShort : itLong, pulse, false);
|
|
}
|
|
OOK_Pair(itEnd, pulse, false);
|
|
break;
|
|
}
|
|
|
|
case OOKSENDER_PROTO_MANCHESTER:
|
|
// 1 = on/off, 0 = off/on (half bit each), then a 20 half-bit gap
|
|
for (uint32_t i = arg; i-- > 0;) {
|
|
const bool one = (code >> i) & 1U;
|
|
OOK_Level(one, pulse);
|
|
OOK_Level(!one, pulse);
|
|
}
|
|
OOK_Level(false, 20U * pulse);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static bool OOK_Valid(void)
|
|
{
|
|
const uint32_t *v = gOOKSenderValue;
|
|
|
|
if (v[OOK_FIELD_REPEAT] == 0 || v[OOK_FIELD_PULSE] < 50U)
|
|
return false;
|
|
switch (gOOKSenderProtocol) {
|
|
case OOKSENDER_PROTO_PT2262:
|
|
return v[OOK_FIELD_ARG] > 0;
|
|
case OOKSENDER_PROTO_RCSWITCH:
|
|
return v[OOK_FIELD_ARG] > 0 && v[OOK_FIELD_EXTRA] >= 1U && v[OOK_FIELD_EXTRA] <= 7U;
|
|
case OOKSENDER_PROTO_MANCHESTER:
|
|
return v[OOK_FIELD_ARG] > 0;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static void OOK_Transmit(void)
|
|
{
|
|
if (!OOK_Valid() || TX_freq_check(gCurrentVfo->pTX->Frequency) != 0) {
|
|
AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL);
|
|
return;
|
|
}
|
|
|
|
gOOKSenderState = OOKSENDER_SENDING;
|
|
OOKSENDER_Display();
|
|
|
|
// unmodulated carrier, PA switched off until the first pulse
|
|
BK4819_EnterTxMute();
|
|
RADIO_SetTxParameters();
|
|
BK4819_WriteRegister(BK4819_REG_30,
|
|
BK4819_REG_30_ENABLE_VCO_CALIB |
|
|
BK4819_REG_30_ENABLE_UNKNOWN |
|
|
BK4819_REG_30_DISABLE_RX_LINK |
|
|
BK4819_REG_30_DISABLE_AF_DAC |
|
|
BK4819_REG_30_ENABLE_DISC_MODE |
|
|
BK4819_REG_30_ENABLE_PLL_VCO |
|
|
BK4819_REG_30_ENABLE_PA_GAIN |
|
|
BK4819_REG_30_DISABLE_MIC_ADC |
|
|
BK4819_REG_30_ENABLE_TX_DSP |
|
|
BK4819_REG_30_DISABLE_RX_DSP);
|
|
BK4819_ToggleGpioOut(BK4819_GPIO1_PIN29_PA_ENABLE, false);
|
|
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, true);
|
|
SYSTEM_DelayMs(20);
|
|
|
|
ookLastVal = SysTick->VAL;
|
|
ookNow = 0;
|
|
ookDue = 200U * OOK_TICKS_PER_US;
|
|
for (uint32_t r = 0; r < gOOKSenderValue[OOK_FIELD_REPEAT]; r++)
|
|
OOK_SendFrame();
|
|
OOK_Level(false, 0); // end of the last pulse / gap
|
|
|
|
SYSTEM_DelayMs(20);
|
|
BK4819_SetupPowerAmplifier(0, 0);
|
|
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, false);
|
|
RADIO_SetupRegisters(true); // back to normal RX
|
|
|
|
gOOKSenderState = OOKSENDER_DATA_INPUT;
|
|
gUpdateDisplay = true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- keys
|
|
|
|
static void OOK_NextField(const int8_t direction)
|
|
{
|
|
for (uint8_t n = 0; n < OOK_FIELD_COUNT; n++) {
|
|
gOOKSenderField = (uint8_t) ((gOOKSenderField + OOK_FIELD_COUNT + direction) % OOK_FIELD_COUNT);
|
|
if (fieldLabels[gOOKSenderProtocol][gOOKSenderField] != NULL)
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void OOK_Digit(const uint8_t digit)
|
|
{
|
|
uint32_t *value = &gOOKSenderValue[gOOKSenderField];
|
|
|
|
if (gOOKSenderProtocol == OOKSENDER_PROTO_PT2262 && gOOKSenderField == OOK_FIELD_CODE) {
|
|
// keys 0, 1 and 2 (= F) append a trit
|
|
if (digit > 2U || gOOKSenderValue[OOK_FIELD_ARG] >= PT2262_MAX_TRITS) {
|
|
AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL);
|
|
return;
|
|
}
|
|
*value = (*value << 2) | digit;
|
|
gOOKSenderValue[OOK_FIELD_ARG]++;
|
|
return;
|
|
}
|
|
|
|
const uint32_t max = fieldMax[gOOKSenderProtocol][gOOKSenderField];
|
|
if (*value > (max - digit) / 10U) { // value * 10 + digit > max, without overflow
|
|
AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL);
|
|
return;
|
|
}
|
|
*value = *value * 10U + digit;
|
|
|
|
// a new rc-switch protocol brings its own pulse length
|
|
if (gOOKSenderProtocol == OOKSENDER_PROTO_RCSWITCH && gOOKSenderField == OOK_FIELD_EXTRA &&
|
|
*value >= 1U && *value <= 7U)
|
|
gOOKSenderValue[OOK_FIELD_PULSE] = rcProtocols[*value - 1U].pulse_us;
|
|
}
|
|
|
|
// returns false when the field was already empty
|
|
static bool OOK_Backspace(void)
|
|
{
|
|
uint32_t *value = &gOOKSenderValue[gOOKSenderField];
|
|
|
|
if (gOOKSenderProtocol == OOKSENDER_PROTO_PT2262 && gOOKSenderField == OOK_FIELD_CODE) {
|
|
if (gOOKSenderValue[OOK_FIELD_ARG] == 0)
|
|
return false;
|
|
*value >>= 2;
|
|
gOOKSenderValue[OOK_FIELD_ARG]--;
|
|
return true;
|
|
}
|
|
if (*value == 0)
|
|
return false;
|
|
*value /= 10U;
|
|
return true;
|
|
}
|
|
|
|
void OOKSENDER_TimeSlice500ms(void) {
|
|
}
|
|
|
|
void OOKSENDER_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) {
|
|
if (!bKeyPressed || bKeyHeld)
|
|
return;
|
|
|
|
switch (gOOKSenderState) {
|
|
case OOKSENDER_PROTO_CHOOSE:
|
|
if (Key == KEY_EXIT) {
|
|
gRequestDisplayScreen = DISPLAY_MAIN;
|
|
} else if (Key == KEY_MENU) {
|
|
OOK_SetDefaults();
|
|
gOOKSenderState = OOKSENDER_DATA_INPUT;
|
|
} else if (Key == KEY_UP) {
|
|
gOOKSenderProtocol = (gOOKSenderProtocol + 1U) % OOKSENDER_PROTO_END;
|
|
} else if (Key == KEY_DOWN) {
|
|
gOOKSenderProtocol = (gOOKSenderProtocol + OOKSENDER_PROTO_END - 1U) % OOKSENDER_PROTO_END;
|
|
}
|
|
break;
|
|
|
|
case OOKSENDER_DATA_INPUT:
|
|
if (Key <= KEY_9) {
|
|
OOK_Digit((uint8_t) (Key - KEY_0));
|
|
} else if (Key == KEY_UP) {
|
|
OOK_NextField(-1);
|
|
} else if (Key == KEY_DOWN) {
|
|
OOK_NextField(1);
|
|
} else if (Key == KEY_EXIT) {
|
|
if (!OOK_Backspace())
|
|
gOOKSenderState = OOKSENDER_PROTO_CHOOSE; // EXIT on an empty field
|
|
} else if (Key == KEY_MENU || Key == KEY_PTT) {
|
|
OOK_Transmit();
|
|
}
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
gUpdateDisplay = true;
|
|
}
|