2021年9月29日,准备开始写博客。用来记录自己在嵌入式开发、C语言以及自己其他小项目的心得体会和知识点。 下面是直接在oled上面写的一个简单的计算器程序,有一些功能没写,以后尽量补充。 #include “calculator.h” #include “delay.h” #include “Display.h” #include “math.h” #include “key.h”
#define GET_ARRAY_LEN(array,len) {len = (sizeof(array) / sizeof(array[0]));}
u8 char_switch_flag[3][8]= { {1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0} }; char char_switch[24][4]= //最后还有一个 ‘\0’,所以是4个字节。 { “7”,“8”,“9”,".",“sin”,“x^2”,"+","-", “4”,“5”,“6”,“0”,“cos”,“sqr”,"*","/", “1”,“2”,“3”,“c”,“tan”,“cur”,“d”,"=" }; u8 x_line_l[]={0,11,11,11,13,23,25,11}; u8 x_line_r[]={0,11,11,11,25,23,13,11};
void array_set(u8 *array,u8 lenth,u8 set_num) //数字数组清零 { while(lenth–){ array[lenth]=set_num; } }
void calculator_display_Init() //NO_INVERSED { u8 m=0,n=0; u8 left=7,top=20,right=15,bottom=30;
DrawRect(0,0,127,17,1);
for(m=0;m<3;m++){
for(n=0;n<8;n++){
left+=x_line_l[n];
right+=x_line_r[n];
DrawRect(left,top+m*13,right,bottom+m*13,1);
GUI_DrawString(left+1,top+2+m*13,*(char_switch+m*8+n),char_switch_flag[m][n],FONT6X8);
}
left=7;right=15;
}
ScreenRefreshAll(main_cache);
}
static u8 i=0,j=0; void calculator_char_selection() { u8 x=0,y=0;
if(G_IS_BIT_SET(key_down,4)){ //up 单击 1
G_CLEAR_BIT(key_down,4);
if(i--<1)i=2;
}
if(G_IS_BIT_SET(key_down,5)){ //down 单击 2
G_CLEAR_BIT(key_down,5);
if(++i>=3)i=0;
}
if(G_IS_BIT_SET(key_down,6)){ //right 单击 3
G_CLEAR_BIT(key_down,6);
if(++j>=8)j=0;
}
if(G_IS_BIT_SET(key_down,7)){ //left 单击 4
G_CLEAR_BIT(key_down,7);
if(j--<1)j=7;
}
for(x=0;x<3;x++){
for(y=0;y<8;y++){
if((x==i)&&(y==j))char_switch_flag[i][j]=1;
else char_switch_flag[x][y]=0;
}
}
}
TYPE_FLAG alg_judge_type(char c) { if(c>=‘0’ && c<=‘9’)return NUMBER; else if((c == ‘+’)||(c == ‘-’)||(c == ‘*’)||(c == ‘/’)||(c == ‘=’))return YUNSHUAN_1; //这些是需要显示的 else if((c == ‘.’)||(c == ‘c’)||(c == ‘d’))return YUNSHUAN_2; //这些是功能按键,不用显示。 return INVALID_TYPE; }
//m^n函数 u32 Num_pow(u8 m,u8 n) { u32 result=1; while(n–)result*=m; return result; }
float String_To_Num(unsigned char *input_num_buff,unsigned char num_lenth) { float result=0; u8 i=0; for(i=0;i<num_lenth;i++){ result+=input_num_buff[i]*Num_pow(10,num_lenth-i); } return result+input_num_buff[num_lenth]; }
float input_num1=0,input_num2=0,output_num=0; u8 input_num_buff[8]={0}; extern char str[10]; static u8 string_lenth=0; static u8 Num_1_2_flag=0; static u8 num_lenth=0; static u8 char_flag=0; volatile TYPE_FLAG rec_state;
void calculator_display_Fuwei() { string_lenth=0;Num_1_2_flag=0;num_lenth=0;char_flag=0;input_num1=0;input_num2=0;output_num=0; array_set(input_num_buff,8,0); }
void calculator_char_input() //计算器内容处理函数 { if(G_IS_BIT_SET(key_down,12)){ //确认 双击 1 输入框显示对应的符号。 G_CLEAR_BIT(key_down,12); rec_state=alg_judge_type(char_switch[i*8+j][0]);
switch(rec_state) //显示处理部分
{
case NUMBER:case YUNSHUAN_1:
GUI_DrawString(1+string_lenth*6,1,*(char_switch+i*8+j),NO_INVERSED,FONT6X8);
if((j==4)||(j==5))string_lenth+=3; //这是那两列特殊符号函数。
else string_lenth+=1;
break;
case YUNSHUAN_2:case INVALID_TYPE:break;
}
switch(rec_state){
case NUMBER:
if(Num_1_2_flag==0){
input_num_buff[num_lenth++]=char_switch[i*8+j][0]-'0';
}else{
input_num_buff[num_lenth++]=char_switch[i*8+j][0]-'0';
}
break;
case YUNSHUAN_1:
switch(char_switch[i*8+j][0]){
case '+':char_flag=1;break;
case '-':char_flag=2;break;
case '*':char_flag=3;break;
case '/':char_flag=4;break;
case '=':
if(Num_1_2_flag==1){ //计算第二个数
input_num2=String_To_Num(input_num_buff,num_lenth-1);
array_set(input_num_buff,8,0);
num_lenth=0;Num_1_2_flag=0;
}
switch(char_flag){
case 1:
output_num=input_num1+input_num2;
input_num1=0;input_num2=0;
break;
case 2:
output_num=input_num1-input_num2;
input_num1=0;input_num2=0;
break;
case 3:
output_num=input_num1*input_num2;
input_num1=0;input_num2=0;
break;
case 4:
output_num=input_num1/input_num2;
input_num1=0;input_num2=0;
break;
}
sprintf(str,"%20.3f",(float)output_num);
GUI_DrawString(7,9,str,NO_INVERSED,FONT6X8);
break;
}
if(Num_1_2_flag==0){ //计算第一个数
input_num1=String_To_Num(input_num_buff,num_lenth-1);
array_set(input_num_buff,8,0);
num_lenth=0;Num_1_2_flag=1;
}
break;
case YUNSHUAN_2:
switch(char_switch[i*8+j][0]){
case 'd':
break;
case 'c': //屏幕回复原始状态。
calculator_display_Fuwei();
ClearCacheArea(1,1,127,17,0);
break;
case '.':break;
}
break;
case INVALID_TYPE:break;
}
}
}
void Calculator_All() { calculator_display_Init(); calculator_char_selection(); calculator_char_input(); } `
|