/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-08-06 Jinliang the first version
*/
#include "tetris_clock.h"
#include "cn_font.h"
#include "calendar.h"
/* config grid */
#define GRID_SIZE 14
#define GRID_ROW_NUM (RK043FN48H_HEIGHT/GRID_SIZE-6)
#define GRID_COL_NUM (RK043FN48H_WIDTH/GRID_SIZE-1)
/* config number and colon position */
#define NUMBER_COUNT 4
#define FIRST_NUMBER_OFFSET 1
#define SECOND_NUMBER_OFFSET 8
#define THIRD_NUMBER_OFFSET 18
#define FOURTH_NUMBER_OFFSET 25
#define COLON_OFFSET_X 15
#define COLON_OFFSET_Y 5
uint16_t u16GridMap[GRID_ROW_NUM * GRID_COL_NUM];
uint8_t u8ClockNumber[NUMBER_COUNT+1] = {0, 0, 0, 0, 0};
stTetrisClockHandle stTetrisNumHandle[NUMBER_COUNT];
struct tm *p_tm;
static uint8_t TetrisClockUpdateTime(uint8_t *data);
static void DrawTetrisRect(uint16_t TetrisX, uint16_t TetrisY, uint16_t Width, uint16_t Height,
uint32_t Fcolor, uint32_t Bcolor);
const uint32_t u32ColorRgb565Table[] =
{
// SCRLET ,
CAMEL ,
SUN_ORANGE ,
// LAWN_GREEN ,
FOREST_GREEN ,
MARINE_BLUE ,
CHROME_YELLOW ,
// DARK_SLATE_BLUE ,
// CAYN_BLUE ,
// RUBY ,
// MIDIUM_TURQUOISE ,
// VERIDIAN ,
// LIGHT_LIME ,
// INDIAN_RED ,
// MAROON ,
// MISTY_ROSE ,
// LIGHT_CORAL ,
// HELIOTROPE ,
};
const stNumSeqBase au8NumZeroTerisSeq[] =
{
{18, 3}, {4, 1}, {9, 3}, {9, -1}, {5, 0}, {5, 4}, {5, 4}, {11, 0}, {0, 0}, {9, 0},
{6, 2}, {19, 4}
};
const stNumSeqBase au8NumOneTerisSeq[] =
{
{3, 4}, {17, 3}, {19, 4}, {17, 3}, {19, 4}
};
const stNumSeqBase au8NumTwoTerisSeq[] =
{
{18, 3}, {14, 0}, {1, 1}, {2, 4}, {18, 3}, {14, 0}, {1, 1}, {2, 4}, {2, 2}, {0, 1}, {0, 0}
};
const stNumSeqBase au8NumThreeTerisSeq[] =
{
{18, 3}, {14, 0}, {1, 1}, {0, 5}, {19, 3}, {14, 0}, {1, 1}, {2, 4}, {1, 1}, {12, 3}, {16, 0}
};
const stNumSeqBase au8NumFourTerisSeq[] =
{
{2, 4}, {2, 4}, {2, 0}, {2, 0}, {18, 3}, {14, 0}, {1, 1}, {2, 4}, {2, 4}
};
const stNumSeqBase au8NumFiveTerisSeq[] =
{
{2, 4, 8}, {2, 2, 8}, {0, 1}, {0, 0}, {1, 1}, {16, 0, 4}, {12, 3}, {2, 4}, {1, 1}, {12, 3},
{16, 0}
};
const stNumSeqBase au8NumSixTerisSeq[] =
{
{2, 4, 8}, {2, 2, 8}, {0, 1}, {0, 0}, {1, 1}, {16, 0}, {12, 3}, {2, 4}, {2, 0}, {1, 1},
{12, 3},{16, 0}
};
const stNumSeqBase au8NumSevenTerisSeq[] =
{
{18, 3}, {14, 0}, {1, 1}, {17, 3}, {0, 5}, {13, 3}, {2, 4}
};
const stNumSeqBase au8NumEightTerisSeq[] =
{
{15, 4}, {1, 0}, {1, 1}, {7, 4}, {7, 0}, {16, 0}, {12, 1}, {9, 3}, {7, 0}, {12, 2}, {0, 5},
{9, -1},{1, 1}
};
const stNumSeqBase au8NumNineTerisSeq[] =
{
{10, 2}, {4, 0}, {5, 4}, {9, -1}, {12, 2}, {0, 5}, {5, 0}, {1, 1}, {2, 4}, {1, 2}, {1, 2}, {2, 0}
};
const stNumSeqBase *stNumSeqArrayTable[] =
{
au8NumZeroTerisSeq ,
au8NumOneTerisSeq ,
au8NumTwoTerisSeq ,
au8NumThreeTerisSeq ,
au8NumFourTerisSeq ,
au8NumFiveTerisSeq ,
au8NumSixTerisSeq ,
au8NumSevenTerisSeq ,
au8NumEightTerisSeq ,
au8NumNineTerisSeq ,
};
const uint16_t u16NumSeqArraySize[] =
{
sizeof(au8NumZeroTerisSeq )/sizeof(stNumSeqBase),
sizeof(au8NumOneTerisSeq )/sizeof(stNumSeqBase),
sizeof(au8NumTwoTerisSeq )/sizeof(stNumSeqBase),
sizeof(au8NumThreeTerisSeq)/sizeof(stNumSeqBase),
sizeof(au8NumFourTerisSeq )/sizeof(stNumSeqBase),
sizeof(au8NumFiveTerisSeq )/sizeof(stNumSeqBase),
sizeof(au8NumSixTerisSeq )/sizeof(stNumSeqBase),
sizeof(au8NumSevenTerisSeq)/sizeof(stNumSeqBase),
sizeof(au8NumEightTerisSeq)/sizeof(stNumSeqBase),
sizeof(au8NumNineTerisSeq )/sizeof(stNumSeqBase),
};
const uint16_t u16BasicModelTable[20] = {
/* #
* #
* #
* #
*/
0xC840,/* 0 */
/* # # # #
*/
0x3210,/* 1 */
/* # #
* # #
*/
0x5410,/* 2 */
/* # #
* # #
*/
0x5410,/* 3 */
/* # #
* # #
*/
0x6510,/* 4 */
/* #
* # #
* #
*/
0x8541,/* 5 */
/* # #
* # #
*/
0x5421,/* 6 */
/* #
* # #
* #
*/
0x9540,/* 7 */
/* #
* # # #
*/
0x6541,/* 8 */
/* #
* # #
* #
*/
0x9651,/* 9 */
/* # # #
* #
*/
0x9654,/* 10 */
/* #
* # #
* #
*/
0x9541,/* 11 */
/* #
* # # #
*/
0x6542,/* 12 */
/* #
* #
* # #
*/
0xA951,/* 13 */
/* # # #
* #
*/
0x8654,/* 14 */
/* # #
* #
* #
*/
0x9510,/* 15 */
/* #
* # # #
*/
0x6540,/* 16 */
/* # #
* #
* #
*/
0x9521,/* 17 */
/* # # #
* #
*/
0xA654,/* 18 */
/* #
* #
* # #
*/
0x9851, /* 19 */
};
void down(stTetrisClockHandle *handle)
{
int16_t xx, yy;
handle->u16TerisY += 1;
for (uint16_t i = handle->u16TerisMode; xx = handle->u16TerisX + (i&0x3), yy = handle->u16TerisY + ((i>>2)&0x3), i; i >>= 4)
{
if (yy >= GRID_ROW_NUM-handle->pNumSeq[handle->u8SeqIdx].u8DefY|| u16GridMap[yy * GRID_COL_NUM + xx]) {
/* crash bottom */
handle->u16TerisY -= 1;
handle->u8EndFlag = 1;
}
}
}
void tetris_main(void)
{
static uint8_t u8LastSec = 0xff;
static uint8_t u8LastDay = 0xff;
static uint32_t u8AnimDelay = 0;
stTetrisClockHandle *handle = NULL;
uint32_t u32RandomColor = 0;
int16_t xx, yy;
uint8_t u8RandomNum = 0;
uint8_t u8MinuteChange = TetrisClockUpdateTime(u8ClockNumber);
if (u8MinuteChange) {
/* clean screen and grid map */
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_FillRect(0, 0, RK043FN48H_WIDTH, 200);
rt_memset(u16GridMap, 0, sizeof(u16GridMap));
/* init handle */
for (uint16_t i = 0; i < NUMBER_COUNT; i++) {
handle = &stTetrisNumHandle[i];
u8RandomNum = HAL_RNG_GetRandomNumber(&hrng);
/* set clock number offset */
(i == 0) && (handle->u16TerisXOffset = FIRST_NUMBER_OFFSET);
(i == 1) && (handle->u16TerisXOffset = SECOND_NUMBER_OFFSET);
(i == 2) && (handle->u16TerisXOffset = THIRD_NUMBER_OFFSET);
(i == 3) && (handle->u16TerisXOffset = FOURTH_NUMBER_OFFSET);
/* init paramter */
handle->pNumSeq = stNumSeqArrayTable[u8ClockNumber[i]];
handle->u8SeqIdx = u16NumSeqArraySize[u8ClockNumber[i]] - 1;
handle->u32TerisColor = u32ColorRgb565Table[u8RandomNum % (sizeof(u32ColorRgb565Table)/sizeof(uint32_t))];
handle->u16TerisMode = u16BasicModelTable[handle->pNumSeq[handle->u8SeqIdx].u8ModeIdx];
handle->u16TerisX = handle->pNumSeq[handle->u8SeqIdx].u8DefX + handle->u16TerisXOffset;
handle->u16TerisY = 0;
handle->u8EndFlag = 0;
}
}
/* delay 80 ms */
if (++u8AnimDelay % 8 == 0) {
/* draw clock number loop */
for (uint16_t i = 0; i < NUMBER_COUNT; i++) {
handle = &stTetrisNumHandle[i];
/* update next tetris mode */
if (handle->u8EndFlag) {
if (handle->u8SeqIdx == 0)
continue;
u8RandomNum = HAL_RNG_GetRandomNumber(&hrng);
handle->u8SeqIdx--;
handle->u32TerisColor = u32ColorRgb565Table[u8RandomNum % (sizeof(u32Colo
评论0