OLED Analog Clock

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OLED Analog Clock相关的知识,希望对你有一定的参考价值。

OLED Analog Clock

本示例来源于:http://www.rinkydinkelectronics.com/p_oled_analog_clock.php

  • 相关的库:OLED_I2C和DS3231都可在上面网页下载
  • 时钟效果
    在这里插入图片描述
    在这里插入图片描述
  • 源码
// Enable this if your terminal program supports VT100 control sequences.
// The Serial Monitor in the Arduino IDE does not support VT100 control sequences. 
// If using the Serial Monitor the line ending should be set to 'No line ending'.
#define VT100_MODE  0

#include <OLED_I2C.h>
#include <DS3231.h>

// Declare which fonts we will be using
extern uint8_t Sinclair_M[];

// Init the OLED and DS3231 using the hardware I2C pins
OLED myOLED(SDA, SCL);
DS3231 rtc(SDA, SCL);

// Init a Time-data structure
Time  t;

int clockCenterX=31;
int clockCenterY=31;
int oldsec=0;

void setup()
{
  myOLED.begin();
  myOLED.setFont(Sinclair_M);
  
  rtc.begin();

  Serial.begin(115200);
  Serial.println("Send any character to enter serial mode...");
  Serial.println();

  t = rtc.getTime();
}

void drawDisplay()
{
  // Clear screen
  myOLED.clrScr();
  
  // Draw Clockface
  for (int i=0; i<2; i++)
  {
    myOLED.drawCircle(clockCenterX, clockCenterY, 31-i);
  }
  for (int i=0; i<3; i++)
  {
    myOLED.drawCircle(clockCenterX, clockCenterY, i);
  }
  
  // Draw a small mark for every hour
  for (int i=0; i<12; i++)
  {
    drawMark(i);
  }  
  t = rtc.getTime();
}

void drawMark(int h)
{
  float x1, y1, x2, y2;
  
  h=h*30;
  h=h+270;
  
  x1=29*cos(h*0.0175);
  y1=29*sin(h*0.0175);
  x2=26*cos(h*0.0175);
  y2=26*sin(h*0.0175);
  
  myOLED.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
}

void drawSec(int s)
{
  float x1, y1, x2, y2;

  s=s*6;
  s=s+270;
  
  x1=29*cos(s*0.0175);
  y1=29*sin(s*0.0175);
  x2=26*cos(s*0.0175);
  y2=26*sin(s*0.0175);
  
  if ((s % 5) == 0)
    myOLED.clrLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
  else
    myOLED.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
}

void drawMin(int m)
{
  float x1, y1, x2, y2, x3, y3, x4, y4;

  m=m*6;
  m=m+270;
  
  x1=25*cos(m*0.0175);
  y1=25*sin(m*0.0175);
  x2=3*cos(m*0.0175);
  y2=3*sin(m*0.0175);
  x3=10*cos((m+8)*0.0175);
  y3=10*sin((m+8)*0.0175);
  x4=10*cos((m-8)*0.0175);
  y4=10*sin((m-8)*0.0175);
  
  myOLED.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
  myOLED.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
  myOLED.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
  myOLED.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
}

void drawHour(int h, int m)
{
  float x1, y1, x2, y2, x3, y3, x4, y4;

  h=(h*30)+(m/2);
  h=h+270;
  
  x1=20*cos(h*0.0175);
  y1=20*sin(h*0.0175);
  x2=3*cos(h*0.0175);
  y2=3*sin(h*0.0175);
  x3=8*cos((h+12)*0.0175);
  y3=8*sin((h+12)*0.0175);
  x4=8*cos((h-12)*0.0175);
  y4=8*sin((h-12)*0.0175);
  
  myOLED.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
  myOLED.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
  myOLED.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
  myOLED.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
}

void printDate()
{
  Time t_temp;
  
  t_temp = rtc.getTime();
  myOLED.print(rtc.getDOWStr(FORMAT_SHORT), RIGHT, 0);
  if (t_temp.date<10)
    myOLED.printNumI(t_temp.date, 96, 16);
  else
    myOLED.printNumI(t_temp.date, 88, 16);
  myOLED.print(rtc.getMonthStr(FORMAT_SHORT), RIGHT, 32);
  myOLED.printNumI(t_temp.year, RIGHT, 47);
}

void loop()
{
  int x, y;
  int prevSec;
  
  drawDisplay();
  drawSec(t.sec);
  drawMin(t.min);
  drawHour(t.hour, t.min);
  printDate();
  myOLED.update();
  
  prevSec = t.sec;
  while (t.sec == prevSec)
  {
    if (Serial.available()>0)
      serialMode();
    delay(100);
    t = rtc.getTime();
  }
}

以上是关于OLED Analog Clock的主要内容,如果未能解决你的问题,请参考以下文章

51单片机——My-Clock项目

Arduino——My-Clock项目 发布时间:2018-12-31

ESP8266+OLED(时间跳动)

[12小时Javascript Clock显示24时间,错误的AM / PM

ESP8266+1.3寸OLED显示-U8g2图形库—IIC使用方法

perf + 火焰图分析程序性能