lcd动图
如视频,参考了书籍《代码本色:用编程模拟自然系统》 也叫《The Nature of Code》;使用了向量的概念去搭建。
processing.h
#ifndef PROCESSING_H
#define PROCESSING_H
#include <stdint.h>
typedef struct structVector
{
float x;
float y;
}vector;
typedef struct structMover
{
vector location;
vector velocity;
vector acceleration;
unsigned int xsize;//左上角起始坐标开始 向右x距离
unsigned int ysize;// 向下y距离
}mover;
void vectorAdd(vector *a,vector *b);//向量相加
void vectorSub(vector *a,vector *b);//向量相减
void vectorMult(vector *a,signed short k);//乘以标量以延申向量
void vectorDiv(vector *a,signed short k);//除以标量以缩短向量
double vectorMag(vector *a);//计算向量长度
void moverTest(mover *m);
void initMover(mover *m);
#endif
processing.c
#include "processing.h"
#include "math.h"
#include "spi.h"
#include "GUI.h"
#include "stdlib.h"
#include "systick.h"
const unsigned char bmp1_24x24[][72] ={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xE7,0xF0,0x1F,0xFF,0xF8,0x3C,
0x7E,0x3C,0x78,0x3C,0x1E,0x70,0x18,0x0E,0x70,0x00,0x0E,0x60,0x00,0x06,0x60,0x00,
0x06,0x70,0x00,0x0E,0x70,0x00,0x0E,0x38,0x00,0x1C,0x38,0x00,0x1C,0x1C,0x00,0x38,
0x0E,0x00,0x70,0x0F,0x00,0xF0,0x07,0xC3,0xE0,0x01,0xE7,0x80,0x00,0xFF,0x00,0x00,
0x7E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*\图标\喜欢_like.bmp",1*/
};
void vectorAdd(vector *a,vector *b)//向量相加
{
vector *dest = a;
vector *source = b;
dest->x += source->x;
dest->y += source->y;
}
void vectorSub(vector *a,vector *b)//向量相减
{
vector *dest = a;
vector *source = b;
dest->x -= source->x;
dest->y -= source->y;
}
void vectorMult(vector *a,signed short k)//乘以标量以延申向量
{
vector *dest = a;
dest->x *= k;
dest->y *= k;
}
void vectorDiv(vector *a,signed short k)//除以标量以缩短向量
{
vector *dest = a;
dest->x /= k;
dest->y /= k;
}
double vectorMag(vector *a)//计算向量长度
{
return sqrt(a->x * a->x + a->y * a->y);
}
void vectorNormalize(vector *a)//归一化
{
float m = vectorMag(a);
if(m != 0)
{
vectorDiv(a,m);
}
}
//------------------------------------------------mover
//#define X_MAX_PIXEL 160
//#define Y_MAX_PIXEL 80
void initMover(mover *m)
{
// m->location.x = rand()%159;
// m->location.y = rand()%79;
m->location.x = 80;
m->location.y = 0;
m->velocity.x = 0;
m->velocity.y = 0;
m->acceleration.x = 0;
m->acceleration.y = 0;
m->xsize = 23;
m->ysize = 23;
}
void checkEdges(mover *m)
{
if((m->location.x + m->xsize)> X_MAX_PIXEL-1)
{
m->location.x = X_MAX_PIXEL - m->xsize - 1;
m->velocity.x *= -1;
}
else if(m->location.x<0)
{
m->velocity.x *= -1;
m->location.x =0;
}
if((m->location.y + m->ysize)> Y_MAX_PIXEL-1)
{
m->location.y =Y_MAX_PIXEL-1-23;
m->velocity.y *= -1;
}
// else if(m->location.y<0)
// {
// m->location.y = Y_MAX_PIXEL - 1 - m->ysize;
// }
}
void vectorDisplay(mover *m)
{
showBmp(m->location.x,m->location.y,RED,BLACK,0,bmp1_24x24[0]);
}
void applyForce(mover *m,vector force)//力加进来
{
vectorAdd(&(m->acceleration),&force);//加速度赋值
}
void vectorUpdate(mover *m)
{
vectorAdd(&(m->velocity),&(m->acceleration));//通过加速度获取速度
vectorAdd(&(m->location),&(m->velocity));//通过速度获取位置
vectorMult(&(m->acceleration),0);//加速度不要累计
//applyForce会给加速度赋值,导致速度每经过同样时间间隔 累加同样的值 也就是匀加速
}
void moverTest(mover *m)
{
static unsigned short xlast = 0;
static unsigned short ylast = 0;
vector gravity;
gravity.x=0;
gravity.y=0.5;
if(IS_TIM25MS)
{
//Lcd_Clear(BLACK);
Lcd_ClearArea(xlast,ylast,xlast+23,ylast+23,BLACK);
applyForce(m,gravity);//给物体一个重力加速度
vectorUpdate(m);//速度一直随着时间累加 所以速度会越来越快
checkEdges(m);
vectorDisplay(m);
//delay_1ms(30);
xlast = m->location.x;
ylast = m->location.y;
}
}