From b24e19b43e33906b4cea548ef91afec4448d15c6 Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Sun, 3 Sep 2017 16:56:10 +1000 Subject: [PATCH 1/7] Work in progress adding MIC-E format --- LibAPRS/LibAPRS.cpp | 169 ++++++++++++++++++++++++++++++++++++++------ LibAPRS/LibAPRS.h | 10 +-- 2 files changed, 153 insertions(+), 26 deletions(-) diff --git a/LibAPRS/LibAPRS.cpp b/LibAPRS/LibAPRS.cpp index f271933..31c8e00 100644 --- a/LibAPRS/LibAPRS.cpp +++ b/LibAPRS/LibAPRS.cpp @@ -18,14 +18,17 @@ AX25Call dst; AX25Call path1; AX25Call path2; -char CALL[7] = "NOCALL"; -int CALL_SSID = 0; -char DST[7] = "APZMDM"; -int DST_SSID = 0; -char PATH1[7] = "WIDE1"; -int PATH1_SSID = 1; -char PATH2[7] = "WIDE2"; -int PATH2_SSID = 2; +#define MAX_CALL_LENGTH 6 +char CALL[MAX_CALL_LENGTH] = "NOCALL"; +int8_t CALL_SSID = 0; +char DST[MAX_CALL_LENGTH] = "APZMDM"; +int8_t DST_SSID = 0; +char PATH1[MAX_CALL_LENGTH] = "WIDE1"; +int8_t PATH1_SSID = 1; +char PATH2[MAX_CALL_LENGTH] = "WIDE2"; +int8_t PATH2_SSID = 2; +uint8_t MICE_MSG; +uint8_t MICE_SSID; AX25Call path[4]; @@ -39,11 +42,13 @@ uint8_t power = 10; uint8_t height = 10; uint8_t gain = 10; uint8_t directivity = 10; +uint16_t speed; +uint16_t course; ///////////////////////// // Message packet assembly fields -char message_recip[7]; -int message_recip_ssid = -1; +char message_recip[6]; +int8_t message_recip_ssid = -1; int message_seq = 0; char lastMessage[67]; @@ -63,8 +68,8 @@ void APRS_poll(void) { ax25_poll(&AX25); } -void APRS_setCallsign(char *call, int ssid) { - memset(CALL, 0, 7); +void APRS_setCallsign(char *call, int8_t ssid) { + memset(CALL, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { CALL[i] = call[i]; @@ -73,8 +78,8 @@ void APRS_setCallsign(char *call, int ssid) { CALL_SSID = ssid; } -void APRS_setDestination(char *call, int ssid) { - memset(DST, 0, 7); +void APRS_setDestination(char *call, int8_t ssid) { + memset(DST, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { DST[i] = call[i]; @@ -83,8 +88,8 @@ void APRS_setDestination(char *call, int ssid) { DST_SSID = ssid; } -void APRS_setPath1(char *call, int ssid) { - memset(PATH1, 0, 7); +void APRS_setPath1(char *call, int8_t ssid) { + memset(PATH1, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { PATH1[i] = call[i]; @@ -93,8 +98,8 @@ void APRS_setPath1(char *call, int ssid) { PATH1_SSID = ssid; } -void APRS_setPath2(char *call, int ssid) { - memset(PATH2, 0, 7); +void APRS_setPath2(char *call, int8_t ssid) { + memset(PATH2, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { PATH2[i] = call[i]; @@ -103,8 +108,8 @@ void APRS_setPath2(char *call, int ssid) { PATH2_SSID = ssid; } -void APRS_setMessageDestination(char *call, int ssid) { - memset(message_recip, 0, 7); +void APRS_setMessageDestination(char *call, int8_t ssid) { + memset(message_recip, 0, 6); int i = 0; while (i < 6 && call[i] != 0) { message_recip[i] = call[i]; @@ -175,6 +180,24 @@ void APRS_setDirectivity(int s) { } } +// Set the speed in knots. Valid speeds are 0-799 knots +void APRS_setSpeed(int s) { + if (s >= 0 && s < 800) { + speed = s; + } else { + speed = 0; + } +} + +// Set the course, valid courses are 0-360 where 0 is unknown and 360 is due north +void APRS_setCourse(int c) { + if (c >= 0 && c <= 360) { + course = c; + } else { + course = 0; + } +} + void APRS_printSettings() { Serial.println(F("LibAPRS Settings:")); Serial.print(F("Callsign: ")); Serial.print(CALL); Serial.print(F("-")); Serial.println(CALL_SSID); @@ -218,6 +241,110 @@ void APRS_sendPkt(void *_buffer, size_t length) { ax25_sendVia(&AX25, path, countof(path), buffer, length); } +// 3 bits of MIC-E message. Bit A is the most significant bit. +// If custom is set then we use the custom message bits when encoding +// Standard messages Custom Messages +// 0x07: M0: Off Duty C0: Custom-0 +// 0x06: M1: En route C1: Custom-1 +// 0x05: M2: In service C2: Custom-2 +// 0x04: M3: Returning C3: Custom-3 +// 0x03: M4: Committed C4: Custom-4 +// 0x02: M5: Special C5: Custom-5 +// 0x01: M6: Priority C6: Custom-6 +// 0x00: Emergency Emergency +void APRS_set_mice_msg(uint8_t msg, bool custom) { + MICE_MSG = msg & 0x07; + // If custom message bits, store the custom flag in bit 7 of the MICE_MSG + if (custom) { + MICE_MSG |= 0x80; + } +} + +void APRS_set_mice_ssid(uint8_t ssid) { + MICE_SSID = ssid & 0x0F; +} + +uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { + uint8_t payloadLength = 9; + uint8_t *packet = (uint8_t*)malloc(payloadLength); + // Sanity check the latitude and longtitude + if (latitude[7] != 'N' && latitude[7] != 'S') { + return 1; + } + if (longtitude[8] != 'E' && longtitude[8] != 'W') { + return 1; + } + // Build the Destination callsign with the latitude information + DST[0] = (latitude[0] & 0x04) | 0x30; + if (MICE_MSG & 0x04) { + if (MICE_MSG & 0x80) { // Standard message bit + DST[0] += 0x20; + } else { + DST[0] += 0x17; // Custom message bit + } + } + DST[1] = (latitude[1] & 0x04) | 0x30; + if (MICE_MSG & 0x02) { + if (MICE_MSG & 0x80) { // Standard message bit + DST[1] += 0x20; + } else { + DST[1] += 0x17; // Custom message bit + } + } + DST[2] = (latitude[2] & 0x04) | 0x30; + if (MICE_MSG & 0x01) { + if (MICE_MSG & 0x80) { // Standard message bit + DST[2] += 0x20; + } else { + DST[2] += 0x17; // Custom message bit + } + } + DST[3] = (latitude[3] & 0x04) | 0x30; + if (latitude[7] == 'N') { // North/South Latitude Indicator + DST[3] += 0x20; + } + DST[4] = (latitude[5] & 0x04) | 0x30; // Use latitude[5] becuase latitude[4] is a . + // if (longtitude[0] == '1') { // If the longitude is > 100, set this bit + // DST[4] += 0x20; + // } + DST[5] = (latitude[6] & 0x04) | 0x30; + if (longtitude[8] == 'W') { // If the longitude is > 100, set this bit + DST[4] += 0x20; + } + if (MICE_SSID == 0) { + path_len = 4; + } + + packet[0] = 0x60; // The ` character indicating valid GPS data + // Degrees. If longitude > 100 the +100 longitude bit is set in the Destination field, but only in + // certain circumstances, see http://www.aprs.org/doc/APRS101.PDF page 47 + uint8_t lon_deg = ((longitude[1] & 0x04) * 10 + (longitude[2] & 0x04)); + if (lon < 9) { + packet[1] = 118 + lon_deg; + DST[4] += 0x20; + } else if (lon < 100) { + packet[1] = 38 + lon_deg - 10; + } else if (lon < 110) { + packet[1] = 108 + lon_deg - 100; + DST[4] += 0x20; + } else { + packet[1] = 38 + lon_deg - 110; + DST[4] += 0x20; + } + + uint8_t lon_min = ((longitude[3] & 0x04) * 10) + (longitude[4] & 0x04)); + if (lon_min < 10) { + packet[2] = 88 + lon_min; + } else { + packet[2] = 38 + lon_min - 10; + } + packet[3] = ((longitude[6] & 0x04) * 10) + (longitude[7] & 0x04)) + 28; + + // bytes 4, 5 and 6 encode the speed and course + packet[4] = (speed / 10) + 28; + +} + // Dynamic RAM usage of this function is 30 bytes void APRS_sendLoc(void *_buffer, size_t length) { size_t payloadLength = 20+length; @@ -303,7 +430,7 @@ void APRS_sendMsg(void *_buffer, size_t length) { packet[12+length] = h+48; packet[13+length] = d+48; packet[14+length] = n+48; - + APRS_sendPkt(packet, payloadLength); free(packet); } diff --git a/LibAPRS/LibAPRS.h b/LibAPRS/LibAPRS.h index 78e725a..2f806a2 100644 --- a/LibAPRS/LibAPRS.h +++ b/LibAPRS/LibAPRS.h @@ -11,11 +11,11 @@ void APRS_init(int reference, bool open_squelch); void APRS_poll(void); -void APRS_setCallsign(char *call, int ssid); -void APRS_setDestination(char *call, int ssid); -void APRS_setMessageDestination(char *call, int ssid); -void APRS_setPath1(char *call, int ssid); -void APRS_setPath2(char *call, int ssid); +void APRS_setCallsign(char *call, int8_t ssid); +void APRS_setDestination(char *call, int8_t ssid); +void APRS_setMessageDestination(char *call, int8_t ssid); +void APRS_setPath1(char *call, int8_t ssid); +void APRS_setPath2(char *call, int8_t ssid); void APRS_setPreamble(unsigned long pre); void APRS_setTail(unsigned long tail); From 851be6893ed43a6b44a72c789e8dfc29b78d7077 Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Sun, 3 Sep 2017 19:01:58 +1000 Subject: [PATCH 2/7] Working(?) MIC-E encoder --- LibAPRS/LibAPRS.cpp | 50 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/LibAPRS/LibAPRS.cpp b/LibAPRS/LibAPRS.cpp index 31c8e00..c2d8503 100644 --- a/LibAPRS/LibAPRS.cpp +++ b/LibAPRS/LibAPRS.cpp @@ -311,24 +311,21 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { if (longtitude[8] == 'W') { // If the longitude is > 100, set this bit DST[4] += 0x20; } - if (MICE_SSID == 0) { - path_len = 4; - } packet[0] = 0x60; // The ` character indicating valid GPS data // Degrees. If longitude > 100 the +100 longitude bit is set in the Destination field, but only in // certain circumstances, see http://www.aprs.org/doc/APRS101.PDF page 47 uint8_t lon_deg = ((longitude[1] & 0x04) * 10 + (longitude[2] & 0x04)); - if (lon < 9) { + if (lon < 10) { packet[1] = 118 + lon_deg; DST[4] += 0x20; } else if (lon < 100) { packet[1] = 38 + lon_deg - 10; } else if (lon < 110) { - packet[1] = 108 + lon_deg - 100; + packet[1] = 108 + (lon_deg - 100); DST[4] += 0x20; } else { - packet[1] = 38 + lon_deg - 110; + packet[1] = 38 + (lon_deg - 110); DST[4] += 0x20; } @@ -341,8 +338,45 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { packet[3] = ((longitude[6] & 0x04) * 10) + (longitude[7] & 0x04)) + 28; // bytes 4, 5 and 6 encode the speed and course - packet[4] = (speed / 10) + 28; - + // Page 50 of the APRS spec + packet[4] = (speed / 10) + 28; // 100's an 10's of knots + packet[5] = (speed % 10) * 10 + 32; // 1's of knots + if (course > 99 && course < 200) { // 100's of degrees + packet[5] += 1; + } else if (course < 300) { + packet[5] += 2; + } else { + packet[5] += 3; + } + packet[6] = (course % 100) = 28; + packet[7] = (uint8_t)symbol; + packet[8] = (uint8_t)symbolTable; + + if (MICE_SSID != 0) { + path_len = 0; + } else { + path_len = 4; + } + + uint8_t *buffer = (uint8_t *)_buffer; + + memcpy(dst.call, DST, 6); + dst.ssid = DST_SSID; + + memcpy(src.call, CALL, 6); + src.ssid = CALL_SSID; + + memcpy(path1.call, PATH1, 6); + path1.ssid = PATH1_SSID; + + memcpy(path2.call, PATH2, 6); + path2.ssid = PATH2_SSID; + + path[0] = dst; + path[1] = src; + path[2] = path1; + path[3] = path2; + ax25_sendVia(&AX25, path, path_len, buffer, length); } // Dynamic RAM usage of this function is 30 bytes From 5a21cf4e8ab0a96364489b467862a01ab1f7e7de Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Sun, 3 Sep 2017 21:59:51 +1000 Subject: [PATCH 3/7] Working Mic-E Encoder --- LibAPRS/LibAPRS.cpp | 77 ++++++++++++++++++++++++--------------------- LibAPRS/LibAPRS.h | 6 ++++ 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/LibAPRS/LibAPRS.cpp b/LibAPRS/LibAPRS.cpp index c2d8503..c473cdc 100644 --- a/LibAPRS/LibAPRS.cpp +++ b/LibAPRS/LibAPRS.cpp @@ -18,7 +18,7 @@ AX25Call dst; AX25Call path1; AX25Call path2; -#define MAX_CALL_LENGTH 6 +#define MAX_CALL_LENGTH 7 char CALL[MAX_CALL_LENGTH] = "NOCALL"; int8_t CALL_SSID = 0; char DST[MAX_CALL_LENGTH] = "APZMDM"; @@ -265,7 +265,8 @@ void APRS_set_mice_ssid(uint8_t ssid) { } uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { - uint8_t payloadLength = 9; + uint8_t path_len; + uint8_t payloadLength = 9 + length; uint8_t *packet = (uint8_t*)malloc(payloadLength); // Sanity check the latitude and longtitude if (latitude[7] != 'N' && latitude[7] != 'S') { @@ -275,53 +276,53 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { return 1; } // Build the Destination callsign with the latitude information - DST[0] = (latitude[0] & 0x04) | 0x30; + DST[0] = (latitude[0] & 0x0F) | 0x30; if (MICE_MSG & 0x04) { - if (MICE_MSG & 0x80) { // Standard message bit - DST[0] += 0x20; + if (MICE_MSG & 0x80) { + DST[0] += 0x17; // Custom message bit } else { - DST[0] += 0x17; // Custom message bit + DST[0] += 0x20; // Standard message bit } } - DST[1] = (latitude[1] & 0x04) | 0x30; + DST[1] = (latitude[1] & 0x0F) | 0x30; if (MICE_MSG & 0x02) { - if (MICE_MSG & 0x80) { // Standard message bit - DST[1] += 0x20; + if (MICE_MSG & 0x80) { + DST[1] += 0x17; // Custom message bit } else { - DST[1] += 0x17; // Custom message bit + DST[1] += 0x20; // Standard message bit } } - DST[2] = (latitude[2] & 0x04) | 0x30; + DST[2] = (latitude[2] & 0x0F) | 0x30; if (MICE_MSG & 0x01) { - if (MICE_MSG & 0x80) { // Standard message bit - DST[2] += 0x20; + if (MICE_MSG & 0x80) { + DST[2] += 0x17; // Custom message bit } else { - DST[2] += 0x17; // Custom message bit + DST[2] += 0x20; // Standard message bit } } - DST[3] = (latitude[3] & 0x04) | 0x30; + DST[3] = (latitude[3] & 0x0F) | 0x30; if (latitude[7] == 'N') { // North/South Latitude Indicator DST[3] += 0x20; } - DST[4] = (latitude[5] & 0x04) | 0x30; // Use latitude[5] becuase latitude[4] is a . - // if (longtitude[0] == '1') { // If the longitude is > 100, set this bit + DST[4] = (latitude[5] & 0x0F) | 0x30; // Use latitude[5] becuase latitude[4] is a . + // if (longtitude[0] == '1') { // If the longtitude is > 100, set this bit // DST[4] += 0x20; // } - DST[5] = (latitude[6] & 0x04) | 0x30; - if (longtitude[8] == 'W') { // If the longitude is > 100, set this bit + DST[5] = (latitude[6] & 0x0F) | 0x30; + if (longtitude[8] == 'W') { // If the longtitude is > 100, set this bit DST[4] += 0x20; } packet[0] = 0x60; // The ` character indicating valid GPS data - // Degrees. If longitude > 100 the +100 longitude bit is set in the Destination field, but only in + // Degrees. If longtitude > 100 the +100 longtitude bit is set in the Destination field, but only in // certain circumstances, see http://www.aprs.org/doc/APRS101.PDF page 47 - uint8_t lon_deg = ((longitude[1] & 0x04) * 10 + (longitude[2] & 0x04)); - if (lon < 10) { + uint8_t lon_deg = ((longtitude[0] & 0x0F) * 100 + (longtitude[1] & 0x0F) * 10 + (longtitude[2] & 0x0F)); + if (lon_deg < 10) { packet[1] = 118 + lon_deg; DST[4] += 0x20; - } else if (lon < 100) { + } else if (lon_deg < 100) { packet[1] = 38 + lon_deg - 10; - } else if (lon < 110) { + } else if (lon_deg < 110) { packet[1] = 108 + (lon_deg - 100); DST[4] += 0x20; } else { @@ -329,26 +330,26 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { DST[4] += 0x20; } - uint8_t lon_min = ((longitude[3] & 0x04) * 10) + (longitude[4] & 0x04)); + uint8_t lon_min = ((longtitude[3] & 0x0F) * 10) + (longtitude[4] & 0x0F); if (lon_min < 10) { packet[2] = 88 + lon_min; } else { packet[2] = 38 + lon_min - 10; } - packet[3] = ((longitude[6] & 0x04) * 10) + (longitude[7] & 0x04)) + 28; + packet[3] = ((longtitude[6] & 0x0F) * 10) + (longtitude[7] & 0x0F) + 28; // bytes 4, 5 and 6 encode the speed and course // Page 50 of the APRS spec packet[4] = (speed / 10) + 28; // 100's an 10's of knots packet[5] = (speed % 10) * 10 + 32; // 1's of knots - if (course > 99 && course < 200) { // 100's of degrees - packet[5] += 1; - } else if (course < 300) { - packet[5] += 2; - } else { - packet[5] += 3; + if (course > 299) { + packet[5] += 3; + } else if (course > 199) { + packet[5] += 2; + } else if (course > 99) { + packet[5] += 1; } - packet[6] = (course % 100) = 28; + packet[6] = (course % 100) + 28; packet[7] = (uint8_t)symbol; packet[8] = (uint8_t)symbolTable; @@ -358,8 +359,6 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { path_len = 4; } - uint8_t *buffer = (uint8_t *)_buffer; - memcpy(dst.call, DST, 6); dst.ssid = DST_SSID; @@ -376,7 +375,13 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { path[1] = src; path[2] = path1; path[3] = path2; - ax25_sendVia(&AX25, path, path_len, buffer, length); + if (length > 0) { + uint8_t *buffer = (uint8_t *)_buffer; + memcpy(&packet[9], buffer, length); + } + ax25_sendVia(&AX25, path, path_len, packet, payloadLength); + free(packet); + return 0; } // Dynamic RAM usage of this function is 30 bytes diff --git a/LibAPRS/LibAPRS.h b/LibAPRS/LibAPRS.h index 2f806a2..40a8eb4 100644 --- a/LibAPRS/LibAPRS.h +++ b/LibAPRS/LibAPRS.h @@ -28,11 +28,17 @@ void APRS_setPower(int s); void APRS_setHeight(int s); void APRS_setGain(int s); void APRS_setDirectivity(int s); +void APRS_setSpeed(int s); +void APRS_setCourse(int c); void APRS_sendPkt(void *_buffer, size_t length); void APRS_sendLoc(void *_buffer, size_t length); void APRS_sendMsg(void *_buffer, size_t length); void APRS_msgRetry(); +uint8_t APRS_sendLoc_mice(void *_buffer, size_t length); +void APRS_set_mice_ssid(uint8_t ssid); +void APRS_set_mice_msg(uint8_t msg, bool custom); + void APRS_printSettings(); From 06d61c6a14c19a2f602deba18f8a3ee16a55a064 Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Sun, 3 Sep 2017 22:06:15 +1000 Subject: [PATCH 4/7] Reverted some of the memory reduction changes --- LibAPRS/LibAPRS.cpp | 12 ++++++------ LibAPRS/LibAPRS.h | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/LibAPRS/LibAPRS.cpp b/LibAPRS/LibAPRS.cpp index c473cdc..71b5749 100644 --- a/LibAPRS/LibAPRS.cpp +++ b/LibAPRS/LibAPRS.cpp @@ -20,13 +20,13 @@ AX25Call path2; #define MAX_CALL_LENGTH 7 char CALL[MAX_CALL_LENGTH] = "NOCALL"; -int8_t CALL_SSID = 0; +int CALL_SSID = 0; char DST[MAX_CALL_LENGTH] = "APZMDM"; -int8_t DST_SSID = 0; +int DST_SSID = 0; char PATH1[MAX_CALL_LENGTH] = "WIDE1"; -int8_t PATH1_SSID = 1; +int PATH1_SSID = 1; char PATH2[MAX_CALL_LENGTH] = "WIDE2"; -int8_t PATH2_SSID = 2; +int PATH2_SSID = 2; uint8_t MICE_MSG; uint8_t MICE_SSID; @@ -47,8 +47,8 @@ uint16_t course; ///////////////////////// // Message packet assembly fields -char message_recip[6]; -int8_t message_recip_ssid = -1; +char message_recip[7]; +int message_recip_ssid = -1; int message_seq = 0; char lastMessage[67]; diff --git a/LibAPRS/LibAPRS.h b/LibAPRS/LibAPRS.h index 40a8eb4..4881d84 100644 --- a/LibAPRS/LibAPRS.h +++ b/LibAPRS/LibAPRS.h @@ -11,11 +11,11 @@ void APRS_init(int reference, bool open_squelch); void APRS_poll(void); -void APRS_setCallsign(char *call, int8_t ssid); -void APRS_setDestination(char *call, int8_t ssid); -void APRS_setMessageDestination(char *call, int8_t ssid); -void APRS_setPath1(char *call, int8_t ssid); -void APRS_setPath2(char *call, int8_t ssid); +void APRS_setCallsign(char *call, int ssid); +void APRS_setDestination(char *call, int ssid); +void APRS_setMessageDestination(char *call, int ssid); +void APRS_setPath1(char *call, int ssid); +void APRS_setPath2(char *call, int ssid); void APRS_setPreamble(unsigned long pre); void APRS_setTail(unsigned long tail); From bd5ad6e8b67a96771773b6888c7ed828d6dd6a9c Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Sun, 3 Sep 2017 22:08:06 +1000 Subject: [PATCH 5/7] Reverted more memory reduction changes --- LibAPRS/LibAPRS.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/LibAPRS/LibAPRS.cpp b/LibAPRS/LibAPRS.cpp index 71b5749..0f3b0fc 100644 --- a/LibAPRS/LibAPRS.cpp +++ b/LibAPRS/LibAPRS.cpp @@ -68,7 +68,7 @@ void APRS_poll(void) { ax25_poll(&AX25); } -void APRS_setCallsign(char *call, int8_t ssid) { +void APRS_setCallsign(char *call, int ssid) { memset(CALL, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { @@ -78,7 +78,7 @@ void APRS_setCallsign(char *call, int8_t ssid) { CALL_SSID = ssid; } -void APRS_setDestination(char *call, int8_t ssid) { +void APRS_setDestination(char *call, int ssid) { memset(DST, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { @@ -88,7 +88,7 @@ void APRS_setDestination(char *call, int8_t ssid) { DST_SSID = ssid; } -void APRS_setPath1(char *call, int8_t ssid) { +void APRS_setPath1(char *call, int ssid) { memset(PATH1, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { @@ -98,7 +98,7 @@ void APRS_setPath1(char *call, int8_t ssid) { PATH1_SSID = ssid; } -void APRS_setPath2(char *call, int8_t ssid) { +void APRS_setPath2(char *call, int ssid) { memset(PATH2, 0, MAX_CALL_LENGTH); int i = 0; while (i < 6 && call[i] != 0) { @@ -108,7 +108,7 @@ void APRS_setPath2(char *call, int8_t ssid) { PATH2_SSID = ssid; } -void APRS_setMessageDestination(char *call, int8_t ssid) { +void APRS_setMessageDestination(char *call, int ssid) { memset(message_recip, 0, 6); int i = 0; while (i < 6 && call[i] != 0) { From d9797bb285fe4d95a6ba8959447186cedc12626b Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Sun, 10 Sep 2017 21:53:37 +1000 Subject: [PATCH 6/7] Added #defines for the MIC-E Standard Messages --- LibAPRS/LibAPRS.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/LibAPRS/LibAPRS.h b/LibAPRS/LibAPRS.h index 4881d84..d003940 100644 --- a/LibAPRS/LibAPRS.h +++ b/LibAPRS/LibAPRS.h @@ -8,6 +8,15 @@ #include "AFSK.h" #include "AX25.h" +#define MICE_STD_MSG_OFF_DUTY 0x07 +#define MICE_STD_MSG_EN_ROUTE 0x06 +#define MICE_STD_MSG_IN_SERVICE 0x05 +#define MICE_STD_MSG_RETURNING 0x04 +#define MICE_STD_MSG_COMMITTED 0x03 +#define MICE_STD_MSG_SPECIAL 0x02 +#define MICE_STD_MSG_PRIORITY 0x01 +#define MICE_STD_MSG_EMERGENCY 0x00 + void APRS_init(int reference, bool open_squelch); void APRS_poll(void); From 23b912827b6a40159e4d6229f43fceca575489f1 Mon Sep 17 00:00:00 2001 From: Tim Ahpee Date: Tue, 26 Sep 2017 20:57:50 +1000 Subject: [PATCH 7/7] Added dynamic switching to 300 baud TX --- LibAPRS/AFSK.cpp | 50 ++++++++++++++++++++++++--------------------- LibAPRS/AFSK.h | 37 +++++++++++++++++++-------------- LibAPRS/AX25.cpp | 1 + LibAPRS/LibAPRS.cpp | 17 +++++++++++++++ LibAPRS/LibAPRS.h | 3 +++ 5 files changed, 70 insertions(+), 38 deletions(-) diff --git a/LibAPRS/AFSK.cpp b/LibAPRS/AFSK.cpp index 47a7e7c..12e3a9f 100644 --- a/LibAPRS/AFSK.cpp +++ b/LibAPRS/AFSK.cpp @@ -30,7 +30,7 @@ void AFSK_hw_init(void) { AFSK_hw_refDetect(); - TCCR1A = 0; + TCCR1A = 0; TCCR1B = _BV(CS10) | _BV(WGM13) | _BV(WGM12); ICR1 = (((CPU_FREQ+FREQUENCY_CORRECTION)) / 9600) - 1; @@ -45,7 +45,7 @@ void AFSK_hw_init(void) { DIDR0 |= _BV(0); ADCSRB = _BV(ADTS2) | _BV(ADTS1) | - _BV(ADTS0); + _BV(ADTS0); ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE)| @@ -62,33 +62,37 @@ void AFSK_init(Afsk *afsk) { memset(afsk, 0, sizeof(*afsk)); AFSK_modem = afsk; // Set phase increment - afsk->phaseInc = MARK_INC; + afsk->dataRate = 1200; + afsk->phaseInc = MARK_INC_1200; // Initialise FIFO buffers fifo_init(&afsk->delayFifo, (uint8_t *)afsk->delayBuf, sizeof(afsk->delayBuf)); fifo_init(&afsk->rxFifo, afsk->rxBuf, sizeof(afsk->rxBuf)); fifo_init(&afsk->txFifo, afsk->txBuf, sizeof(afsk->txBuf)); // Fill delay FIFO with zeroes - for (int i = 0; idelayFifo, 0); } AFSK_hw_init(); +} +void AFSK_setDataRate(Afsk *afsk, uint16_t dataRate) { + afsk->dataRate = dataRate; } static void AFSK_txStart(Afsk *afsk) { if (!afsk->sending) { - afsk->phaseInc = MARK_INC; + afsk->phaseInc = afsk->dataRate == 1200 ? MARK_INC_1200 : MARK_INC_300; afsk->phaseAcc = 0; afsk->bitstuffCount = 0; afsk->sending = true; LED_TX_ON(); - afsk->preambleLength = DIV_ROUND(custom_preamble * BITRATE, 8000); + afsk->preambleLength = DIV_ROUND(custom_preamble * afsk->dataRate, 8000); AFSK_DAC_IRQ_START(); } ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - afsk->tailLength = DIV_ROUND(custom_tail * BITRATE, 8000); + afsk->tailLength = DIV_ROUND(custom_tail * afsk->dataRate, 8000); } } @@ -154,18 +158,18 @@ uint8_t AFSK_dac_isr(Afsk *afsk) { if (afsk->bitStuff && afsk->bitstuffCount >= BIT_STUFF_LEN) { afsk->bitstuffCount = 0; - afsk->phaseInc = SWITCH_TONE(afsk->phaseInc); + afsk->phaseInc = (afsk->dataRate == 1200) ? SWITCH_TONE_1200(afsk->phaseInc) : SWITCH_TONE_300(afsk->phaseInc); } else { if (afsk->currentOutputByte & afsk->txBit) { afsk->bitstuffCount++; } else { afsk->bitstuffCount = 0; - afsk->phaseInc = SWITCH_TONE(afsk->phaseInc); + afsk->phaseInc = (afsk->dataRate == 1200) ? SWITCH_TONE_1200(afsk->phaseInc) : SWITCH_TONE_300(afsk->phaseInc); } afsk->txBit <<= 1; } - afsk->sampleIndex = SAMPLESPERBIT; + afsk->sampleIndex = (afsk->dataRate == 1200) ? SAMPLESPERBIT_1200 : SAMPLESPERBIT_300; } afsk->phaseAcc += afsk->phaseInc; @@ -184,7 +188,7 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { // the left by one bit, to make room for the // next incoming bit hdlc->demodulatedBits <<= 1; - // And then put the newest bit from the + // And then put the newest bit from the // demodulator into the byte. hdlc->demodulatedBits |= bit ? 1 : 0; @@ -205,9 +209,9 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { } } else { // If the buffer is full, we have a problem - // and abort by setting the return value to + // and abort by setting the return value to // false and stopping the here. - + ret = false; hdlc->receiving = false; LED_RX_OFF(); @@ -255,7 +259,7 @@ static bool hdlcParse(Hdlc *hdlc, bool bit, FIFOBuffer *fifo) { // a control character. Therefore, if we detect such a // "stuffed bit", we simply ignore it and wait for the // next bit to come in. - // + // // We do the detection by applying an AND bit-mask to the // stream of demodulated bits. This mask is 00111111 (0x3f) // if the result of the operation is 00111110 (0x3e), we @@ -333,7 +337,7 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { afsk->iirX[1] = ((int8_t)fifo_pop(&afsk->delayFifo) * currentSample) >> 2; afsk->iirY[0] = afsk->iirY[1]; - + afsk->iirY[1] = afsk->iirX[0] + afsk->iirX[1] + (afsk->iirY[0] >> 1); // Chebyshev filter @@ -347,7 +351,7 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { fifo_push(&afsk->delayFifo, currentSample); // We need to check whether there is a signal transition. - // If there is, we can recalibrate the phase of our + // If there is, we can recalibrate the phase of our // sampler to stay in sync with the transmitter. A bit of // explanation is required to understand how this works. // Since we have PHASE_MAX/PHASE_BITS = 8 samples per bit, @@ -363,13 +367,13 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { // Past Future // 0000000011111111000000001111111100000000 // |________| - // || + // || // Window // // Every time we detect a signal transition, we adjust // where this window is positioned little. How much we // adjust it is defined by PHASE_INC. If our current phase - // phase counter value is less than half of PHASE_MAX (ie, + // phase counter value is less than half of PHASE_MAX (ie, // the window size) when a signal transition is detected, // add PHASE_INC to our phase counter, effectively moving // the window a little bit backward (to the left in the @@ -380,7 +384,7 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { // our timing to the transmitter, even if it's timing is // a little off compared to our own. if (SIGNAL_TRANSITIONED(afsk->sampledBits)) { - if (afsk->currentPhase < PHASE_THRESHOLD) { + if (afsk->currentPhase < PHASE_THRESHOLD_1200) { afsk->currentPhase += PHASE_INC; } else { afsk->currentPhase -= PHASE_INC; @@ -392,10 +396,10 @@ void AFSK_adc_isr(Afsk *afsk, int8_t currentSample) { // Check if we have reached the end of // our sampling window. - if (afsk->currentPhase >= PHASE_MAX) { + if (afsk->currentPhase >= PHASE_MAX_1200) { // If we have, wrap around our phase // counter by modulus - afsk->currentPhase %= PHASE_MAX; + afsk->currentPhase %= PHASE_MAX_1200; // Bitshift to make room for the next // bit in our stream of demodulated bits @@ -462,7 +466,7 @@ ISR(ADC_vect) { TIFR1 = _BV(ICF1); AFSK_adc_isr(AFSK_modem, ((int16_t)((ADC) >> 2) - 128)); if (hw_afsk_dac_isr) { - DAC_PORT = (AFSK_dac_isr(AFSK_modem) & 0xF0) | _BV(3); + DAC_PORT = (AFSK_dac_isr(AFSK_modem) & 0xF0) | _BV(3); } else { DAC_PORT = 128; } @@ -472,4 +476,4 @@ ISR(ADC_vect) { poll_timer = 0; APRS_poll(); } -} \ No newline at end of file +} diff --git a/LibAPRS/AFSK.h b/LibAPRS/AFSK.h index fe2ebad..5a46dbe 100644 --- a/LibAPRS/AFSK.h +++ b/LibAPRS/AFSK.h @@ -29,8 +29,9 @@ inline static uint8_t sinSample(uint16_t i) { return (i >= (SIN_LEN/2)) ? (255 - sine) : sine; } - -#define SWITCH_TONE(inc) (((inc) == MARK_INC) ? SPACE_INC : MARK_INC) +#define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor)) +#define SWITCH_TONE_300(inc) (((inc) == MARK_INC_300) ? SPACE_INC_300 : MARK_INC_300) +#define SWITCH_TONE_1200(inc) (((inc) == MARK_INC_1200) ? SPACE_INC_1200 : MARK_INC_1200) #define BITS_DIFFER(bits1, bits2) (((bits1)^(bits2)) & 0x01) #define DUAL_XOR(bits1, bits2) ((((bits1)^(bits2)) & 0x03) == 0x03) #define SIGNAL_TRANSITIONED(bits) DUAL_XOR((bits), (bits) >> 2) @@ -44,15 +45,23 @@ inline static uint8_t sinSample(uint16_t i) { #define CONFIG_AFSK_PREAMBLE_LEN 150UL #define CONFIG_AFSK_TRAILER_LEN 50UL #define SAMPLERATE 9600 -#define BITRATE 1200 -#define SAMPLESPERBIT (SAMPLERATE / BITRATE) +#define SAMPLESPERBIT_1200 (SAMPLERATE / 1200) +#define SAMPLESPERBIT_300 (SAMPLERATE / 300) #define BIT_STUFF_LEN 5 -#define MARK_FREQ 1200 -#define SPACE_FREQ 2200 +#define MARK_FREQ_300 1600 +#define SPACE_FREQ_300 1800 +#define MARK_FREQ_1200 1200 +#define SPACE_FREQ_1200 2200 #define PHASE_BITS 8 // How much to increment phase counter each sample #define PHASE_INC 1 // Nudge by an eigth of a sample each adjustment -#define PHASE_MAX (SAMPLESPERBIT * PHASE_BITS) // Resolution of our phase counter = 64 -#define PHASE_THRESHOLD (PHASE_MAX / 2) // Target transition point of our phase window +#define PHASE_MAX_300 (SAMPLESPERBIT_300 * PHASE_BITS) // Resolution of our phase counter = 64 +#define PHASE_MAX_1200 (SAMPLESPERBIT_1200 * PHASE_BITS) // Resolution of our phase counter = 64 +#define PHASE_THRESHOLD_300 (PHASE_MAX_300 / 2) // Target transition point of our phase window +#define PHASE_THRESHOLD_1200 (PHASE_MAX_1200 / 2) // Target transition point of our phase window +#define MARK_INC_300 (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ_300, CONFIG_AFSK_DAC_SAMPLERATE)) +#define SPACE_INC_300 (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ_300, CONFIG_AFSK_DAC_SAMPLERATE)) +#define MARK_INC_1200 (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ_1200, CONFIG_AFSK_DAC_SAMPLERATE)) +#define SPACE_INC_1200 (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ_1200, CONFIG_AFSK_DAC_SAMPLERATE)) typedef struct Hdlc @@ -85,13 +94,13 @@ typedef struct Afsk uint16_t phaseInc; // Phase increment per sample FIFOBuffer txFifo; // FIFO for transmit data - uint8_t txBuf[CONFIG_AFSK_TX_BUFLEN]; // Actial data storage for said FIFO + uint8_t txBuf[CONFIG_AFSK_TX_BUFLEN]; // Actual data storage for said FIFO volatile bool sending; // Set when modem is sending // Demodulation values FIFOBuffer delayFifo; // Delayed FIFO for frequency discrimination - int8_t delayBuf[SAMPLESPERBIT / 2 + 1]; // Actual data storage for said FIFO + int8_t delayBuf[SAMPLESPERBIT_300 / 2 + 1]; // Actual data storage for said FIFO FIFOBuffer rxFifo; // FIFO for received data uint8_t rxBuf[CONFIG_AFSK_RX_BUFLEN]; // Actual data storage for said FIFO @@ -100,16 +109,13 @@ typedef struct Afsk int16_t iirY[2]; // IIR Filter Y cells uint8_t sampledBits; // Bits sampled by the demodulator (at ADC speed) - int8_t currentPhase; // Current phase of the demodulator + int16_t currentPhase; // Current phase of the demodulator uint8_t actualBits; // Actual found bits at correct bitrate volatile int status; // Status of the modem, 0 means OK - + uint16_t dataRate; // Data rate for the modem } Afsk; -#define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor)) -#define MARK_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)MARK_FREQ, CONFIG_AFSK_DAC_SAMPLERATE)) -#define SPACE_INC (uint16_t)(DIV_ROUND(SIN_LEN * (uint32_t)SPACE_FREQ, CONFIG_AFSK_DAC_SAMPLERATE)) #define AFSK_DAC_IRQ_START() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = true; } while (0) #define AFSK_DAC_IRQ_STOP() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = false; } while (0) @@ -131,6 +137,7 @@ typedef struct Afsk void AFSK_init(Afsk *afsk); void AFSK_transmit(char *buffer, size_t size); void AFSK_poll(Afsk *afsk); +void AFSK_setDataRate(Afsk *afsk, uint16_t rate); void afsk_putchar(char c); int afsk_getchar(void); diff --git a/LibAPRS/AX25.cpp b/LibAPRS/AX25.cpp index 078b931..78f51e0 100644 --- a/LibAPRS/AX25.cpp +++ b/LibAPRS/AX25.cpp @@ -16,6 +16,7 @@ extern int LibAPRS_vref; extern bool LibAPRS_open_squelch; + void ax25_init(AX25Ctx *ctx, ax25_callback_t hook) { memset(ctx, 0, sizeof(*ctx)); ctx->hook = hook; diff --git a/LibAPRS/LibAPRS.cpp b/LibAPRS/LibAPRS.cpp index 0f3b0fc..73a835a 100644 --- a/LibAPRS/LibAPRS.cpp +++ b/LibAPRS/LibAPRS.cpp @@ -68,6 +68,14 @@ void APRS_poll(void) { ax25_poll(&AX25); } +void APRS_setDataRate300() { + AFSK_setDataRate(&modem, 300); +} + +void APRS_setDataRate1200() { + AFSK_setDataRate(&modem, 1200); +} + void APRS_setCallsign(char *call, int ssid) { memset(CALL, 0, MAX_CALL_LENGTH); int i = 0; @@ -134,6 +142,10 @@ void APRS_useAlternateSymbolTable(bool use) { } } +void APRS_setSymbolTable(char table) { + symbolTable = table; +} + void APRS_setSymbol(char sym) { symbol = sym; } @@ -371,6 +383,11 @@ uint8_t APRS_sendLoc_mice(void *_buffer, size_t length) { memcpy(path2.call, PATH2, 6); path2.ssid = PATH2_SSID; + for (int i=0; i<6; i++) { + Serial.print((char)DST[i]); + } + + path[0] = dst; path[1] = src; path[2] = path1; diff --git a/LibAPRS/LibAPRS.h b/LibAPRS/LibAPRS.h index d003940..aee87a4 100644 --- a/LibAPRS/LibAPRS.h +++ b/LibAPRS/LibAPRS.h @@ -30,6 +30,7 @@ void APRS_setPreamble(unsigned long pre); void APRS_setTail(unsigned long tail); void APRS_useAlternateSymbolTable(bool use); void APRS_setSymbol(char sym); +void APRS_setSymbolTable(char table); void APRS_setLat(char *lat); void APRS_setLon(char *lon); @@ -39,6 +40,8 @@ void APRS_setGain(int s); void APRS_setDirectivity(int s); void APRS_setSpeed(int s); void APRS_setCourse(int c); +void APRS_setDataRate300(); +void APRS_setDataRate1200(); void APRS_sendPkt(void *_buffer, size_t length); void APRS_sendLoc(void *_buffer, size_t length);