Arduino UNO是基于ATmega328P的Arduino开发板。它有14个数字输入/输出引脚(其中6个可用于PWM输出)、6个模拟输入引脚,一个16 MHz的晶体振荡器,一个USB接口,一个DC接口,一个ICSP接口,一个复位按钮。Arduino一词意味着该款开发板可以使用Arduino官方和第三方库文件,简化开发流程,可以让我们在不了解单片机底层原理下对其进行充分开发,因此Arduino获得了众多初级开发者的喜爱。
一、总体设计
本次项目采用模块化编程的设计方式,因为在实际设计中,我们利用状态者模式隔离不同模式下的功能操作,如下:
程序整体设计思路来自于状态者模式,对于初学者并不友好,使用C++语言设计,参考代码如下:
#include "State.h"
//状态1,继承重写doThing
void StateCase1::doThing(CurState *s){
//静态变量作为开关,防止重复创建对象
static int count = 0;
//进行开关判断,创建过就不再创建
if(count == 0){
s->getOled()->setLogo(new ServoP(s->getOled()));
count = 1;
}
//按下1按键,滑动到2状态
if(m_command == 1){
delete s->getCase(); //回收状态管理者的此时指针
s->setCase(new StateCase2); //new 一个新的状态,设置进去这里为2
count = 0; //开关打开,方便下次跳转到这时创建对象
}
//按下1按键,滑动到3状态
else if(m_command == 2){
delete s->getCase(); //回收状态管理者的此时BaseCase指针
s->setCase(new StateCase3); //new 一个新的状态,设置进去这里为3
count = 0; //开关打开,方便下次跳转到这时创建对象
}
//5键为确定,跳转到状态5
else if(m_command == 5){
delete s->getCase(); //回收状态管理者的此时BaseCase指针
s->setCase(new StateCase4); //new 一个新的状态,设置进去这里为3
count = 0; //开关打开,方便下次跳转到这时创建对象
}
}
void StateCase2::doThing(CurState *s){
//静态变量作为开关,放置重复创建对象
static int count = 0;
//进行开关判断,创建过就不再创建
if(count == 0){
s->getOled()->setLogo(new MusicP(s->getOled()));
count = 1;
}
//按下1按键,滑动到1状态
else if(m_command == 2){
delete s->getCase(); //回收状态管理者的此时BaseCase指针
s->setCase(new StateCase1); //new 一个新的状态,设置进去这里为1
count = 0; //开关打开,方便下次跳转到这时创建对象
}
else if(m_command == 5){
delete s->getCase(); //回收状态管理者的此时BaseCase指针
s->setCase(new StateCase5); //new 一个新的状态,设置进去这里为5
count = 0; //开关打开,方便下次跳转到这时创建对象
}
}
//以下状态剩余类推
void StateCase3::doThing(CurState *s){
static int count = 0;
if(count == 0){
s->getOled()->setLogo(new ClockP(s->getOled()));
count = 1;
}
if(m_command == 1){
delete s->getCase();
s->setCase(new StateCase1);
count = 0;
}
else if(m_command == 5){
delete s->getCase();
s->setCase(new StateCase7);
count = 0;
}
}
void StateCase4::doThing(CurState *s){
static int count = 0;
static int angle=0;
if(count == 0){
s->getOled()->setLogo(new Servo_fun(s->getOled()));
count = 1;
}
s->getOled()->printW(48, 32, 2, angle);
if(m_command == 3)