Advertisement
jp112

FreqTestSTM32

Sep 3rd, 2019
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //- -----------------------------------------------------------------------------------------------------------------------
  2. // AskSin++
  3. // 2018-08-09 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
  4. //- -----------------------------------------------------------------------------------------------------------------------
  5.  
  6. // define this to read the device id, serial and device type from bootloader section
  7. // #define USE_OTA_BOOTLOADER
  8.  
  9. #define STORAGEDRIVER at24cX<0x50,128,32>
  10. #define TICKS_PER_SECOND 500UL
  11. #define USE_HW_SERIAL
  12.  
  13. #include <SPI.h>    // when we include SPI.h - we can use LibSPI class
  14. #include <Wire.h>
  15. #include <EEPROM.h> // the EEPROM library contains Flash Access Methods
  16. #include <AskSinPP.h>
  17.  
  18. #include <Dimmer.h>
  19.  
  20.  
  21. #define CONFIG_BUTTON_PIN PB8
  22. using namespace as;
  23.  
  24. // define all device properties
  25. const struct DeviceInfo PROGMEM devinfo = {
  26.     {0x01,0x01,0x01},       // Device ID
  27.     "FreqTest00",           // Device Serial
  28.     {0x00,0x00},            // Device Model
  29.     0x10,                   // Firmware Version
  30.     as::DeviceType::Sensor, // Device Type
  31.     {0x00,0x00}             // Info Bytes
  32. };
  33.  
  34. /**
  35.  * Configure the used hardware
  36.  */
  37. typedef LibSPI<PA4> RadioSPI;
  38. typedef AskSin<StatusLed<LED_BUILTIN>,NoBattery,Radio<RadioSPI,PB0> > HalType;
  39.  
  40. #define STARTFREQ 0x656A             // frequency we start scanning
  41. #define MINFREQ (STARTFREQ - 0x300)  // frequency we abort scanning
  42. #define SEARCHSTEP 0x50              // step with during search
  43. #define BOUNDSTEP 0x10               // step width during upper/lower bound analysis
  44.  
  45.  
  46. #define ACTIVE_PING
  47. HMID PING_FROM(0x47,0xEE,0x1D);      // from address for status message e.g. switch
  48. HMID PING_TO(0x4A,0x80,0xE8);        // to address for status message / central / CCU
  49. #ifdef ACTIVE_PING
  50.   #define SCANTIME seconds2ticks(5)  // maximal time to wait for a valid message
  51. #else
  52.   #define SCANTIME seconds2ticks(60) // maximal time to wait for a valid message
  53. #endif
  54.  
  55.  
  56.  
  57. class TestDevice : public Device<HalType,DefList0>, Alarm {
  58.   DefList0 l0;
  59.   uint16_t freq, start, end;
  60.   uint8_t received, rssi;
  61. public:
  62.   enum SearchMode { Search, Up, Down, Done };
  63.   SearchMode mode;
  64.   HMID id;
  65.  
  66.   typedef Device<HalType,DefList0> BaseDevice;
  67.   TestDevice (const DeviceInfo& i,uint16_t addr) : BaseDevice(i,addr,l0,0), Alarm(0), l0(addr),
  68.       freq(STARTFREQ), start(STARTFREQ), end(STARTFREQ),
  69.       received(0), rssi(0), mode(Search)  {}
  70.   virtual ~TestDevice () {}
  71.  
  72.   virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
  73.     DPRINT("  ");DDEC(received);DPRINT("/");DDECLN(rssi);
  74.  
  75.     if( mode == Search ) {
  76.       if( received > 0 ) {
  77.         start = end = freq;
  78.         mode = Up; // start find upper bound
  79.         DPRINTLN("Search for upper bound");
  80.         setFreq(freq + BOUNDSTEP);
  81.       }
  82.       else {
  83.         if( freq < MINFREQ ) mode = Done;
  84.         if( freq <= STARTFREQ ) setFreq(STARTFREQ + (STARTFREQ-freq) + SEARCHSTEP);
  85.         else setFreq(STARTFREQ - (freq-STARTFREQ));
  86.       }
  87.     }
  88.     else if(mode == Up) {
  89.       if( received > 0 ) {
  90.         end = freq;
  91.         setFreq(end + BOUNDSTEP);
  92.       }
  93.       else {
  94.         mode = Down; // start find lower bound
  95.         DPRINTLN("Search for lower bound");
  96.         setFreq(start - BOUNDSTEP);
  97.       }
  98.     }
  99.     else if(mode == Down) {
  100.       if( received > 0 ) {
  101.         start = freq;
  102.         setFreq(start - BOUNDSTEP);
  103.       }
  104.       else {
  105.         mode = Done;
  106.       }
  107.     }
  108.     if( mode == Done ) {
  109.       DPRINT("\nDone: 0x21");DHEX(start);DPRINT(" - 0x21");DHEXLN(end);
  110.       if( start == end && start == STARTFREQ ) {
  111.         DPRINT("Could not receive any message");
  112.       }
  113.       else {
  114.         freq = start+((end - start)/2);
  115.         DPRINT("Calculated Freq: 0x21");DHEX((uint8_t)(freq>>8));DHEXLN((uint8_t)(freq&0xff));
  116.  
  117.         // store frequency
  118.         DPRINT("Store into config area: ");DHEX((uint8_t)(freq>>8));DHEXLN((uint8_t)(freq&0xff));
  119.         StorageConfig sc = getConfigArea();
  120.         sc.clear();
  121.         sc.setByte(CONFIG_FREQ1, freq>>8);
  122.         sc.setByte(CONFIG_FREQ2, freq&0xff);
  123.         sc.validate();
  124.  
  125.         //activity().savePower<Sleep<> >(this->getHal());
  126.       }
  127.     }
  128.   }
  129.  
  130.   virtual bool process(Message& msg) {
  131.     msg.from().dump(); DPRINT(".");
  132.     rssi = max(rssi,radio().rssi());
  133.     received++;
  134.     if( received > 0 ) {
  135.       trigger(sysclock);
  136.     }
  137.     return true;
  138.   }
  139.  
  140.   bool init (HalType& hal) {
  141.     this->setHal(hal);
  142.     this->getDeviceID(id);
  143.     hal.init(id);
  144.  
  145.     DPRINTLN("Start searching ...");
  146.     setFreq(STARTFREQ);
  147.     return false;
  148.   }
  149.  
  150.   void setFreq (uint16_t current) {
  151.     sysclock.cancel(*this);
  152.     freq = current;
  153.     rssi=0;
  154.     received=0;
  155.     DPRINT("Freq 0x21");DHEX(freq);DPRINT(": ");
  156.     this->radio().initReg(CC1101_FREQ2, 0x21);
  157.     this->radio().initReg(CC1101_FREQ1, freq >> 8);
  158.     this->radio().initReg(CC1101_FREQ0, freq & 0xff);
  159.     set(SCANTIME);
  160.     sysclock.add(*this);
  161.   }
  162. };
  163.  
  164. HalType hal;
  165. TestDevice sdev(devinfo,0x20);
  166.  
  167. class InfoSender : public Alarm {
  168.   class channel {
  169.   public:
  170.     uint8_t number() const {return 1; }
  171.     uint8_t status() const {return 0; }
  172.     uint8_t flags()  const {return 0; }
  173.     void patchStatus(__attribute__ ((unused)) Message& msg) {}
  174.     void changed(__attribute__ ((unused)) bool b) {}
  175.   };
  176.   uint8_t cnt;
  177.   channel ch;
  178. public:
  179.   InfoSender () : Alarm(0), cnt(0) {}
  180.   virtual ~InfoSender () {}
  181.  
  182.   virtual void trigger (AlarmClock& clock) {
  183.     InfoActuatorStatusMsg msg;
  184.     msg.init(cnt++, ch, hal.radio.rssi());
  185.     msg.to(PING_TO);
  186.     msg.from(PING_FROM);
  187.     msg.ackRequired();
  188.     msg.setRpten();
  189. #ifdef ACTIVE_PING
  190.     sdev.radio().write(msg,msg.burstRequired());
  191. #endif
  192.     sdev.led().ledOn(millis2ticks(100), 0);
  193.     if( sdev.mode != TestDevice::SearchMode::Done ) {
  194.       set(seconds2ticks(1));
  195.       clock.add(*this);
  196.     }
  197.   }
  198. } info;
  199.  
  200. void setup () {
  201.   delay(3000);
  202.   DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
  203.   sdev.init(hal);
  204.   // start sender
  205.   info.trigger(sysclock);
  206. }
  207.  
  208. void loop() {
  209.   sdev.pollRadio();
  210.   hal.runready();
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement