M5SatmpS3、8x8 Puzzle Unit (Unit Puzzle ?)スクロール時計で遊ぶ。SNTP



参考
----


https://github.com/Tamakichi/Arduino-misakiUTF16


いろいろ、注意
----

- 過去ログを見よ!!!
- M5Stack by M5Stack official 2.1.4
- 書き換える

```c++

const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";

```

結果
----

![image_original(34).jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/5ae1ff36-df3a-4e7a-a42f-53f0d7e93760.jpeg)

プログラム
----

```c++



//NeoPixel8x8_kanji_tate_scroll_M5StampS3_1


//インクルド
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <misakiUTF16.h>

#include <WiFi.h>
#include <time.h>

//定義
#define MAX_X 8
#define MAX_Y 8
#define NUM_LEDS 64
#define RGBLED_OUT 13
Adafruit_NeoPixel strip(NUM_LEDS, RGBLED_OUT, NEO_GRB + NEO_KHZ800);

unsigned char Pixel[8][8][3];  //キャラクターRAM(カラー)

long s_buff[8];

const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";


void kanji_scroll_clr() {
  for (int i = 0; i < 8; i++) {
    s_buff[i] = 0;
  }  //for
}  //kanji_scroll_clr


void kanji_scroll_set(unsigned char *font) {
  for (int i = 0; i < 8; i++) {
    int a;
    a = s_buff[i] & 0xff00;
    a = a | font[i];
    s_buff[i] = a;
  }  //for
}  //kanji_scroll_set


void kanji_scroll_1up(unsigned char *font) {
  for (int i = 0; i < 8; i++) {
    s_buff[i] = s_buff[i] << 1;
    font[i] = s_buff[i] >> 8;
  }  //for
}  //kanji_scroll_1up


//ネオピクセルに転送後に表示
void NeoPixe_OUT() {

  int k = 0;
  for (int y = 0; y < MAX_Y; y++) {
    for (int x = 0; x < MAX_X; x++) {

      strip.setPixelColor(k++, strip.Color(
                                 Pixel[y][x][0], Pixel[y][x][1], Pixel[y][x][2]));

    }  //for j
  }    //for i

  strip.show();

}  //NeoPixe_OUT


//8x8文字の表示
void ch_8x8_f(unsigned char *font, int c1, int c2, int c3) {

  int i = 0;
  for (int y = 0; y < MAX_Y; y++) {
    int a = font[i++];
    for (int x = 0; x < MAX_X; x++) {

      if (((a << x) & 0x80) != 0) {
        Pixel[y][x][0] = c1;
        Pixel[y][x][1] = c2;
        Pixel[y][x][2] = c3;
      } else {
        Pixel[y][x][0] = 0;
        Pixel[y][x][1] = 0;
        Pixel[y][x][2] = 0;
      }  //if

    }  //for j
  }    //for i

  NeoPixe_OUT();  //RBG LEDに出力

}  //ch_8x8


void font270(unsigned char *fontO, unsigned char *fontI) {

  for (int i = 0; i < 8; i++) {
    fontO[i] = 0;
  }  //for i

  for (int i = 0; i < 8; i++) {

    //処理
    //        12345678
    int m = 0b00000001;
    int a = fontI[i];

    fontO[7] = fontO[7] << 1;
    fontO[6] = fontO[6] << 1;
    fontO[5] = fontO[5] << 1;
    fontO[4] = fontO[4] << 1;

    fontO[3] = fontO[3] << 1;
    fontO[2] = fontO[2] << 1;
    fontO[1] = fontO[1] << 1;
    fontO[0] = fontO[0] << 1;

    fontO[0] = fontO[0] | (a & m);a = a >> 1;
    fontO[1] = fontO[1] | (a & m);a = a >> 1;
    fontO[2] = fontO[2] | (a & m);a = a >> 1;
    fontO[3] = fontO[3] | (a & m);a = a >> 1;

    fontO[4] = fontO[4] | (a & m);a = a >> 1;
    fontO[5] = fontO[5] | (a & m);a = a >> 1;
    fontO[6] = fontO[6] | (a & m);a = a >> 1;
    fontO[7] = fontO[7] | (a & m);

  }  //for i

}  //font270


//初期化
void setup() {

  //WiFiの初期化
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  //ネオピクセルの設定
  strip.begin();
  strip.show();

  delay(3000);  //決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
  configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");

  unsigned char font1[] = {
    0b00000000,
    0b00000000,
    0b00000000,
    0b11111111,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00000000
  };

  ch_8x8_f(font1, 5, 5, 5);  //表示
  delay(2500);

  unsigned char font2[] = {
    0b00000000,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00000000,
    0b11111111
  };

  ch_8x8_f(font2, 5, 5, 5);  //表示
  delay(2500);

}  //main


//メインループ
void loop() {

  unsigned char fontI[8];  // フォント格納バッファ
  unsigned char *font;
  font = fontI;

  char str[64] = "abcあ、埼玉、現在時刻 xx:xx";  // 文字列

  kanji_scroll_clr();


  time_t t;       //経過秒
  struct tm *tm;  //時間の実体とポインター

  t = time(NULL);                                              //経過秒を取得する
  tm = localtime(&t);                                          //経過秒を時間の構造体に変換する
  sprintf(str, "現在時刻%02d:%02d", tm->tm_hour, tm->tm_min);  //時刻を表示する
  char *ptr = str;

  while (*ptr) {  // 文字列分ループ

    font = fontI;  //tate

    ptr = getFontData(font, ptr, true);  // 1文字分のフォント取得
    if (!ptr)
      break;  // エラーの場合は終了

    //tate
    if (1 != 1) {
      unsigned char fontO[8];
      font270(fontO, font);
      font = fontO;
    }  //endif

    kanji_scroll_set(font);

    for (int i = 0; i < 8; i++) {

      kanji_scroll_1up(font);

      ch_8x8_f(font, 5, 5, 5);
      delay(500);  //0.5秒待つ

    }  //for

  }  //while


  //set time
  static int set_1H = 0;
  if (set_1H > 200) {  // 1 hour ?
    set_1H = 0;        //counter reset
    configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
  }
  set_1H++;

}  //loop


```