Hi, welcome to this week's Weekend project with Stonez56.
This week is Arduino Episode #39, "Drawing Dice Dynamically at 0.96" OLED with u8g2" This video is suitable for people who want to learn how to draw patterns on SD 1336 LED.
Hi,歡迎來到本週的 Weekend project with Stonez56。本週是 Arduino 第 39 集,“在 0.96" OLED 上使用 u8g2 動態繪製骰子”。此影片適合希望學習如何在 SD 1336 LED 上繪製圖案的人。
- u8g2 字體參考:https://github.com/olikraus/u8g2/wiki/fntlist16
- u8g2 Github:https://github.com/olikraus/u8g2
- u8g2 UTF8 font generator: https://kidsgo.net/u8g2(這是我寫的 UI, 快速, 小型化, 非方便建立你的 UTF8字型檔)
====================== ESP32 Codes ===============================
#include <Arduino.h> #include <U8g2lib.h> U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE); void setup(void) { Serial.begin(115200); u8g2.begin(); randomSeed(analogRead(5)); // randomSeed() must be in setup()! } // OLED TEXT ROW number, vertical position const byte ROW[5] = {0, 15, 31, 47, 63}; // 4 dice top-left {x, y} locations in tl[] const int tl[4][2] = {{2, 20}, {34, 28}, {68, 20}, {100, 28}}; const int size = 28; // dice width and height const int diceRoundedCorner = 5; /** * drawDiceImage * n = dice serial (1 ~ 4) * pnt = dice points (1 ~ 6) */ void drawDiceImage(int n, int pnt) { int tx = tl[n][0]; // top left x int ty = random(17, 35); // Makes the y showing at random height // int ty = tl[n][1]; //top left y, makes Y showing at fixed height int centerX = tx + (size / 2); // center of the dice X int centerY = ty + (size / 2); // center of th dice Y const int largeDotSize = 4; // drawing dot size for 1 only const int smallDotSize = 2; // drawing dot size for rest u8g2.drawRFrame(tx, ty, size, size, diceRoundedCorner); // draw the dice border // draw the points based on the variable: pnt switch (pnt) { case 1: u8g2.drawFilledEllipse(centerX, centerY, largeDotSize, largeDotSize, U8G2_DRAW_ALL); break; case 2: u8g2.drawFilledEllipse(centerX, centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX, centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); break; case 3: u8g2.drawFilledEllipse(centerX, centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX, centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX, centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); break; case 4: u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); break; case 5: u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX, centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL); break; case 6: u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX - (size / 4), centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL); u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL); break; default: break; } } void loop(void) { int dicePoint[4]; int totalPoints = 0; u8g2.clearBuffer(); //Generate 4 random dice points for (int i = 0; i < 4; i++) { dicePoint[i] = random(1, 6); // get random dice number to point drawDiceImage(i, dicePoint[i]); // draw dice i with the random dicePoint totalPoints += dicePoint[i]; // adding current points to totalPoints } //Print out the title + total points u8g2.setCursor(0, ROW[1]); u8g2.setFont(u8g2_font_lubBI14_te); // font list: https://github.com/olikraus/u8g2/wiki/fntlist16 u8g2.print("POINTS:"); u8g2.print(totalPoints); u8g2.sendBuffer(); delay(1500); }==================================================================