diff --git a/lib/FT817_NBS/FT817_NBS.cpp b/lib/FT817_NBS/FT817_NBS.cpp index 119b2f7..16bc0fe 100644 --- a/lib/FT817_NBS/FT817_NBS.cpp +++ b/lib/FT817_NBS/FT817_NBS.cpp @@ -1,19 +1,17 @@ #include -SoftwareSerial serial; - -FT817_NBS::FT817_NBS(SoftwareSerial connection){ +FT817_NBS::FT817_NBS(SoftwareSerial *connection){ serial = connection; } -unsigned long bcdToInt(uint8_t bcd){ +unsigned long FT817_NBS::bcdToInt(uint8_t bcd){ int byte = (bcd & 0xF0) >> 4; byte *= 10; byte += bcd & 0x0F; return byte; } -uint8_t intToBcd(unsigned long value){ +uint8_t FT817_NBS::intToBcd(unsigned long value){ unsigned long tens = value / 10; value -= tens * 10; unsigned long singles = value; @@ -22,7 +20,7 @@ uint8_t intToBcd(unsigned long value){ return returnValue; } -unsigned long getDevicer(int step){ +unsigned long FT817_NBS::getDevicer(int step){ switch (step) { case 0: return 1000000; @@ -43,7 +41,7 @@ unsigned long getDevicer(int step){ } } -FT817_NBS::SignalMode getSignalMode(uint8_t numericValue){ +FT817_NBS::SignalMode FT817_NBS::getSignalMode(uint8_t numericValue){ switch (numericValue){ case 0x0: return FT817_NBS::SignalMode::LSB; @@ -78,7 +76,7 @@ FT817_NBS::SignalMode getSignalMode(uint8_t numericValue){ } } -void convertFromValueToBcd(uint8_t *buffer, unsigned long actualValue){ +void FT817_NBS::convertFromValueToBcd(uint8_t *buffer, unsigned long actualValue){ for(int partsLeft = 0;partsLeft < 4; partsLeft++){ unsigned long part = actualValue / getDevicer(partsLeft); actualValue -= part * getDevicer(partsLeft); @@ -87,38 +85,38 @@ void convertFromValueToBcd(uint8_t *buffer, unsigned long actualValue){ } } -unsigned long convertFromBcdToValue(uint8_t *buffer){ +unsigned long FT817_NBS::convertFromBcdToValue(uint8_t *buffer){ unsigned long actualValue = 0; for(int partsleft = 0; partsleft < 4; partsleft++){ unsigned long multiplier = getDevicer(partsleft); - unsigned long partValue = bcdToInt(buffer[0]); + unsigned long partValue = bcdToInt(buffer[partsleft]); actualValue += (partValue * multiplier); } return actualValue; } -void clearAvailableBytes(){ - if(serial.available() != 0){ //Emptying ft817.available() - uint8_t throwAwayBuffer[serial.available()]; - serial.readBytes(&throwAwayBuffer[0], serial.available()); +void FT817_NBS::clearAvailableBytes(){ + if(serial->available() != 0){ //Emptying ft817.available() + uint8_t throwAwayBuffer[serial->available()]; + serial->readBytes(&throwAwayBuffer[0], serial->available()); } } -void sendCommand(uint8_t *bytes, size_t len){ +void FT817_NBS::sendCommand(uint8_t *bytes, size_t len){ for (unsigned int i = 0; i < len; i++){ - serial.write(bytes[i]); + serial->write(bytes[i]); //Serial.print(String(bytes[i], HEX)); delay(1); } } -void toggleAB(){ +void FT817_NBS::toggleAB(){ clearAvailableBytes(); uint8_t data[] = {0x0,0x0,0x0,0x0,0x81}; sendCommand(&data[0], sizeof(data)); } -FT817_NBS::Frequency getFrequency(){ +FT817_NBS::Frequency FT817_NBS::getFrequency(){ clearAvailableBytes(); uint8_t data[] = {0x0,0x0,0x0,0x0,0x3}; sendCommand(&data[0], sizeof(data)); @@ -126,14 +124,14 @@ FT817_NBS::Frequency getFrequency(){ delay(10); uint8_t buffer[5]; - serial.readBytes(&buffer[0], sizeof(buffer)); + serial->readBytes(&buffer[0], sizeof(buffer)); FT817_NBS::Frequency frequency; frequency.frequency = convertFromBcdToValue(&buffer[0]); frequency.mode = getSignalMode(buffer[4]); return frequency; } -void setFrequency(unsigned long frequency){ +void FT817_NBS::setFrequency(unsigned long frequency){ clearAvailableBytes(); uint8_t data[5]; convertFromValueToBcd(&data[0], frequency); diff --git a/lib/FT817_NBS/FT817_NBS.h b/lib/FT817_NBS/FT817_NBS.h index 44a7cda..73ea31d 100644 --- a/lib/FT817_NBS/FT817_NBS.h +++ b/lib/FT817_NBS/FT817_NBS.h @@ -9,17 +9,19 @@ public: enum SignalMode {LSB = 0x0, USB = 0x1, CW = 0x2, CWR = 0x3, AM = 0x4, WFM = 0x6, FM = 0x8, DIG = 0xA, PKT = 0xC, UNKNOWN}; private: + SoftwareSerial *serial; unsigned long bcdToInt(uint8_t bcd); unsigned long getDevicer(int step); uint8_t intToBcd(unsigned long value); void convertFromValueToBcd(uint8_t *buffer, unsigned long actualValue); unsigned long convertFromBcdToValue(uint8_t *buffer); void clearAvailableBytes(); - void sendCommand(); + void sendCommand(uint8_t *data, size_t len); FT817_NBS::SignalMode getSignalMode(uint8_t numericValue); public: - FT817_NBS(SoftwareSerial serial); + FT817_NBS(SoftwareSerial *connection); + struct Frequency { unsigned long frequency; FT817_NBS::SignalMode mode; diff --git a/src/main.cpp b/src/main.cpp index 5c27019..6aa90e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,15 +6,13 @@ #define SWITCH_AB 5 SoftwareSerial radio = SoftwareSerial(2, 3); -FT817_NBS ft817(radio); +FT817_NBS ft817(&radio); void setFreq(); - void setup() { // put your setup code here, to run once: radio.begin(9600); - ft817 = FT817_NBS(radio); Serial.begin(9600); pinMode(SET_FREQ, INPUT_PULLUP); pinMode(SWITCH_AB, INPUT_PULLUP); @@ -32,6 +30,7 @@ void loop() { } else if(digitalRead(SET_FREQ) != LOW){ pressBlockSetFreq = false; + delay(20); } if (digitalRead(SWITCH_AB) == LOW && !pressBlockSwitchAB){ @@ -41,10 +40,8 @@ void loop() { } else if(digitalRead(SWITCH_AB) != LOW){ pressBlockSwitchAB = false; + delay(20); } - //String freq = Serial.readString(); - - } void setFreq(){