MMDVMHost-Private/UMP/UMP.ino

199 lines
5.2 KiB
Arduino
Raw Normal View History

2016-11-03 20:29:56 +00:00
/*
2018-01-17 19:04:00 +00:00
* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX
2016-11-03 20:29:56 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined(__AVR_ATmega1280__) && !defined(__AVR_ATmega2560__) && !defined(__AVR_ATmega32U4__) && !defined(__SAM3X8E__) && !defined(__MK20DX256__)
#include <AltSoftSerial.h>
#endif
2016-11-03 20:29:56 +00:00
#if !defined(PIN_LED)
#define PIN_LED 13
#endif
2016-11-21 21:38:15 +00:00
#define PIN_DSTAR 3
#define PIN_DMR 4
#define PIN_YSF 5
#define PIN_P25 6
2018-01-17 19:04:00 +00:00
#define PIN_NXDN 7
2016-11-03 20:29:56 +00:00
#define PIN_TX 10
#define PIN_CD 11
2016-11-03 20:29:56 +00:00
#define PIN_LOCKOUT 12
2016-11-21 21:38:15 +00:00
2018-01-17 19:04:00 +00:00
#if defined(__MK20DX256__)
2016-11-21 21:38:15 +00:00
#define FLASH_DELAY 200000U
#else
2016-11-21 21:38:15 +00:00
#define FLASH_DELAY 3200U
#endif
#if !defined(__AVR_ATmega1280__) && !defined(__AVR_ATmega2560__) && !defined(__AVR_ATmega32U4__) && !defined(__SAM3X8E__) && !defined(__MK20DX256__)
AltSoftSerial mySerial;
#endif
2016-11-03 20:29:56 +00:00
2016-11-09 08:25:55 +00:00
// Use the LOCKOUT function on the UMP
// #define USE_LOCKOUT
2016-11-03 20:29:56 +00:00
void setup()
{
Serial.begin(115200);
2016-11-17 22:24:49 +00:00
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__SAM3X8E__) || defined(__MK20DX256__)
2016-11-03 20:29:56 +00:00
Serial1.begin(9600);
#else
2016-11-17 18:42:10 +00:00
mySerial.begin(9600);
2016-11-03 20:29:56 +00:00
#endif
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_DSTAR, OUTPUT);
pinMode(PIN_DMR, OUTPUT);
pinMode(PIN_YSF, OUTPUT);
pinMode(PIN_P25, OUTPUT);
2018-01-17 19:04:00 +00:00
pinMode(PIN_NXDN, OUTPUT);
2016-11-03 20:29:56 +00:00
pinMode(PIN_TX, OUTPUT);
2016-11-17 10:48:04 +00:00
pinMode(PIN_CD, OUTPUT);
2016-11-03 20:29:56 +00:00
pinMode(PIN_LOCKOUT, INPUT);
digitalWrite(PIN_DSTAR, LOW);
digitalWrite(PIN_DMR, LOW);
digitalWrite(PIN_YSF, LOW);
digitalWrite(PIN_P25, LOW);
2018-01-17 19:04:00 +00:00
digitalWrite(PIN_NXDN, LOW);
2016-11-03 20:29:56 +00:00
digitalWrite(PIN_TX, LOW);
2016-11-17 10:48:04 +00:00
digitalWrite(PIN_CD, LOW);
2016-11-03 20:29:56 +00:00
}
#define UMP_FRAME_START 0xF0U
#define UMP_HELLO 0x00U
#define UMP_SET_MODE 0x01U
#define UMP_SET_TX 0x02U
2016-11-17 10:48:04 +00:00
#define UMP_SET_CD 0x03U
2016-11-03 20:29:56 +00:00
#define UMP_WRITE_SERIAL 0x10U
#define UMP_STATUS 0x50U
#define MODE_IDLE 0U
#define MODE_DSTAR 1U
#define MODE_DMR 2U
#define MODE_YSF 3U
#define MODE_P25 4U
2018-01-17 19:04:00 +00:00
#define MODE_NXDN 5U
2016-11-03 20:29:56 +00:00
bool m_started = false;
uint32_t m_count = 0U;
bool m_led = false;
2016-11-04 06:46:27 +00:00
uint8_t m_buffer[256U];
2016-11-03 20:29:56 +00:00
uint8_t m_offset = 0U;
uint8_t m_length = 0U;
bool m_lockout = false;
void loop()
{
while (Serial.available()) {
uint8_t c = Serial.read();
if (m_offset == 0U) {
if (c == UMP_FRAME_START) {
m_buffer[m_offset] = c;
m_offset = 1U;
}
} else if (m_offset == 1U) {
m_length = m_buffer[m_offset] = c;
m_offset = 2U;
} else {
m_buffer[m_offset] = c;
m_offset++;
2016-11-04 06:46:27 +00:00
if (m_length == m_offset) {
switch (m_buffer[2U]) {
case UMP_HELLO:
m_started = true;
break;
case UMP_SET_MODE:
digitalWrite(PIN_DSTAR, m_buffer[3U] == MODE_DSTAR ? HIGH : LOW);
digitalWrite(PIN_DMR, m_buffer[3U] == MODE_DMR ? HIGH : LOW);
digitalWrite(PIN_YSF, m_buffer[3U] == MODE_YSF ? HIGH : LOW);
digitalWrite(PIN_P25, m_buffer[3U] == MODE_P25 ? HIGH : LOW);
2018-01-17 19:04:00 +00:00
digitalWrite(PIN_NXDN, m_buffer[3U] == MODE_NXDN ? HIGH : LOW);
2016-11-04 06:46:27 +00:00
break;
case UMP_SET_TX:
digitalWrite(PIN_TX, m_buffer[3U] == 0x01U ? HIGH : LOW);
break;
2016-11-17 10:48:04 +00:00
case UMP_SET_CD:
digitalWrite(PIN_CD, m_buffer[3U] == 0x01U ? HIGH : LOW);
break;
2016-11-04 06:46:27 +00:00
case UMP_WRITE_SERIAL:
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__SAM3X8E__) || defined(__MK20DX256__)
2016-11-04 06:46:27 +00:00
Serial1.write(m_buffer + 3U, m_length - 3U);
#else
2016-11-17 18:42:10 +00:00
mySerial.write(m_buffer + 3U, m_length - 3U);
2016-11-03 20:29:56 +00:00
#endif
break;
2016-11-04 06:46:27 +00:00
default:
break;
}
2016-11-03 20:29:56 +00:00
2016-11-04 06:46:27 +00:00
m_length = 0U;
m_offset = 0U;
}
2016-11-03 20:29:56 +00:00
}
}
2016-11-09 08:32:30 +00:00
bool lockout = false;
2016-11-09 08:25:55 +00:00
#if defined(USE_LOCKOUT)
2016-11-09 08:32:30 +00:00
lockout = digitalRead(PIN_LOCKOUT) == HIGH;
2016-11-09 08:25:55 +00:00
#endif
2016-11-03 20:29:56 +00:00
if (lockout != m_lockout) {
uint8_t data[4U];
data[0U] = UMP_FRAME_START;
data[1U] = 4U;
data[2U] = UMP_STATUS;
data[3U] = lockout ? 0x01U : 0x00U;
Serial.write(data, 4U);
m_lockout = lockout;
}
2016-11-17 22:24:49 +00:00
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__SAM3X8E__) || defined(__MK20DX256__)
2016-11-03 20:29:56 +00:00
while (Serial1.available())
Serial1.read();
#else
2016-11-17 18:42:10 +00:00
while (mySerial.available())
mySerial.read();
2016-11-03 20:29:56 +00:00
#endif
m_count++;
if (m_started) {
2016-11-21 21:38:15 +00:00
if (m_count > FLASH_DELAY) {
2016-11-03 20:29:56 +00:00
digitalWrite(PIN_LED, m_led ? LOW : HIGH);
m_led = !m_led;
m_count = 0U;
}
} else {
2016-11-21 21:38:15 +00:00
if (m_count > (FLASH_DELAY * 3U)) {
2016-11-03 20:29:56 +00:00
digitalWrite(PIN_LED, m_led ? LOW : HIGH);
m_led = !m_led;
m_count = 0U;
}
}
}