#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_types_base.h"
#include "hi_errno.h"
#include "hi_io.h"
#include "hi_gpio.h"
#include "hi_i2c.h"
#include "hi_time.h"
#include "hi_watchdog.h"
#include "engine.h"
#include "ssd1306.h"
#include "pca9685.h"
static hi_u16 an1 = 135; // 当前角度
static hi_u16 an2 = 135; // 转向角度
static hi_u16 lef = 0; // 左边界
static hi_u16 mid = 135; // 中间
static hi_u16 rig = 270; // 右边界
static hi_u8 pin = 0; // 端口
static hi_u8 run = 0; // 运行 1 0
static hi_u8 spa = 0; // 跨度 1 10 30
static hi_u8 del = 0; // 延时
hi_void Display_State(hi_u8 s, hi_u8 d, hi_u8 r)
{
hi_u8 y = (2 - 1) * 2;
Buffer_Clear(y);
hi_u8 ss[8] = {0};
sprintf(ss, "%d", s);
hi_u8 sl = (hi_u8)strlen(ss);
hi_u8 sx = 4 + (sl * 8) / 2 - 1;
Buffer_Input_String(sx, y, ss, sl);
hi_u8 ds[8] = {0};
sprintf(ds, "%d", d);
hi_u8 dl = (hi_u8)strlen(ds);
hi_u8 dx = 90 + (dl * 8) / 2 - 1;
Buffer_Input_String(dx, y, ds, dl);
if(r)
{
Buffer_Input_String(51, y, "Run", 3);
}else{
Buffer_Input_String(47, y, "Stop", 4);
}
Buffer_Show(y);
}
hi_void Display_Angle(hi_u16 a, hi_u16 b)
{
hi_u8 y = (3 - 1) * 2;
Buffer_Clear(y);
hi_u8 as[8] = {0};
sprintf(as, "%d", a);
hi_u8 al = (hi_u8)strlen(as);
hi_u8 ax = (128 - (al * 8)) / 2 - 1;
Buffer_Input_String(ax, y, as, al);
hi_u8 bs[8] = {0};
sprintf(bs, "%d", b);
hi_u8 bl = (hi_u8)strlen(bs);
hi_u8 bx = (24 - (bl * 8)) / 2;
if(a > b)
{
Buffer_Input_String(11 + bx, y, bs, bl);
Buffer_Input_String(35, y, "<-", 2);
}
if(a < b)
{
Buffer_Input_String(91 + bx, y, bs, bl);
Buffer_Input_String(75, y, "->", 2);
}
Buffer_Show(y);
}
hi_void Display_SE(hi_void)
{
Display_State(spa, del, run);
Display_Angle(an1, an2);
}
hi_u8 RC_Control(hi_u8 con)
{
hi_u8 ret = 1;
// 0xa2, 1
if(con == 0xa2)
{
printf(" 1 : \n");
spa = 1;
}
// 0x62, 2
if(con == 0x62)
{
printf(" 2 : \n");
spa = 10;
}
// 0xe2, 3
if(con == 0xe2)
{
printf(" 3 : \n");
spa = 30;
}
// 0x22, 4
if(con == 0x22)
{
printf(" 4 : \n");
}
// 0x02, 5
if(con == 0x02)
{
printf(" 5 : \n");
}
// 0xc2, 6
if(con == 0xc2)
{
printf(" 6 : \n");
}
// 0xe0, 7
if(con == 0xe0)
{
printf(" 7 : \n");
}
// 0xa8, 8
if(con == 0xa8)
{
printf(" 8 : \n");
}
// 0x90, 9
if(con == 0x90)
{
printf(" 9 : \n");
}
// 0x98, 0
if(con == 0x98)
{
printf(" 0 : \n");
spa = 0;
}
// 0x68, *
if(con == 0x68)
{
printf(" * : \n");
ret = 0;
}
// 0xb0, #
if(con == 0xb0)
{
printf(" # : \n");
if(del == 0)
{
del = 1;
}else{
del = 0;
}
}
// 0x18, up
if(con == 0x18)
{
printf(" up : \n");
an2 = mid;
run = 1;
}
// 0x4a, down
if(con == 0x4a)
{
printf(" down : \n");
}
// 0x10, left
if(con == 0x10)
{
printf(" left : \n");
if(spa == 0)
{
an2 = lef;
}else{
if(an1 > spa)
{
an2 = an1 - spa;
}else{
an2 = lef;
}
}
run = 1;
}
// 0x5a, right
if(con == 0x5a)
{
printf(" right : \n");
if(spa == 0)
{
an2 = rig;
}else{
an2 = an1 + spa;
if(an2 > rig)
{
an2 = rig;
}
}
run = 1;
}
// 0x38, OK
if(con == 0x38)
{
printf(" OK : \n");
if(run == 1)
{
run = 0;
an2 = an1;
Display_Angle(an1, an2);
}
}
Display_State(spa, del, run);
return ret;
}
hi_void Engine_Run(hi_void)
{
if(an1 == an2)
{
run = 0;
Display_State(spa, del, run);
return;
}
if(an1 > an2)
{
an1--;
}
if(an1 < an2)
{
an1++;
}
if(del == 0)
{
PCA9685_Angle_270(pin, an1, 270);
Display_Angle(an1, an2);
}
if(del == 1)
{
PCA9685_Angle_270(pin, an2, 270);
an1 = an2;
run = 0;
Display_State(spa, del, run);
Display_Angle(an1, an2);
}
}
hi_void Engine_Init(hi_u16 a1, hi_u16 a2)
{
an1 = a1;
an2 = a2;
PCA9685_Angle_270(pin, an1, 270);
}
hi_u8 SE_Get_Pin(hi_void)
{
return pin;
}
hi_u8 SE_Get_Run(hi_void)
{
return run;
}
hi_u8 SE_Get_Spa(hi_void)
{
return spa;
}
hi_u8 SE_Get_Del(hi_void)
{
return del;
}
hi_u16 SE_Get_An1(hi_void)
{
return an1;
}
hi_u16 SE_Get_An2(hi_void)
{
return an2;
}
hi_void SE_Set_Run(hi_u8 r)
{
run = r;
}
hi_void SE_Set_An1(hi_u16 a)
{
an1 = a;
}
hi_void SE_Set_An2(hi_u16 a)
{
an2 = a;
}