m0zah/GB3TX/GB3TX.ino
2024-05-06 11:28:43 +01:00

165 lines
4.3 KiB
C++

#include "config.h"
#include "repeater.h"
void setup() {
pinMode(3, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(PIP_ATT, OUTPUT);
}
void loop() {
repeater rpt;
repeater* myrpt;
myrpt = &rpt;
myrpt->callsign = ID;
myrpt->params.cw_pitch = CW_PITCH;
myrpt->serial.enable = SERIAL;
myrpt->serial.speed = 115200;
if (myrpt->serial.enable)
Serial.begin(myrpt->serial.speed);
/* Setup params */
myrpt->params.cw_speed = 1200 / CW_SPEED;
myrpt->params.close_down = CLOSEDOWN;
myrpt->params.pip_length = RFPIPLEN;
myrpt->params.pip_pitch = RFPIP;
myrpt->params.pip_gw_pitch = RFPIP;
myrpt->params.pip_gw_length = GWPIPLEN;
myrpt->params.courtesy = COURTESY;
myrpt->params.pip_letter = PIP_LETTER;
myrpt->params.pip_gw_letter = PIP_GW_LETTER;
myrpt->params.start_up = START;
delay(1000);
myrpt->transmitter.tx = true;
sendID(myrpt);
myrpt->state = SLEEP;
unsigned long ht = millis();
unsigned long tot = ht;
unsigned long kc;
myrpt->id_time = 0;
serial_writer(&myrpt->serial, "READY");
digitalWrite(PIP_ATT, LOW);
myrpt->stats.gw_time = 0;
myrpt->stats.tx_time = 0;
myrpt->stats.rx_time = 0;
while (1) {
serial_command(&myrpt->serial, myrpt);
rx(myrpt);
if (busy(myrpt) && myrpt->state < KEYCHUNK) {
myrpt->state = KEYCHUNK;
serial_writer(&myrpt->serial, "KEYCHUNK");
kc = millis();
}
if (myrpt->state == KEYCHUNK && millis() - kc > KEYCHUNK_TIME) {
if (!myrpt->receiver.rx) {
myrpt->state = TT;
serial_writer(&myrpt->serial, "RX: RF");
} else {
myrpt->state = GW;
serial_writer(&myrpt->serial, "RX: GATEWAY");
myrpt->timer.gw_start = millis();
}
tot = millis();
if (myrpt->params.start_up)
sendID(myrpt);
}
if ((myrpt->receiver.rx && myrpt->gateway.receiver.rx) && myrpt->state == KEYCHUNK) {
serial_writer(&myrpt->serial, "SHORT SIGNAL");
myrpt->state = IDLE;
}
if ((myrpt->receiver.rx && myrpt->gateway.receiver.rx) && myrpt->state > HANG) {
serial_writer(&myrpt->serial, "HANG");
if (myrpt->state == GW)
myrpt->stats.gw_time += millis() - myrpt->timer.gw_start;
delay(500);
myrpt->last = myrpt->state;
myrpt->state = HANG;
ht = millis();
myrpt->tail = ht;
if (ht - tot < PIP_KEYCHUNK || busy(myrpt))
continue;
courtesyTone(myrpt);
}
if (myrpt->state == HANG && TAILPIPS) {
if (millis() - myrpt->tail >= TAIL_PIP_DELAY && !busy(myrpt)) {
myrpt->tail = millis();
tone(PIP, TAILPIP_PITCH, myrpt->params.pip_length);
}
}
if (!myrpt->receiver.rx && myrpt->state == HANG) {
serial_writer(&myrpt->serial, "RX: RF");
myrpt->state = TT;
tot = millis();
}
if (!myrpt->gateway.receiver.rx && myrpt->state == HANG) {
serial_writer(&myrpt->serial, "RX: GATEWAY");
myrpt->state = GW;
tot = millis();
}
if (millis() - tot >= TIMEOUT && (myrpt->state == TT || myrpt->state == GW)) {
serial_writer(&myrpt->serial, "REPEATER: TIMEOUT");
myrpt->state = TIMEOUT;
tone(PIP, 440, 1000);
delay(1000);
}
if (myrpt->state == HANG && millis() - ht > HANGTIME) {
if (myrpt->params.close_down) {
serial_writer(&myrpt->serial, "REPEATER: CLOSEDOWN ID");
myrpt->state = SLEEP;
sendID(myrpt);
continue;
}
serial_writer(&myrpt->serial, "REPEATER: IDLE");
myrpt->state = IDLE;
}
if (myrpt->state == TIMEOUT) {
myrpt->state = IDLE;
tx(myrpt);
while (1) {
rx(myrpt);
if (myrpt->receiver.rx && myrpt->gateway.receiver.rx)
break;
}
serial_writer(&myrpt->serial, "REPEATER: TIMEOUT RESET");
}
if (myrpt->state > SLEEP && millis() - myrpt->id_time >= (IDTIME - 60000)) {
/* Check if the repeater is in use
* wait the full ID time
*/
if (myrpt->state > IDLE && millis() - myrpt->id_time < IDTIME) {
continue;
}
/* Repeater is IDLE, bring the transmitter up */
if (!myrpt->transmitter.tx) {
myrpt->state = HANG;
tx(myrpt);
myrpt->state = SLEEP;
}
serial_writer(&myrpt->serial, "REPEATER: ID");
delay(250);
sendID(myrpt);
}
tx(myrpt);
}
}