DHT / my_draw.h /
00bf2b0 5 years ago
1 contributor
216 lines | 6.498kb
#ifndef _MY_DRAW_H
#define _MY_DRAW_H

#define NOLOGO -1
#define SUN 0
#define SUN_CLOUD  1
#define CLOUD 2
#define RAIN 3
#define THUNDER 4


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
#ifdef ARDUINO_ESP8266_WEMOS_D1MINI
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
  #include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
  #include <Wire.h>
#endif

#define SCROLL_Y 10
#define T_FONTSZ 18

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 14, /* data=*/ 2);
//https://github.com/olikraus/u8g2/wiki/fntlistall
//https://github.com/olikraus/u8g2/wiki/setup_tutorial


void drawScreenReset() {
  pinMode(2, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}
void drawInit() {
  drawScreenReset();
  u8g2.begin();  
  u8g2.enableUTF8Print();
}
  
void drawWeatherSymbol(u8g2_uint_t x, u8g2_uint_t y, uint8_t symbol)
{
  // fonts used:
  // u8g2_font_open_iconic_embedded_6x_t
  // u8g2_font_open_iconic_weather_6x_t
  // encoding values, see: https://github.com/olikraus/u8g2/wiki/fntgrpiconic
  
  switch(symbol)
  {
    case SUN:
      u8g2.setFont(u8g2_font_open_iconic_weather_6x_t);
      u8g2.drawGlyph(x, y, 69);  
      break;
    case SUN_CLOUD:
      u8g2.setFont(u8g2_font_open_iconic_weather_6x_t);
      u8g2.drawGlyph(x, y, 65); 
      break;
    case CLOUD:
      u8g2.setFont(u8g2_font_open_iconic_weather_6x_t);
      u8g2.drawGlyph(x, y, 64); 
      break;
    case RAIN:
      u8g2.setFont(u8g2_font_open_iconic_weather_6x_t);
      u8g2.drawGlyph(x, y, 67); 
      break;
    case THUNDER:
      u8g2.setFont(u8g2_font_open_iconic_embedded_6x_t);
      u8g2.drawGlyph(x, y, 67);
      break;
    default:
      break;
  }
}

void drawWeather(uint8_t symbol, float degree, float humidity)
{
  drawWeatherSymbol(0, 48, symbol);
  //u8g2.setFont(u8g2_font_logisoso32_tf);
  u8g2.setFont(u8g2_font_logisoso18_tf );
  //u8g2.setCursor(48+3, 50);
  if(-1 != degree) {
    u8g2.setCursor(60, T_FONTSZ+SCROLL_Y+9);
    u8g2.print((float)((int)(10*degree+0.5))/10);
    u8g2.print("°");    // requires enableUTF8Print()
  }
  if(-1 != humidity) {
    u8g2.setCursor(0, 60);
    u8g2.print((float)((int)(10*humidity+0.5))/10);
    u8g2.print("%");    // requires enableUTF8Print()
  }
}

/*
  Draw a string with specified pixel offset. 
  The offset can be negative.
  Limitation: The monochrome font with 8 pixel per glyph
*/
void drawScrollString(int16_t offset, const char *s)
{
  static char buf[36];  // should for screen with up to 256 pixel width 
  size_t len;
  size_t char_offset = 0;
  u8g2_uint_t dx = 0;
  size_t visible = 0;
  

  u8g2.setDrawColor(0);   // clear the scrolling area
  u8g2.drawBox(0, 0, u8g2.getDisplayWidth()-1, SCROLL_Y);
  u8g2.setDrawColor(1);   // set the color for the text
    
  
  len = strlen(s);
  if ( offset < 0 )
  {
    char_offset = (-offset)/8;
    dx = offset + char_offset*8;
    if ( char_offset >= u8g2.getDisplayWidth()/8 )
      return;
    visible = u8g2.getDisplayWidth()/8-char_offset+1;
    strncpy(buf, s, visible);
    buf[visible] = '\0';
    u8g2.setFont(u8g2_font_8x13_mf);
    //u8g2.drawStr(char_offset*8-dx, 62, buf);
    u8g2.drawStr(char_offset*8-dx, SCROLL_Y, buf);
  }
  else
  {
    char_offset = offset / 8;
    if ( char_offset >= len )
      return; // nothing visible
    dx = offset - char_offset*8;
    visible = len - char_offset;
    if ( visible > u8g2.getDisplayWidth()/8+1 )
      visible = u8g2.getDisplayWidth()/8+1;
    strncpy(buf, s+char_offset, visible);
    buf[visible] = '\0';
    u8g2.setFont(u8g2_font_8x13_mf);
    u8g2.drawStr(-dx, SCROLL_Y, buf);
  }
}

void draw(const char *s, uint8_t symbol, float degree, float humidity)
{
  int16_t offset = -(int16_t)u8g2.getDisplayWidth();
  int16_t len = strlen(s);
  
  u8g2.clearBuffer();         // clear the internal memory
  drawWeather(symbol, degree, humidity);    // draw the icon and degree only once
  drawScrollString(0, s);
  u8g2.sendBuffer();        // transfer internal memory to the display
}
void displaySetStatus(int state) {}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
#elif ARDUINO_ESP8266_WEMOS_D1R1
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
#include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`

SSD1306  display(0x3c, 5, 4);//d1 d2

#define SCROLL_Y 10
#define T_FONTSZ 18

void drawScreenReset() {
}
void drawInit() {
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
}
void drawWeatherSymbol(uint8_t x, uint8_t y, uint8_t symbol) {}
void drawWeather(uint8_t symbol, float degree, float humidity) {
  display.setFont(ArialMT_Plain_24);
  if(-1 != degree) {
    char myString[8]="";
    sprintf(myString,"%2.1f°",(float)((int)(10*degree+0.5))/10);
    display.drawString(60, T_FONTSZ-4, myString );
  }
  if(-1 != humidity) {
    char myString[8]="";
    sprintf(myString,"%2.0f%%",(float)((int)(10*humidity+0.5))/10);
    display.drawString(0,2*T_FONTSZ, myString );
  }
}
void drawScrollString(int16_t offset, const char *s) {
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, offset, s );
}
void draw(const char *s, uint8_t symbol, float degree, float humidity) {
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.clear();
  drawWeather(NOLOGO, degree, humidity);
  drawScrollString(0, s);
  display.display();
}
void displaySetStatus(int state) {
  if( 0 == state ) {
    display.displayOff();
  } else {
    display.displayOn();
  }
}
#endif //





#endif //_MY_DRAW_H