2016-08-11 00:30:43 +00:00
|
|
|
/* Hamshield
|
|
|
|
* Example: AFSK Serial Messenger
|
|
|
|
* Serial glue to send messages over APRS. You will need a
|
|
|
|
* seperate AFSK receiver to test the output of this example.
|
|
|
|
* Connect the HamShield to your Arduino. Screw the antenna
|
|
|
|
* into the HamShield RF jack. After uploading this program
|
2016-08-26 21:40:47 +00:00
|
|
|
* to your Arduino, open the Serial Monitor to monitor. Type
|
2016-08-11 00:30:43 +00:00
|
|
|
* a message under 254 characters into the bar at the top of
|
|
|
|
* the monitor. Click the "Send" button. Check for output on
|
|
|
|
* AFSK receiver.
|
2018-07-03 23:02:21 +00:00
|
|
|
*
|
|
|
|
* To send a message: connect to the Arduino over a Serial link.
|
|
|
|
* Send the following over the serial link:
|
|
|
|
* `from,to,:message
|
|
|
|
* example: * `KG7OGM,KG7OGM,:Hi there
|
|
|
|
*/
|
2015-07-15 03:05:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <HamShield.h>
|
2016-08-17 21:17:56 +00:00
|
|
|
#include <DDS.h>
|
2016-08-20 01:52:36 +00:00
|
|
|
#include <packet.h>
|
2015-07-15 03:05:20 +00:00
|
|
|
#include <avr/wdt.h>
|
|
|
|
|
2015-11-23 16:54:10 +00:00
|
|
|
#define PWM_PIN 3
|
|
|
|
#define RESET_PIN A3
|
|
|
|
#define SWITCH_PIN 2
|
|
|
|
|
2015-07-15 03:05:20 +00:00
|
|
|
HamShield radio;
|
|
|
|
DDS dds;
|
2016-08-17 21:17:56 +00:00
|
|
|
AFSK afsk;
|
2015-07-15 03:05:20 +00:00
|
|
|
String messagebuff = "";
|
|
|
|
String origin_call = "";
|
|
|
|
String destination_call = "";
|
|
|
|
String textmessage = "";
|
|
|
|
int msgptr = 0;
|
|
|
|
|
|
|
|
void setup() {
|
2015-11-23 16:54:10 +00:00
|
|
|
// NOTE: if not using PWM out, it should be held low to avoid tx noise
|
|
|
|
pinMode(PWM_PIN, OUTPUT);
|
|
|
|
digitalWrite(PWM_PIN, LOW);
|
|
|
|
|
|
|
|
// prep the switch
|
|
|
|
pinMode(SWITCH_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
// set up the reset control pin
|
|
|
|
pinMode(RESET_PIN, OUTPUT);
|
|
|
|
// turn on the radio
|
|
|
|
digitalWrite(RESET_PIN, HIGH);
|
2018-08-05 02:47:58 +00:00
|
|
|
delay(5); // wait for device to come up
|
2015-11-23 16:54:10 +00:00
|
|
|
|
2016-08-11 00:30:43 +00:00
|
|
|
Serial.begin(9600);
|
2016-04-14 02:12:06 +00:00
|
|
|
|
2015-07-15 03:05:20 +00:00
|
|
|
radio.initialize();
|
|
|
|
radio.frequency(145570);
|
2016-01-05 03:36:37 +00:00
|
|
|
radio.setRfPower(0);
|
2016-09-24 22:32:24 +00:00
|
|
|
radio.setVolume1(0xFF);
|
|
|
|
radio.setVolume2(0xFF);
|
|
|
|
radio.setSQHiThresh(-100);
|
|
|
|
radio.setSQLoThresh(-100);
|
|
|
|
radio.setSQOn();
|
2018-08-05 02:47:58 +00:00
|
|
|
//radio.bypassPreDeEmph();
|
2015-07-15 03:05:20 +00:00
|
|
|
dds.start();
|
2016-08-17 21:17:56 +00:00
|
|
|
afsk.start(&dds);
|
2015-07-15 03:05:20 +00:00
|
|
|
delay(100);
|
2016-09-24 22:32:24 +00:00
|
|
|
radio.setModeReceive();
|
2015-07-15 03:05:20 +00:00
|
|
|
Serial.println("HELLO");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
if(Serial.available()) {
|
|
|
|
char temp = (char)Serial.read();
|
|
|
|
if(temp == '`') {
|
2016-09-25 00:33:00 +00:00
|
|
|
//Serial.println(messagebuff);
|
|
|
|
prepMessage();
|
|
|
|
msgptr = 0;
|
|
|
|
Serial.print("!!");
|
|
|
|
}
|
2015-07-15 03:05:20 +00:00
|
|
|
else {
|
|
|
|
messagebuff += temp;
|
|
|
|
msgptr++;
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 22:32:24 +00:00
|
|
|
if(msgptr > 254) { messagebuff = ""; Serial.print("X!"); }
|
|
|
|
|
|
|
|
if(afsk.decoder.read() || afsk.rxPacketCount()) {
|
|
|
|
// A true return means something was put onto the packet FIFO
|
|
|
|
// If we actually have data packets in the buffer, process them all now
|
|
|
|
while(afsk.rxPacketCount()) {
|
|
|
|
AFSK::Packet *packet = afsk.getRXPacket();
|
|
|
|
Serial.print(F("Packet: "));
|
|
|
|
if(packet) {
|
|
|
|
packet->printPacket(&Serial);
|
|
|
|
AFSK::PacketBuffer::freePacket(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 03:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void prepMessage() {
|
|
|
|
radio.setModeTransmit();
|
2016-09-25 00:33:00 +00:00
|
|
|
delay(1000);
|
|
|
|
origin_call = messagebuff.substring(0,messagebuff.indexOf(',')); // get originating callsign
|
|
|
|
destination_call = messagebuff.substring(messagebuff.indexOf(',')+1,messagebuff.indexOf(',',messagebuff.indexOf(',')+1)); // get the destination call
|
|
|
|
textmessage = messagebuff.substring(messagebuff.indexOf(":")+1);
|
2015-07-15 03:05:20 +00:00
|
|
|
|
2016-09-25 00:33:00 +00:00
|
|
|
// Serial.print("From: "); Serial.print(origin_call); Serial.print(" To: "); Serial.println(destination_call); Serial.println("Text: "); Serial.println(textmessage);
|
|
|
|
|
|
|
|
AFSK::Packet *packet = AFSK::PacketBuffer::makePacket(22 + 32);
|
|
|
|
|
|
|
|
packet->start();
|
|
|
|
packet->appendCallsign(origin_call.c_str(),0);
|
|
|
|
packet->appendCallsign(destination_call.c_str(),15,true);
|
|
|
|
packet->appendFCS(0x03);
|
|
|
|
packet->appendFCS(0xf0);
|
|
|
|
packet->print(textmessage);
|
|
|
|
packet->finish();
|
|
|
|
|
|
|
|
bool ret = afsk.putTXPacket(packet);
|
|
|
|
|
|
|
|
if(afsk.txReady()) {
|
|
|
|
Serial.println(F("txReady"));
|
|
|
|
radio.setModeTransmit();
|
|
|
|
//delay(100);
|
|
|
|
if(afsk.txStart()) {
|
|
|
|
Serial.println(F("txStart"));
|
|
|
|
} else {
|
|
|
|
radio.setModeReceive();
|
2015-07-15 03:05:20 +00:00
|
|
|
}
|
2016-09-25 00:33:00 +00:00
|
|
|
}
|
|
|
|
// Wait 2 seconds before we send our beacon again.
|
|
|
|
Serial.println("tick");
|
|
|
|
// Wait up to 2.5 seconds to finish sending, and stop transmitter.
|
|
|
|
// TODO: This is hackery.
|
|
|
|
for(int i = 0; i < 500; i++) {
|
|
|
|
if(afsk.encoder.isDone())
|
|
|
|
break;
|
|
|
|
delay(50);
|
|
|
|
}
|
|
|
|
Serial.println("Done sending");
|
|
|
|
radio.setModeReceive();
|
2015-07-15 03:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:54:10 +00:00
|
|
|
|
2015-07-15 03:05:20 +00:00
|
|
|
ISR(TIMER2_OVF_vect) {
|
|
|
|
TIFR2 = _BV(TOV2);
|
|
|
|
static uint8_t tcnt = 0;
|
|
|
|
if(++tcnt == 8) {
|
2016-09-25 00:33:00 +00:00
|
|
|
dds.clockTick();
|
2015-07-15 03:05:20 +00:00
|
|
|
tcnt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ISR(ADC_vect) {
|
|
|
|
static uint8_t tcnt = 0;
|
|
|
|
TIFR1 = _BV(ICF1); // Clear the timer flag
|
|
|
|
dds.clockTick();
|
|
|
|
if(++tcnt == 1) {
|
2016-09-25 00:33:00 +00:00
|
|
|
afsk.timer();
|
2015-07-15 03:05:20 +00:00
|
|
|
tcnt = 0;
|
|
|
|
}
|
2016-09-25 00:33:00 +00:00
|
|
|
}
|