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

週間天気予報 その3


前回、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

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;

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;
  Serial.begin(9600);
  delay(100);

  // 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");

  Serial.printf("[HTTP]begin..\n");
  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);
        }
        delay(1);
      }
      Serial.printf("[HTTP] Disconnected\n");
      dumpTag(description);

      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");
      tft.setTextSize(1);
      tft.setRotation(3);

      for(int i=2;i<description.ValueCount;i++) {
//        tft.printf("%s\n",description.TagValue[i]);
        strcpy(in,(char *)description.TagValue[i]);
        splitUTF8(in,"の",tdate,out);
        tft.printf("%s",tdate);
        strcpy(in,out);
        splitUTF8(in,"は",weater,out);
        strcpy(in,&out[3]);
        splitUTF8(in,"、",weater,out);
        tft.setTextColor("WHITE");
        tft.printf(" %s\n",weater);

        strcpy(in,&out[3]);
        splitUTF8(in,"で",temp,out);
        strcpy(in,temp);
        splitASCII(in,' ',temph,templ);
        strcpy(in,temph);
        removeUTF8(in,"は",temph);
        strcpy(in,templ);
        removeUTF8(in,"は",templ);
        tft.printf("%s%s\n",temph,templ);
        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() {
}

このスケッチを実行すると以下のように表示されます。


次回は表示形式を変えてみます。

続く....