0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArduinoAdvent Calendar 2021

Day 10

arduinoで時計 その2

Last updated at Posted at 2021-10-09

#概要

arduinoで時計やってみた。

#写真

DSCN0542.JPG

#サンプルコード

#include <Adafruit_GFX.h>
#include "Adafruit_TFTLCD.h"

#define XP			  	8
#define YP			  	A3
#define XM			  	A2
#define YM			  	9
#define LCD_CS		  	A3
#define LCD_CD		  	A2
#define LCD_WR		  	A1
#define LCD_RD		  	A0
#define LCD_RESET	  	A4
#define BLACK		  	0x0000
#define BLUE			0x001F
#define RED			 	0xF800
#define GREEN		   	0x07E0
#define CYAN			0x07FF
#define MAGENTA		 	0xF81F
#define YELLOW		  	0xFFE0
#define WHITE		   	0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

uint16_t ccenterx,
	ccentery;
const uint16_t cradius = 110;
const float scosConst = 0.0174532925;
float sx = 0,
	sy = 1,
	mx = 1,
	my = 0,
	hx = -1,
	hy = 0;
float sdeg = 0,
	mdeg = 0,
	hdeg = 0;
uint16_t osx,
	osy,
	omx,
	omy,
	ohx,
	ohy;
uint16_t x0 = 0,
	x1 = 0,
	yy0 = 0,
	yy1 = 0;
uint32_t targetTime = 0;
uint8_t hh,
	mm,
	ss;
void drawClockFace() {
	tft.fillCircle(ccenterx, ccentery, cradius, BLUE);
	tft.fillCircle(ccenterx, ccentery, cradius - 4, BLACK);
	for (int i = 0; i < 360; i += 30) 
	{
		sx = cos((i - 90) * scosConst);
		sy = sin((i - 90) * scosConst);
		x0 = sx * (cradius - 4) + ccenterx;
		yy0 = sy * (cradius - 4) + ccentery;
		x1 = sx * (cradius - 11) + ccenterx;
		yy1 = sy * (cradius - 11) + ccentery;
		tft.drawLine(x0, yy0, x1, yy1, BLUE);
	}
}
static uint8_t conv2d(const char * p) {
	uint8_t v = 0;
	if ('0' <= * p && * p <= '9') 
		v = * p - '0';
	return 10 * v + * ++p - '0';
}
void drawClockHands(uint8_t h, uint8_t m, uint8_t s) {
	sdeg = s * 6;
	mdeg = m * 6 + sdeg * 0.01666667;
	hdeg = h * 30 + mdeg * 0.0833333;
	hx = cos((hdeg - 90) * scosConst);
	hy = sin((hdeg - 90) * scosConst);
	mx = cos((mdeg - 90) * scosConst);
	my = sin((mdeg - 90) * scosConst);
	sx = cos((sdeg - 90) * scosConst);
	sy = sin((sdeg - 90) * scosConst);
	tft.drawLine(ohx, ohy, ccenterx + 1, ccentery + 1, BLACK);
	tft.drawLine(omx, omy, ccenterx + 1, ccentery + 1, BLACK);
	tft.drawLine(osx, osy, ccenterx + 1, ccentery + 1, BLACK);
	tft.drawLine(hx * (cradius - 28) + ccenterx + 1, hy * (cradius - 28) + ccentery + 1, ccenterx + 1, ccentery + 1, WHITE);
	tft.drawLine(mx * (cradius - 17) + ccenterx + 1, my * (cradius - 17) + ccentery + 1, ccenterx + 1, ccentery + 1, WHITE);
	tft.drawLine(sx * (cradius - 14) + ccenterx + 1, sy * (cradius - 14) + ccentery + 1, ccenterx + 1, ccentery + 1, RED);
	tft.fillCircle(ccenterx + 1, ccentery + 1, 3, RED);
	osx = sx * (cradius - 14) + ccenterx + 1;
	osy = sy * (cradius - 14) + ccentery + 1;
	omx = mx * (cradius - 17) + ccenterx + 1;
	omy = my * (cradius - 17) + ccentery + 1;
	ohx = hx * (cradius - 28) + ccenterx + 1;
	ohy = hy * (cradius - 28) + ccentery + 1;
}
void drawPrintTime(uint16_t x, uint16_t y, uint8_t h, uint8_t m, uint8_t s) {
	tft.setCursor(0, 0);
	tft.print("clock");
	tft.setCursor(x, y);
	tft.print(__DATE__);
	tft.setCursor(x, y - 13);
	if (hh > 12)
	{
		if (hh < 22)
			tft.print('0');
		tft.print (hh - 12);
	}
	else
	{
		if (hh < 10)
			tft.print('0');
		tft.print (hh);
	}
	tft.print (':');
	if (mm < 10)
		tft.print('0');
	tft.print(mm);
	tft.print(':');
	if (ss < 10)
		tft.print('0');
	tft.print(ss);
	if (hh > 12)
	{
		tft.print(" PM");
	}
	else
	{
		tft.print(" AM");
	}
}
void setup(void) {
	tft.reset();
	uint16_t identifier = 0x9341;
	tft.begin(identifier);
	tft.fillScreen(BLACK);
	tft.setTextColor(WHITE);
	tft.setTextSize(2);
	ccenterx = tft.width() / 2;
	ccentery = tft.height() / 2;
	osx = ccenterx;
	osy = ccentery;
	omx = ccenterx;
	omy = ccentery;
	ohx = ccenterx;
	ohy = ccentery;
	drawClockFace();
	hh = conv2d(__TIME__);
	mm = conv2d(__TIME__ + 3);
	ss = conv2d(__TIME__ + 6);
	targetTime = millis() + 1000;
}
void loop() {
	if (targetTime < millis())
	{
		targetTime = millis() + 1000;
		ss++;
		if (ss == 60)
		{
			ss = 0;
			mm++;
			if (mm > 59)
			{
				mm = 0;
				hh++;
				if (hh > 23)
					hh = 0;
			}
		}
		drawClockHands(hh, mm, ss);
		tft.fillRect(30, 282, 180, 20, RED);
		drawPrintTime(34, 300, hh, mm, ss);
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?