//【スケッチの説明】 //RP2040 CPU 搭載基板で使用できます。 //WiFi経由でNTPから取得した、時刻情報を整形する。その後 //OLED画面メモリーに 西暦年、月、日、曜日、時、分、秒を設定し //画面メモリ全体を180度回転して、表示する // //【準備】 //・RP2040 pico側 //・SSD1306 OLED側 ////I2Cアドレスは 0x3c // //【バージョン情報】 // 2023/12/31 //#include #include // Wifi制御用ライブラリ #include // I2C0を使用する #include #include #include //漢字フォント用 #define SCREEN_WIDTH 128 //解像度 128 x 64 で使用します。 #define SCREEN_HEIGHT 64 //SCREEN_HEIGHTは 32 に設定することができます。 #define OLED_RESET -1 //使用しないので -1を設定する。 #define SCREEN_ADDRESS 0x3C //I2Cアドレスは 0x3C #define OLED_SCL 17 // I2C0で使用可能 SCLのGPは 1 3 5 7 9 11 13 15 17 19 21 27 #define OLED_SDA 16 // I2C0で使用可能 SDAのGPは 0 2 4 6 8 10 12 14 16 18 20 26 // ------------------------------------------------------------ // 定数/変数 定義部 Constant / variable definition section. // ------------------------------------------------------------ const char* ssid = "ax50anotherZ"; // ご自分のWi-FiルータのSSIDを記述します。 "your-ssid" const char* password = "rock#tang@50Gpgw"; // ご自分のWi-Fiルータのパスワードを記述します。"your-password" // 曜日表示文字 static char *weekChar[7] = { "日", "月", "火", "水", "木", "金", "土" }; uint8_t font[8]; // フォント格納バッファ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //表示制御にはAdafruit製 SSD1306を使用する。 //初期化時には I2C0を使用する (Wire) // NTPによる時刻同期関数 bool setClock() { NTP.begin("ntp.nict.jp", "time.google.com"); return NTP.waitSet(); } void setup() { //Serial.begin(115200); /// Wire.begin();// i2c_scanner /// Wire.setClock(400000); //I2C0で使用するGPは SDA = 0, SCL = 1 Wire.setSDA(OLED_SDA); Wire.setSCL(OLED_SCL); if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { for(;;); } display.clearDisplay(); //何か表示されている場合に備えて表示クリア display.setTextSize(2); //フォントサイズは2(番目に小さい) display.setTextColor(SSD1306_WHITE); //色指定はできないが必要 // WiFi接続 WiFi.begin(ssid, password); if (WiFi.status() != WL_CONNECTED) { //Serial.println("WiFi Connection failed"); for (;;) yield(); } // NTP時刻同期 if (setClock()) { // 時刻取得失敗時は停止 //Serial.println("Failed to obtain time"); for (;;) yield(); } // WiFi切断 WiFi.disconnect(true); // 日本標準時をセット setenv("TZ", "JST-9", 1); tzset(); } void loop() { static time_t now; static struct tm *timeinfo; static int lastSec = -1; static char buf[20]; // 現在時を取得 time(&now); timeinfo = localtime(&now); // 時刻が変わっているか判定 if (lastSec != timeinfo->tm_sec) { lastSec = timeinfo->tm_sec; //Serial.print(asctime(timeinfo)); // 時刻が変わった時に描画する display.clearDisplay(); //何か表示されている場合に備えて表示クリア // 時分秒の描画 sprintf(buf, "%2d", timeinfo->tm_hour); display.setCursor(0, 46);//0,62 display.print(buf); //表示文字列 sprintf(buf, "%02d", timeinfo->tm_min); display.setCursor(45, 46);//45,62 display.print(buf); //表示文字列 sprintf(buf, "%02d", timeinfo->tm_sec); display.setCursor(88, 46);//87,62 display.print(buf); //表示文字列 // 年月日の描画 sprintf(buf, "%4d", timeinfo->tm_year + 1900); display.setCursor(0, 2); //テキストの表示開始位置 0,17 display.print(buf); //表示文字列 sprintf(buf, "%2d", timeinfo->tm_mon + 1); display.setCursor(0, 24); //テキストの表示開始位置 0,40 display.print(buf); //表示文字列 sprintf(buf, "%2d", timeinfo->tm_mday); display.setCursor(46, 24); //テキストの表示開始位置 48,40 display.print(buf); //表示文字列 // 曜日()の描画 getFontData(font, "("); display.drawBitmap(96, 30, font,8,8,1);//46 getFontData(font, ")"); display.drawBitmap(116, 30, font,8,8,1);//46 // 年月日時分秒の描画 // 日本語フォントをセット getFontData(font, "時"); display.drawBitmap(28, 52, font,8,8,1);//46 getFontData(font, "分"); display.drawBitmap(72, 52, font,8,8,1); getFontData(font, "秒"); display.drawBitmap(113, 52, font,8,8,1); getFontData(font, "年"); display.drawBitmap(54, 8, font,8,8,1); getFontData(font, "月"); display.drawBitmap(28, 30, font,8,8,1); getFontData(font, "日"); display.drawBitmap(72, 30, font,8,8,1); getFontData(font, weekChar[timeinfo->tm_wday]); display.drawBitmap(104, 30, font,8,8,1); display.setRotation(2); //デフォルト位置から、180度回転 display.display(); //バッファ転送(表示) } }