jp112

Ssd1306.h

Feb 15th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. //- -----------------------------------------------------------------------------------------------------------------------
  2. // AskSin++
  3. // 2020-02-15 der-pw Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
  4. // 2020-02-15 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
  5. //- -----------------------------------------------------------------------------------------------------------------------
  6.  
  7. #ifndef SSD1306_H_
  8. #define SSD1306_H_
  9.  
  10. #include <Adafruit_GFX.h>
  11. #include <Adafruit_SSD1306.h>
  12.  
  13.  
  14. template <uint8_t PEAK_THRESHOLD = 3, uint8_t ADDRESS = 0x3C>
  15. class DisplayType {
  16. private:
  17.   Adafruit_SSD1306 d;
  18.   uint8_t peakrssi;
  19.   uint8_t peakhold_val;
  20.   uint8_t peakhold_thresh_val;
  21. public:
  22.   DisplayType () : d(128, 32, &Wire), peakrssi(0), peakhold_val(100), peakhold_thresh_val(0)  {}
  23.   virtual ~DisplayType () {}
  24.  
  25.   void init() {
  26.     d.begin(SSD1306_SWITCHCAPVCC, ADDRESS);
  27.     delay(1000);
  28.     d.setRotation(3);
  29.     d.clearDisplay();
  30.     d.display();
  31.   }
  32.  
  33.   void printRSSI(uint8_t rssi, bool update=true) {
  34.     d.clearDisplay();
  35.     d.setTextSize(1);       // tiny display needs tiny font
  36.     d.setCursor(rssi < 100 ? 8 : 5, 0);
  37.  
  38.     d.setTextColor(WHITE, BLACK);               // print the rssi value
  39.     d.print("-");
  40.     d.print(rssi);
  41.     d.setCursor(8, 9);
  42.     d.print("dBm");
  43.  
  44.     if (update) d.display();
  45.   }
  46.  
  47.   void drawRSSIGraph(uint8_t rssi, bool update=true) {
  48.     d.drawRoundRect(9,  19,  15,  107, 2, 1);   // draw the rectangle
  49.  
  50.     uint8_t barh = map(rssi, 120, 20, 100, 0);  // map the rssi values to 0-100
  51.     if (barh > 100) barh = 100;
  52.     d.fillRect(12, 22+barh, 9, 101-barh, 1);    // an build the bar
  53.  
  54.     if ((rssi < peakrssi) || (peakhold_thresh_val > PEAK_THRESHOLD)) {   // peakhold
  55.       peakrssi = rssi;
  56.       peakhold_val = barh;
  57.       peakhold_thresh_val = 1;
  58.     } else {
  59.       peakhold_thresh_val++;
  60.     }
  61.     d.drawFastHLine(12, peakhold_val+22, 9, 1);                 // draw a peak hold line
  62.  
  63.     if (update) d.display();
  64.   }
  65.  
  66.   void printFull(uint8_t rssi) {
  67.     printRSSI(rssi, false);
  68.     drawRSSIGraph(rssi, false);
  69.     d.display();
  70.   }
  71. };
  72.  
  73. #endif /* SSD1306_H_ */
Advertisement
Add Comment
Please, Sign In to add comment