2020-05-08 17:52:43 +00:00
|
|
|
#include <FT817_NBS.h>
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
FT817_NBS::FT817_NBS(SoftwareSerial *connection){
|
2020-05-08 17:52:43 +00:00
|
|
|
serial = connection;
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
unsigned long FT817_NBS::bcdToInt(uint8_t bcd){
|
2020-05-08 17:52:43 +00:00
|
|
|
int byte = (bcd & 0xF0) >> 4;
|
|
|
|
byte *= 10;
|
|
|
|
byte += bcd & 0x0F;
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
uint8_t FT817_NBS::intToBcd(unsigned long value){
|
2020-05-08 17:52:43 +00:00
|
|
|
unsigned long tens = value / 10;
|
|
|
|
value -= tens * 10;
|
|
|
|
unsigned long singles = value;
|
|
|
|
uint8_t returnValue = (tens & 0xF) << 4;
|
|
|
|
returnValue = returnValue | (singles & 0xF);
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
unsigned long FT817_NBS::getDevicer(int step){
|
2020-05-08 17:52:43 +00:00
|
|
|
switch (step) {
|
|
|
|
case 0:
|
|
|
|
return 1000000;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return 10000;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
return 100;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//Serial.println("SHIT"); //CAN'T HAPPEN
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
FT817_NBS::SignalMode FT817_NBS::getSignalMode(uint8_t numericValue){
|
2020-05-08 17:52:43 +00:00
|
|
|
switch (numericValue){
|
|
|
|
case 0x0:
|
|
|
|
return FT817_NBS::SignalMode::LSB;
|
|
|
|
break;
|
|
|
|
case 0x01:
|
|
|
|
return FT817_NBS::SignalMode::USB;
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
return FT817_NBS::SignalMode::CW;
|
|
|
|
break;
|
|
|
|
case 0x03:
|
|
|
|
return FT817_NBS::SignalMode::CWR;
|
|
|
|
break;
|
|
|
|
case 0x4:
|
|
|
|
return FT817_NBS::SignalMode::AM;
|
|
|
|
break;
|
|
|
|
case 0x6:
|
|
|
|
return FT817_NBS::SignalMode::WFM;
|
|
|
|
break;
|
|
|
|
case 0x8:
|
|
|
|
return FT817_NBS::SignalMode::FM;
|
|
|
|
break;
|
|
|
|
case 0xA:
|
|
|
|
return FT817_NBS::SignalMode::DIG;
|
|
|
|
break;
|
|
|
|
case 0xC:
|
|
|
|
return FT817_NBS::SignalMode::PKT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FT817_NBS::SignalMode::UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
void FT817_NBS::convertFromValueToBcd(uint8_t *buffer, unsigned long actualValue){
|
2020-05-08 17:52:43 +00:00
|
|
|
for(int partsLeft = 0;partsLeft < 4; partsLeft++){
|
|
|
|
unsigned long part = actualValue / getDevicer(partsLeft);
|
|
|
|
actualValue -= part * getDevicer(partsLeft);
|
|
|
|
uint8_t bcd = intToBcd(part);
|
|
|
|
buffer[partsLeft] = bcd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
unsigned long FT817_NBS::convertFromBcdToValue(uint8_t *buffer){
|
2020-05-08 17:52:43 +00:00
|
|
|
unsigned long actualValue = 0;
|
|
|
|
for(int partsleft = 0; partsleft < 4; partsleft++){
|
|
|
|
unsigned long multiplier = getDevicer(partsleft);
|
2020-05-08 21:12:15 +00:00
|
|
|
unsigned long partValue = bcdToInt(buffer[partsleft]);
|
2020-05-08 17:52:43 +00:00
|
|
|
actualValue += (partValue * multiplier);
|
|
|
|
}
|
|
|
|
return actualValue;
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
void FT817_NBS::clearAvailableBytes(){
|
|
|
|
if(serial->available() != 0){ //Emptying ft817.available()
|
|
|
|
uint8_t throwAwayBuffer[serial->available()];
|
|
|
|
serial->readBytes(&throwAwayBuffer[0], serial->available());
|
2020-05-08 17:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
void FT817_NBS::sendCommand(uint8_t *bytes, size_t len){
|
2020-05-08 17:52:43 +00:00
|
|
|
for (unsigned int i = 0; i < len; i++){
|
2020-05-08 21:12:15 +00:00
|
|
|
serial->write(bytes[i]);
|
2020-05-08 17:52:43 +00:00
|
|
|
//Serial.print(String(bytes[i], HEX));
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
void FT817_NBS::toggleAB(){
|
2020-05-08 17:52:43 +00:00
|
|
|
clearAvailableBytes();
|
|
|
|
uint8_t data[] = {0x0,0x0,0x0,0x0,0x81};
|
|
|
|
sendCommand(&data[0], sizeof(data));
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
FT817_NBS::Frequency FT817_NBS::getFrequency(){
|
2020-05-08 17:52:43 +00:00
|
|
|
clearAvailableBytes();
|
|
|
|
uint8_t data[] = {0x0,0x0,0x0,0x0,0x3};
|
|
|
|
sendCommand(&data[0], sizeof(data));
|
|
|
|
|
|
|
|
delay(10);
|
|
|
|
|
|
|
|
uint8_t buffer[5];
|
2020-05-08 21:12:15 +00:00
|
|
|
serial->readBytes(&buffer[0], sizeof(buffer));
|
2020-05-08 17:52:43 +00:00
|
|
|
FT817_NBS::Frequency frequency;
|
|
|
|
frequency.frequency = convertFromBcdToValue(&buffer[0]);
|
|
|
|
frequency.mode = getSignalMode(buffer[4]);
|
|
|
|
return frequency;
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:12:15 +00:00
|
|
|
void FT817_NBS::setFrequency(unsigned long frequency){
|
2020-05-08 17:52:43 +00:00
|
|
|
clearAvailableBytes();
|
|
|
|
uint8_t data[5];
|
|
|
|
convertFromValueToBcd(&data[0], frequency);
|
|
|
|
data[4] = 0x1;
|
|
|
|
sendCommand(&data[0], sizeof(data));
|
|
|
|
}
|