UP 模拟器 加入key的模拟支持

main
JiXieShi 2024-06-22 22:51:58 +08:00
parent 0c7c6bb1e9
commit 5876461531
9 changed files with 201 additions and 35 deletions

View File

@ -1,17 +1,17 @@
#include <stdio.h> #include <stdio.h>
#include "key.h" #include "key.h"
#include "log.h" #include "log.h"
#include "tool.h"
#include "t_key.h" #include "t_key.h"
#include <windows.h> #include <windows.h>
#include <process.h>
#include "sim_key.h"
uint8_t Key_Pin(uint8_t l) { uint8_t Key_Pin(uint8_t l) {
uint8_t k = rand() % 2; if (SIM_Key_Scan() == SIM_KEY_UP) {
// LOGT("KEY", "P:%d-%d", l,k); // LOGT("SCAN","ID:%d-UP",l);
return k; return 1;
} else return 0;
} }
void Key_Call(Key_t *key) { void Key_Call(Key_t *key) {
switch (key->event) { switch (key->event) {
case KEY_PRESS_DOWN: case KEY_PRESS_DOWN:
@ -39,18 +39,33 @@ void Key_Call(Key_t *key) {
} }
void Test_Key() { void Test_Key() {
Key_t k1, k2, k3, k4, k5, k6, ks;
key_init(&k1, SIM_Key_UP, 1, SIM_KEY_UP);
key_init(&k2, SIM_Key_DOWN, 1, SIM_KEY_DOWN);
key_init(&k3, SIM_Key_LEFT, 1, SIM_KEY_LEFT);
key_init(&k4, SIM_Key_RIGHT, 1, SIM_KEY_RIGHT);
key_init(&k5, SIM_Key_ENABLE, 1, SIM_KEY_ENABLE);
key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET);
// key_init(&ks,Key_Pin,1,7);
Key_t k1, k2;
key_init(&k1, Key_Pin, 1, 0);
key_init(&k2, Key_Pin, 1, 1);
key_attach(&k1, KEY_ALL_EVENT, Key_Call); key_attach(&k1, KEY_ALL_EVENT, Key_Call);
key_attach(&k2, KEY_ALL_EVENT, Key_Call); key_attach(&k2, KEY_ALL_EVENT, Key_Call);
key_attach(&k3, KEY_ALL_EVENT, Key_Call);
key_attach(&k4, KEY_ALL_EVENT, Key_Call);
key_attach(&k5, KEY_ALL_EVENT, Key_Call);
key_attach(&k6, KEY_ALL_EVENT, Key_Call);
// key_attach(&ks, KEY_ALL_EVENT, Key_Call);
key_start(&k1); key_start(&k1);
key_start(&k2); key_start(&k2);
key_start(&k3);
key_start(&k4);
key_start(&k5);
key_start(&k6);
// key_start(&ks);
while (1) { while (1) {
// 每5ms调用一次key_ticks函数 // 每5ms调用一次key_ticks函数
key_ticks(); key_ticks();
Sleep(5); // 使用Windows平台的Sleep函数进行5ms延时 Sleep(5);
} }
} }

View File

@ -7,16 +7,16 @@
#include <windows.h> #include <windows.h>
uint8_t Cmd(uint8_t *data, size_t l) { uint8_t Cmd(uint8_t *data, size_t l) {
Buf_Print("Cmd", data, l, 16); // Buf_Print("Cmd", data, l, 16);
} }
uint8_t Data(uint8_t *data, size_t l) { uint8_t Data(uint8_t *data, size_t l) {
Buf_Print("Data", data, l, 16); // Buf_Print("Data", data, l, 16);
} }
void Refresh_Call(OLED_T *dev) { void Refresh_Call(OLED_T *dev) {
LOGT("OLED", "CALL"); // LOGT("OLED", "CALL");
Buf_Print("Buf", dev->buf, dev->width * (dev->height / 8), 128); // Buf_Print("Buf", dev->buf, dev->width * (dev->height / 8), 128);
SIM_OLED_DrawFromBuffer(dev->buf, dev->width, dev->height / 8); SIM_OLED_DrawFromBuffer(dev->buf, dev->width, dev->height / 8);
} }
@ -35,14 +35,18 @@ void Test_OLED() {
}; };
OLED_Init(&oled); OLED_Init(&oled);
OLED_CLS(&oled); OLED_CLS(&oled);
OLED_DrawRect(&oled, 0, 0, 127, 63); // OLED_DrawRect(&oled, 0, 0, 127, 63);
OLED_ShowString(&oled, 12, 24, "Hello JXS", 16); OLED_ShowString(&oled, 32, 0, "Main Page", 12);
OLED_ShowString(&oled, 36, 12, "1.PageA", 12);
OLED_ShowString(&oled, 36, 24, "2.PageB", 12);
OLED_ShowString(&oled, 36, 36, "3.PageC", 12);
SIM_OLED_START(); SIM_OLED_START();
int s = 0; int s = 0;
char buf[30]; char buf[30];
while (s < 10) {
while (1) {
sprintf(buf, "DATA:%d", s); sprintf(buf, "DATA:%d", s);
OLED_ShowString(&oled, 2, 2, buf, 12); OLED_ShowString(&oled, 2, 51, buf, 12);
OLED_Refresh(&oled); OLED_Refresh(&oled);
s++; s++;
Sleep(500); Sleep(500);

View File

@ -1,4 +1,4 @@
#ifndef HW_LIB_KEY_H #ifndef HW_LIB_SIM_KEY_H
#define HW_LIB_KEY_H #define HW_LIB_KEY_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -8,7 +8,7 @@ extern "C" {
#include <string.h> #include <string.h>
#define KEY_TICKS_INTERVAL 5 // 定时器间隔时间,单位为毫秒 #define KEY_TICKS_INTERVAL 5 // 定时器间隔时间,单位为毫秒
#define DEBOUNCE_TICKS 3 // 按键去抖动计数阈值最大为70 ~ 7 #define DEBOUNCE_TICKS 0 // 按键去抖动计数阈值最大为70 ~ 7
#define SHORT_TICKS (300 / KEY_TICKS_INTERVAL) // 短按阈值,单位为定时器间隔的倍数 #define SHORT_TICKS (300 / KEY_TICKS_INTERVAL) // 短按阈值,单位为定时器间隔的倍数
#define LONG_TICKS (1000 / KEY_TICKS_INTERVAL) // 长按阈值,单位为定时器间隔的倍数 #define LONG_TICKS (1000 / KEY_TICKS_INTERVAL) // 长按阈值,单位为定时器间隔的倍数
#define KEY_STOP_FREE 1 // 按键停止释放状态标识 #define KEY_STOP_FREE 1 // 按键停止释放状态标识
@ -99,4 +99,4 @@ void key_ticks(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif //HW_LIB_KEY_H #endif //HW_LIB_SIM_KEY_H

27
main.c
View File

@ -10,23 +10,24 @@
#include "t_oled.h" #include "t_oled.h"
#include "tool.h" #include "tool.h"
#include <time.h> #include <time.h>
#include <windows.h>
#include <process.h>
int main() { int main() {
srand((unsigned) time(NULL)); srand((unsigned) time(NULL));
int i = 1; // int i = 1;
POUT((++i) + (++i)); // POUT((++i) + (++i));
//
// char str[] = "123.456";
// float result = Str2Float(str);
// printf("Result: %.3f\n", result);
// Test_RunTime("SPI", Test_spi);
// Test_RunTime("IIC", Test_iic);
// Test_RunTime("ArgPase", Test_argpase);
// Test_RunTime("List", Test_List);
// Test_RunTime("Key", Test_Key);
char str[] = "123.456"; // Test_RunTime("Queue", Test_Queue);
float result = Str2Float(str);
printf("Result: %.3f\n", result);
Test_RunTime("SPI", Test_spi);
Test_RunTime("IIC", Test_iic);
Test_RunTime("ArgPase", Test_argpase);
Test_RunTime("List", Test_List);
Test_RunTime("Queue", Test_Queue);
// Test_RunTime("Task", Test_task); // Test_RunTime("Task", Test_task);
Test_RunTime("OLED", Test_OLED); Test_RunTime("OLED", Test_OLED);
// Test_RunTime("Key", Test_Key);
return 0; return 0;
} }

83
sim/key/key.cpp Normal file
View File

@ -0,0 +1,83 @@
#include "sim_key.h"
#include "log.h"
#include<easyx.h>
#include<conio.h>
uint8_t SIM_Key_Scan() {
uint8_t key = 0;
if (_kbhit()) {
char keys = _getch();
switch (keys) {
//上键
case 72:
case 'w':
case 'W':
key = SIM_KEY_UP;
break;
case 80:
case 's':
case 'S':
key = SIM_KEY_DOWN;
break;
//左键
case 75:
case 'a':
case 'A':
key = SIM_KEY_LEFT;
break;
//右键
case 77:
case 'd':
case 'D':
key = SIM_KEY_RIGHT;
break;
case 'q':
case 'Q':
key = SIM_KEY_SET;
break;
case 'e':
case 'E':
key = SIM_KEY_ENABLE;
break;
case 'r':
case 'R':
key = SIM_KEY_RESET;
break;
}
}
return key;
}
uint8_t SIM_Key_UP(uint8_t) {
return GetAsyncKeyState(VK_UP);
}
uint8_t SIM_Key_DOWN(uint8_t l) {
// uint8_t k=GetAsyncKeyState(VK_DOWN);
// LOGT("SIM","KEYID:%d-DOWN:%d",l,k);
return GetAsyncKeyState(VK_DOWN);
}
uint8_t SIM_Key_LEFT(uint8_t) {
return GetAsyncKeyState(VK_LEFT);
}
uint8_t SIM_Key_RIGHT(uint8_t) {
return GetAsyncKeyState(VK_RIGHT);
}
uint8_t SIM_Key_ENABLE(uint8_t) {
return GetAsyncKeyState(VK_RETURN);
}
uint8_t SIM_Key_SET(uint8_t) {
return GetAsyncKeyState(VK_RSHIFT);
}
uint8_t SIM_SIM_Key_RESET(uint8_t) {
return GetAsyncKeyState(VK_END);
}

36
sim/key/sim_key.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef HW_LIB_SIM_KEY_H
#define HW_LIB_SIM_KEY_H
#ifdef __cplusplus
extern "C" {
#endif
#include "stdint.h"
typedef enum Key_map {
SIM_KEY_UP = 1,
SIM_KEY_DOWN,
SIM_KEY_LEFT,
SIM_KEY_RIGHT,
SIM_KEY_ENABLE,
SIM_KEY_SET,
SIM_KEY_RESET
} Key_Map_t;
uint8_t SIM_Key_Scan();
uint8_t SIM_Key_UP(uint8_t l);
uint8_t SIM_Key_DOWN(uint8_t l);
uint8_t SIM_Key_LEFT(uint8_t l);
uint8_t SIM_Key_RIGHT(uint8_t l);
uint8_t SIM_Key_ENABLE(uint8_t l);
uint8_t SIM_Key_SET(uint8_t l);
uint8_t SIM_Key_RESET(uint8_t l);
#ifdef __cplusplus
}
#endif
#endif //HW_LIB_SIM_KEY_H

View File

@ -37,6 +37,7 @@ void drawOledPixel(int oledX, int oledY) {
void SIM_OLED_DrawFromBuffer(uint8_t *buf, uint8_t width, uint8_t height) { void SIM_OLED_DrawFromBuffer(uint8_t *buf, uint8_t width, uint8_t height) {
BeginBatchDraw();
cleardevice(); cleardevice();
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
@ -47,6 +48,7 @@ void SIM_OLED_DrawFromBuffer(uint8_t *buf, uint8_t width, uint8_t height) {
} }
} }
} }
EndBatchDraw();
} }

14
sim/thread/sim_thread.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef HW_LIB_SIM_THREAD_H
#define HW_LIB_SIM_THREAD_H
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*Thread_Func_t)(void *pArg);
void ThreadCreat(Thread_Func_t func, unsigned int stackSize, void *pArg);
void ThreadStop();
#ifdef __cplusplus
}
#endif
#endif //HW_LIB_SIM_THREAD_H

11
sim/thread/thread.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "sim_thread.h"
#include <windows.h>
#include <process.h>
void ThreadCreat(Thread_Func_t func, unsigned int stackSize, void *pArg) {
_beginthread(func, stackSize, pArg);
}
void ThreadStop() {
_endthread();
}