ESP-WROOM-02でTFTに漢字を表示する

週間天気予報 その4


前回、livedoorの Weather HacksのRSSフィードのページから必要な情報を抜き出して、
TFTに表示する方法を紹介しました。



そこで、今回は表示スタイルを変更してみます。
/*
 *  Simple HTTP get webclient test
 */
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#include <Adafruit_GFX.h>    // https://github.com/adafruit/Adafruit-GFX-Library
#include <Fontx.h>              // https://github.com/h-nari/Fontx
#include <Humblesoft_GFX.h>     // https://github.com/h-nari/Humblesoft_GFX
#include <Humblesoft_ILI9341.h> // https://github.com/h-nari/Humblesoft_ILI9341

#define TAGMAX 10
#define LENMAX 128

//#define LAYOUT 1 // 横書き 小さい文字  7日分
#define LAYOUT 2 // 横書き 大きい文字 2日分
//#define LAYOUT 3 // 縦書き 小さい文字  7日分
//#define LAYOUT 4 // 縦書き 大きい文字  3日分

IMPORT_BIN("/fontx/ILGH16XB.FNT", ILH16XB); //16ドット半角ゴシックフォント
IMPORT_BIN("/fontx/ILGZ16XB.FNT", ILZ16XB); //16ドット全角ゴシックフォント
//IMPORT_BIN("/fontx/ILMH16XB.FNT", ILH16XB); //16ドット半角明朝フォント
//IMPORT_BIN("/fontx/ILMZ16XB.FNT", ILZ16XB); //16ドット全角明朝フォント
extern const uint8_t ILH16XB[], ILZ16XB[];

Humblesoft_ILI9341 tft = Humblesoft_ILI9341();
RomFontx fontx(ILH16XB,ILZ16XB);

const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";

HTTPClient http;

struct TAG {
  uint8_t StartName;
  uint8_t StartValue;
  uint8_t TagPos;
  unsigned char TagName[LENMAX];
  unsigned char TagCurrent[LENMAX];
  uint8_t ValueCount;
  unsigned char TagValue[TAGMAX][LENMAX];
};

struct TAG description;
struct TAG title;

void httpPrint(int len, uint8_t *buff) {
  char ch[2];
  ch[1] = 0;
  for(int i=0; i<len; i++) {
    ch[0] = buff[i];
    if (isAlphaNumeric(ch[0])) {
      Serial.printf("%c",ch[0]);
    } else if (strstr("\"!#$%&'()=-<> ;:/,.@[]+*",ch)) {
      Serial.printf("%c",ch[0]);
    } else if (ch[0] == 0x0a) {
      Serial.printf("\n");
    } else {
//      Serial.printf("0x%02x",ch[0]);
      Serial.printf("*");
    }
  }
}

void initTag(struct TAG* hoge,char* name) {
   hoge->StartName=0;
   hoge->StartValue=0;
   hoge->TagPos=0;
   strcpy((char *)hoge->TagName,name);
   memset(hoge->TagCurrent,0,sizeof(hoge->TagCurrent));
   hoge->ValueCount=0;
   memset(hoge->TagValue,0,sizeof(hoge->TagValue));
}

void addTagCurrent(struct TAG* hoge, char ch) {
   int pos;
   pos=hoge->TagPos;
   if (pos == LENMAX-1) return;
   hoge->TagCurrent[pos]=ch;
   hoge->TagCurrent[++pos]=0;
   (hoge->TagPos)++;
}

void startTag(struct TAG* hoge) {
   hoge->StartName=1;
   hoge->TagPos=0;
   if (hoge->StartValue) {
     hoge->StartValue=0;
     (hoge->ValueCount)++;
   }
}

void endTag(struct TAG* hoge) {
   hoge->StartName=0;
   hoge->TagPos=0;
   if (strcmp ((char *)hoge->TagName, (char *)hoge->TagCurrent) == 0) {
     if (hoge->ValueCount == TAGMAX) return;
     hoge->StartValue=1;
   }
}

void addTagValue(struct TAG* hoge, char ch) {
   int pos;
   int count;
   pos=hoge->TagPos;
   count=hoge->ValueCount;
   if (pos == LENMAX-1) return;
   hoge->TagValue[count][pos]=ch;
   hoge->TagValue[count][++pos]=0;
   (hoge->TagPos)++;
}

void dumpTag(struct TAG hoge) {
   int i;
   Serial.printf("\nhoge.TagName=%s\n",hoge.TagName);
   Serial.printf("hoge.TagCurrent=%s\n",hoge.TagCurrent);
   Serial.printf("hoge.ValueCount=%d\n",hoge.ValueCount);
   for(i=0;i<hoge.ValueCount;i++){
   Serial.printf("hoge.TagValue[%d]=%s\n",i,hoge.TagValue[i]);
   }
}


void httpParse(int len, uint8_t *buff, struct TAG *hoge) {
   int i;
   char ch;
   for(i=0;i<len;i++) {
     ch=buff[i];
     if (ch == '<') {
       startTag(hoge);
     } else if (ch == '>') {
       endTag(hoge);
     } else {
       if (hoge->StartName) addTagCurrent(hoge,ch);
       if (hoge->StartValue) addTagValue(hoge,ch);
     }
   }
}

/*
 * 指定した漢字で文字列を分解する
 */
void splitUTF8(char *in, char* utf8, char* out1, char* out2) {
  int pos=0;
  int len=strlen(in);
  char wk[4]={0};
//  Serial.printf("len=%d\n",len);
  out1[pos]=0;
  out2[0]=0;
  for(int i=0;i<len;) {
//    Serial.printf("%d>%02x\n",i,in[i]);
    if (in[i] < 0x80) {
      out1[pos++]=in[i];
      out1[pos]=0;
      i++;
    } else {
      wk[0]=in[i];
      wk[1]=in[i+1];
      wk[2]=in[i+2];
//      Serial.printf("wk=%02x%02x%02x utf8=%02x%02x%02x\n",
//      wk[0],wk[1],wk[2],utf8[0],utf8[1],utf8[2]);
      if (strcmp(wk,utf8) == 0) {
//        Serial.printf("len-i=%d\n",len-i);
        memcpy(out2,&in[i],len-i);
        out2[len-i]=0;
        return;
      } else {
        out1[pos++]=wk[0];
        out1[pos++]=wk[1];
        out1[pos++]=wk[2];
        out1[pos]=0;
      }
//      delay(10);
      i=i+3;
    }
  }
}

/*
 * 指定した漢字を削除する
 */
void removeUTF8(char *in, char* utf8, char* out1) {
  int pos=0;
  int len=strlen(in);
  char wk[4]={0};
//  Serial.printf("len=%d\n",len);
  out1[0]=0;
  for(int i=0;i<len;) {
//    Serial.printf("%d>%02x\n",i,in[i]);
    if (in[i] < 0x80) {
      out1[pos++]=in[i];
      out1[pos]=0;
      i++;
    } else {
      wk[0]=in[i];
      wk[1]=in[i+1];
      wk[2]=in[i+2];
//      Serial.printf("wk=%02x%02x%02x utf8=%02x%02x%02x\n",
//      wk[0],wk[1],wk[2],utf8[0],utf8[1],utf8[2]);
      if (strcmp(wk,utf8) != 0) {
        out1[pos++]=wk[0];
        out1[pos++]=wk[1];
        out1[pos++]=wk[2];
        out1[pos]=0;
      }
//      delay(10);
      i=i+3;
    }
  }
}

/*
 * 指定した文字で文字列を分解する
 */
void splitASCII(char *in, char ascii, char* out1, char* out2) {
  int pos=0;
  int len=strlen(in);
//  Serial.printf("len=%d\n",len);
  out1[pos]=0;
  out2[0]=0;
  for(int i=0;i<len;) {
//    Serial.printf("%d>%02x %02x\n",i,in[i],ascii);
    if (in[i] < 0x80) {
      if (in[i] == ascii) {
        memcpy(out2,&in[i],len-i);
        out2[len-i]=0;
        return;
      }
      out1[pos++]=in[i];
      out1[pos]=0;
      i++;
    } else {
      out1[pos++]=in[i];
      out1[pos++]=in[i+1];
      out1[pos++]=in[i+2];
      out1[pos]=0;
      i=i+3;
    }
  }
}

void setup() {
  int httpCode;
  delay(1000);Serial.begin(9600);

  // start ttf
  tft.begin();

  // start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Wait for WiFi...");
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  initTag(&description,"description");
  initTag(&title,"title");

  Serial.printf("[HTTP]begin..\n");
  //http.begin("http://weather.livedoor.com/forecast/rss/area/230010.xml");
  http.begin("weather.livedoor.com", 80, "/forecast/rss/area/230010.xml");

  httpCode = http.GET();
  if(httpCode > 0){
    Serial.printf("[HTTP] GET ... code: %d\n",httpCode);
    if(httpCode == HTTP_CODE_OK) {
      Serial.printf("[HTTP] OK\n");
      int len = http.getSize();
      uint8_t buff[128] = {0};
      WiFiClient *stream = http.getStreamPtr();
      while(http.connected() && (len > 0 || len == -1)){
        size_t size = stream->available();
        if(size){
          if(size > sizeof buff) size = sizeof buff;
          int c = stream->readBytes(buff,size);
//          httpPrint(c,buff);
          httpParse(c,buff,&description);
          httpParse(c,buff,&title);
        }
        delay(1);
      }
      Serial.printf("[HTTP] Disconnected\n");
      dumpTag(description);
      dumpTag(title);

      char address1[32];
      char address2[32];
      char tdate[32];
      char weater[32];
      char temp[64];
      char temph[32];
      char templ[32];
      char in[LENMAX];
      char out[LENMAX];

      tft.setCursor(0, 0);
      tft.setFont(&fontx);
      tft.fillScreen("BLACK");

      int loopMax;
#if defined(LAYOUT) && LAYOUT == 1
      tft.setTextSize(1);
      tft.setRotation(3);
      loopMax=description.ValueCount;
#endif

#if defined(LAYOUT) && LAYOUT == 2
      tft.setTextSize(2);
      tft.setRotation(3);
      loopMax=4;
#endif

#if defined(LAYOUT) && LAYOUT == 3
      tft.setTextSize(1);
      tft.setRotation(4);
      loopMax=description.ValueCount;
#endif

#if defined(LAYOUT) && LAYOUT == 4
      tft.setTextSize(2);
      tft.setRotation(4);
      loopMax=5;
#endif

#if defined(LAYOUT) && LAYOUT > 2
      strcpy(in,(char *)title.TagValue[0]);
      splitASCII(in,'-',address1,out);
      strcpy(in,&out[1]);
      splitASCII(in,'-',address2,out);
//      tft.printf("%s\n",title.TagValue[0]);
      tft.printf("%s%s\n\n",address1,address2);
#endif

#if 0
      for(int i=2;i<description.ValueCount;i++) {
        tft.printf("%s\n",description.TagValue[i]);
      }
#endif

      for(int i=2;i<loopMax;i++) {
//        tft.printf("%s\n",description.TagValue[i]);
        strcpy(in,(char *)description.TagValue[i]);
        splitUTF8(in,"の",tdate,out);
        tft.setTextColor("WHITE");
//        if (strstr(tdate,"(土)") != 0) tft.setTextColor("BLUE");
        if (strstr(tdate,"(土)") != 0) tft.setTextColor("CYAN");
        if (strstr(tdate,"(日)") != 0) tft.setTextColor("RED");
        tft.printf("%s",tdate);
#if defined(LAYOUT) && LAYOUT == 2
        tft.printf("\n");
#endif
#if defined(LAYOUT) && LAYOUT == 4
        tft.printf("\n");
#endif
        strcpy(in,out);
        splitUTF8(in,"は",weater,out);
//        tft.printf("%s\n",weater);
        strcpy(in,&out[3]);
        splitUTF8(in,"、",weater,out);
        tft.setTextColor("WHITE");
#if defined(LAYOUT) && LAYOUT == 2
        tft.printf(" %s  ",weater);
#else
        tft.printf(" %s\n",weater);
#endif

#if defined(LAYOUT) && LAYOUT < 4
        strcpy(in,&out[3]);
        splitUTF8(in,"で",temp,out);
//        tft.printf("%s\n",temp);
        strcpy(in,temp);
        splitASCII(in,' ',temph,templ);
//        tft.printf("%s:%s\n",temph,templ);
        strcpy(in,temph);
        removeUTF8(in,"は",temph);
        strcpy(in,templ);
        removeUTF8(in,"は",templ);
        tft.printf("%s%s\n",temph,templ);
#endif
        delay(10);
      }

    } else {
      Serial.printf("[HTTP] Server response Not OK\n");
    }
  } else {
    Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  }
  http.end();
}

void loop() {
}

LAYOUTの値に応じて表示は以下の様になります。








表示についてはこれで完成です。

表示内容を最新にリフレッシュする方法として2つ方法が有ります。
@タッチパネルをタッチしたら最新の情報に書き換える
A決まった時間に最新情報に書き換える
次回は@を紹介します。

続く
....