Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add More Features #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 122 additions & 12 deletions src/ft857d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
static FuncPtrVoid empty[1];
static FuncPtrVoidByte emptyB[3];
static FuncPtrVoidLong emptyL[1];
static FuncPtrVoidLongLong emptyLL[1];
static FuncPtrToggles toggle[1];
static FuncPtrByte fbyte[1];
static FuncPtrLong longf[1];
static FuncPtrByte fbyte[3];
static FuncPtrLong longf[2];

/*
* Contructor, simple constructor, it initiates the serial port in the
Expand All @@ -49,6 +50,7 @@ static FuncPtrLong longf[1];
void ft857d::begin() {
Serial.begin(9600, SERIAL_8N2);
Serial.flush();
serialPort = &Serial;
}

// Alternative initializer with a custom baudrate and mode
Expand All @@ -62,6 +64,15 @@ void ft857d::begin(long br, int mode) {
*/
Serial.begin(br, mode);
Serial.flush();
serialPort = &Serial;
}

/*
* Constructor, accept any Stream ie: HardwareSerial or SoftwareSerial or even BLE Serial
*
*/
void ft857d::begin(Stream *s) {
serialPort = s;
}

/*
Expand Down Expand Up @@ -103,7 +114,6 @@ void ft857d::addCATTXStatus(byte (*userFunc)(void)) {
emptyB[2] = userFunc;
}


/*
* Linking the function for the freq set, this one must link a function that
* accept a unisgned long as the freq in hz
Expand All @@ -114,6 +124,19 @@ void ft857d::addCATFSet(void (*userFunc)(long)) {
longf[0] = userFunc;
}

/*
* Linking the function for the Offset Freq, accept an unsigned long as the offset
* freq in hz.
*/

void ft857d::addCATOffsetFreq(void (*userFunc)(long)) {
longf[1] = userFunc;
}

void ft857d::addCATSetCTCSSTone(void (*userFunc)(long, long)) {
emptyLL[0] = userFunc;
}

/*
* Linking the function for the mode set, this expect a function that accepts a
* byte that is the mode in the way the CAT is defined
Expand All @@ -124,6 +147,20 @@ void ft857d::addCATMSet(void (*userFunc)(byte)) {
fbyte[0] = userFunc;
}

/*
* Linking the function for the offset direction, this expects a function that accepts a
* byte that is the encoded direction value.
*/

void ft857d::addCATOffsetDir(void (*userFunc)(byte)) {
fbyte[1] = userFunc;
}

// CTCSS/DCS Mode
void ft857d::addCATSQLMode(void (*userFunc)(byte)) {
fbyte[2] = userFunc;
}

/*
* Periodic call function, this must be placed inside the loop()
* to check for incomming serial commands.
Expand All @@ -135,44 +172,50 @@ void ft857d::check() {
if (!enabled) return;

// first check if we have at least 5 bytes waiting on the buffer
byte i = Serial.available();
byte i = serialPort->available();
if (i < 5) return;

// if you got here then there is at least 5 bytes waiting: get it.
for (i=0; i<5; i++) {
nullPad[i] = Serial.read();
nullPad[i] = serialPort->read();
}

// now chek for the command in the last byte
switch (nullPad[4]) {
case CAT_PTT_ON:
if (toggle[0]) {
toggle[0](true);
Serial.write(ACK);
serialPort->write(ACK);
}
break;
case CAT_PTT_OFF:
if (toggle[0]) {
toggle[0](false);
Serial.write(ACK);
serialPort->write(ACK);
}
break;
case CAT_VFO_AB:
if (empty[0]) {
empty[0]();
Serial.write(ACK);
serialPort->write(ACK);
}
break;
case CAT_FREQ_SET:
if (longf[0]) {
fset();
Serial.write(ACK);
serialPort->write(ACK);
}
break;
case CAT_MODE_SET:
if (fbyte[0]) {
fbyte[0](nullPad[0]);
Serial.write(ACK);
serialPort->write(ACK);
}
break;
case CAT_SQL_CMD:
if (fbyte[2]){
fbyte[2](nullPad[0]);
serialPort->write(ACK);
}
break;
case CAT_RX_FREQ_CMD:
Expand All @@ -187,8 +230,25 @@ void ft857d::check() {
case CAT_TX_DATA_CMD:
if (emptyB[2]) sendTxStatus(); // without ACK
break;
case CAT_RPTR_OFFSET_CMD:
if (fbyte[1]){
fbyte[1](nullPad[0]);
serialPort->write(ACK);
}
break;
case CAT_RPTR_FREQ_SET:
if (longf[1]) {
rptfset();
serialPort->write(ACK);
}
break;
case CAT_SQL_CTCSS_SET:
if (emptyLL[0]) {
rptctcssset();
serialPort->write(ACK);
}
default:
Serial.write(ACK);
serialPort->write(ACK);
break;
}
}
Expand All @@ -202,6 +262,40 @@ void ft857d::fset() {
longf[0](freq);
}

// Set the Offset frequency
void ft857d::rptfset() {
// reconstruct the freq from the bytes we got
from_bcd_be2();

// call the function with the freq as parameter
longf[1](freq);
}

// Set the Offset frequency
void ft857d::rptctcssset() {
// reconstruct the freq from the bytes we got
txtone = 0;
for (byte i=0; i<2; i++) {
txtone *= 10;
txtone += nullPad[i]>>4;
txtone *= 10;
txtone += nullPad[i] & 0x0f;
}
txtone *= 10;

rxtone = 0;
for (byte i=2; i<4; i++) {
rxtone *= 10;
rxtone += nullPad[i]>>4;
rxtone *= 10;
rxtone += nullPad[i] & 0x0f;
}
rxtone *= 10;

// call the function with the freq as parameter
emptyLL[0](txtone, rxtone);
}

// send the TX status
void ft857d::sendTxStatus() {
// just one byte with the format the CAT expect, see the exemple in the library
Expand Down Expand Up @@ -291,7 +385,7 @@ void ft857d::npadClear() {
// sent the data to the PC
void ft857d::sent(byte amount) {
// sent the nullpad content
for (byte i=0; i<amount; i++) Serial.write(nullPad[i]);
for (byte i=0; i<amount; i++) serialPort->write(nullPad[i]);
}

/*
Expand Down Expand Up @@ -336,3 +430,19 @@ void ft857d::from_bcd_be() {
freq *= 10;
freq += nullPad[4]>>4;
}

// put the freq in the freq var from the nullpad array
// Offset Freq does is only 4 bytes, not the 5 above.
// Probably needs a better function name?
void ft857d::from_bcd_be2() {
// {0x00,0x60,0x00,0x00,0xF9} sets offset to 0.600MHz
freq = 0;
for (byte i=0; i<4; i++) {
freq *= 10;
freq += nullPad[i]>>4;
freq *= 10;
freq += nullPad[i] & 0x0f;
}

freq *= 10;
}
62 changes: 37 additions & 25 deletions src/ft857d.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,33 @@
#define CAT_RX_DATA_CMD 0xE7
#define CAT_TX_DATA_CMD 0xF7
#define CAT_RX_FREQ_CMD 0x03
//~ #define CAT_RPTR_OFFSET_CMD 0x09
//~ #define CAT_RPTR_FREQ_SET 0xF9
//~ #define CAT_SQL_CMD 0x0A
#define CAT_RPTR_OFFSET_CMD 0x09
#define CAT_RPTR_FREQ_SET 0xF9
#define CAT_SQL_CMD 0x0A
// >>> Modes definition
//~ #define CAT_MODE_LSB 0x00
//~ #define CAT_MODE_USB 0x01
//~ #define CAT_MODE_CW 0x02
//~ #define CAT_MODE_CWR 0x03
//~ #define CAT_MODE_AM 0x04
//~ #define CAT_MODE_FM 0x08
//~ #define CAT_MODE_DIG 0x0A
//~ #define CAT_MODE_PKT 0x0C
//~ #define CAT_MODE_FMN 0x88
#define CAT_MODE_LSB 0x00
#define CAT_MODE_USB 0x01
#define CAT_MODE_CW 0x02
#define CAT_MODE_CWR 0x03
#define CAT_MODE_AM 0x04
#define CAT_MODE_FM 0x08
#define CAT_MODE_DIG 0x0A
#define CAT_MODE_PKT 0x0C
#define CAT_MODE_FMN 0x88
// >>> SQL modes
//~ #define CAT_SQL_DCS 0x0A
//~ #define CAT_SQL_DCS_DECD 0x0B
//~ #define CAT_SQL_DCS_ENCD 0x0C
//~ #define CAT_SQL_CTCSS 0x2A
//~ #define CAT_SQL_CTCSS_DECD 0x3A
//~ #define CAT_SQL_CTCSS_ENCD 0x4A
//~ #define CAT_SQL_OFF 0x8A
//~ #define CAT_SQL_CTCSS_SET 0x0B
//~ #define CAT_SQL_DCS_SET 0x0C
#define CAT_SQL_DCS 0x0A
#define CAT_SQL_DCS_DECD 0x0B
#define CAT_SQL_DCS_ENCD 0x0C
#define CAT_SQL_CTCSS 0x2A
#define CAT_SQL_CTCSS_DECD 0x3A
#define CAT_SQL_CTCSS_ENCD 0x4A
#define CAT_SQL_OFF 0x8A
#define CAT_SQL_CTCSS_SET 0x0B
#define CAT_SQL_DCS_SET 0x0C
// >>> RPT related
//~ #define CAT_RPTR_OFFSET_N 0x09
//~ #define CAT_RPTR_OFFSET_P 0x49
//~ #define CAT_RPTR_OFFSET_S 0x89
#define CAT_RPTR_OFFSET_N 0x09
#define CAT_RPTR_OFFSET_P 0x49
#define CAT_RPTR_OFFSET_S 0x89
// >>> HAMLIB specific ones
#define CAT_HAMLIB_EEPROM 0xBB

Expand All @@ -96,6 +96,7 @@ typedef byte (*FuncPtrVoidByte)(void);
typedef void (*FuncPtrToggles)(boolean);
typedef void (*FuncPtrByte)(byte);
typedef void (*FuncPtrLong)(long);
typedef void (*FuncPtrVoidLongLong)(long, long);

/*
* The class...
Expand All @@ -105,24 +106,33 @@ class ft857d {
// we have two kind of constructors here
void begin(); // default for the radio 9600 @ 8N2
void begin(long baudrate, int mode); // custom baudrate and mode
void begin(Stream *s); // Accept Stream Device like SoftwareSerial
void check(); // periodic check for serial commands
// the functions that links the lib with your code
void addCATPtt(void (*)(boolean));
void addCATAB(void (*)(void));
void addCATFSet(void (*)(long));
void addCATMSet(void (*)(byte));
void addCATOffsetDir(void (*)(byte));
void addCATSQLMode(void (*)(byte));
void addCATOffsetFreq(void (*)(long));
void addCATGetFreq(long (*)(void));
void addCATGetMode(byte (*)(void));
void addCATSMeter(byte (*)(void));
void addCATTXStatus(byte (*)(void));
void addCATSetCTCSSTone(void (*)(long, long));
boolean enabled = true;

private:
byte nullPad[5] = {0,0,0,0,0};
long freq = 0;
byte ACK = 0;
long txtone = 0;
long rxtone = 0;
Stream *serialPort = NULL;
void setFreq(void);
void from_bcd_be(void);
void from_bcd_be2(void);
void to_bcd_be(long);
void sendFreqMode(void);
void rxStatus(void);
Expand All @@ -131,7 +141,9 @@ class ft857d {
void npadClear(void);
void sendTxStatus(void);
void sent(byte);
void fset(void);
void fset();
void rptfset();
void rptctcssset();
};

#endif