acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

FSK4.cpp (2475B)

      1 #include "FSK4.h"
      2 #if !defined(RADIOLIB_EXCLUDE_FSK4)
      3 
      4 FSK4Client::FSK4Client(PhysicalLayer* phy) {
      5   _phy = phy;
      6   #if !defined(RADIOLIB_EXCLUDE_AFSK)
      7   _audio = nullptr;
      8   #endif
      9 }
     10 
     11 #if !defined(RADIOLIB_EXCLUDE_AFSK)
     12  FSK4Client::FSK4Client(AFSKClient* audio) {
     13    _phy = audio->_phy;
     14    _audio = audio;
     15  }
     16 #endif
     17 
     18 int16_t FSK4Client::begin(float base, uint32_t shift, uint16_t rate) {
     19   // save configuration
     20   _baseHz = base;
     21   _shiftHz = shift;
     22 
     23   // calculate duration of 1 bit
     24   _bitDuration = (uint32_t)1000000/rate;
     25 
     26   // calculate module carrier frequency resolution
     27   uint32_t step = round(_phy->getFreqStep());
     28 
     29   // check minimum shift value
     30   if(shift < step / 2) {
     31     return(RADIOLIB_ERR_INVALID_RTTY_SHIFT);
     32   }
     33 
     34   // round shift to multiples of frequency step size
     35   if(shift % step < (step / 2)) {
     36     _shift = shift / step;
     37   } else {
     38     _shift = (shift / step) + 1;
     39   }
     40 
     41   // Write resultant tones into arrays for quick lookup when modulating.
     42   _tones[0] = 0;
     43   _tones[1] = _shift;
     44   _tones[2] = _shift*2;
     45   _tones[3] = _shift*3;
     46 
     47   _tonesHz[0] = 0;
     48   _tonesHz[1] = _shiftHz;
     49   _tonesHz[2] = _shiftHz*2;
     50   _tonesHz[3] = _shiftHz*3;
     51 
     52   // calculate 24-bit frequency
     53   _base = (base * 1000000.0) / _phy->getFreqStep();
     54 
     55   // configure for direct mode
     56   return(_phy->startDirect());
     57 }
     58 
     59 void FSK4Client::idle() {
     60   // Idle at Tone 0.
     61   tone(0);
     62 }
     63 
     64 size_t FSK4Client::write(uint8_t* buff, size_t len) {
     65   size_t n = 0;
     66   for(size_t i = 0; i < len; i++) {
     67     n += FSK4Client::write(buff[i]);
     68   }
     69   FSK4Client::standby();
     70   return(n);
     71 }
     72 
     73 size_t FSK4Client::write(uint8_t b) {
     74   // send symbols MSB first
     75   for(uint8_t i = 0; i < 4; i++) {
     76     // Extract 4FSK symbol (2 bits)
     77     uint8_t symbol = (b & 0xC0) >> 6;
     78 
     79     // Modulate
     80     FSK4Client::tone(symbol);
     81 
     82     // Shift to next symbol
     83     b = b << 2;
     84   }
     85 
     86   return(1);
     87 }
     88 
     89 void FSK4Client::tone(uint8_t i) {
     90   Module* mod = _phy->getMod();
     91   uint32_t start = mod->micros();
     92   transmitDirect(_base + _tones[i], _baseHz + _tonesHz[i]);
     93   while(mod->micros() - start < _bitDuration) {
     94     mod->yield();
     95   }
     96 }
     97 
     98 int16_t FSK4Client::transmitDirect(uint32_t freq, uint32_t freqHz) {
     99   #if !defined(RADIOLIB_EXCLUDE_AFSK)
    100   if(_audio != nullptr) {
    101     return(_audio->tone(freqHz));
    102   }
    103   #endif
    104   return(_phy->transmitDirect(freq));
    105 }
    106 
    107 int16_t FSK4Client::standby() {
    108   #if !defined(RADIOLIB_EXCLUDE_AFSK)
    109   if(_audio != nullptr) {
    110     return(_audio->noTone());
    111   }
    112   #endif
    113   return(_phy->standby());
    114 }
    115 
    116 #endif