2012-11-04 13:52:14 +08:00
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <mmsystem.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include "serial.h"
|
|
|
|
|
|
|
|
|
|
struct serial_int_rx serial_rx;
|
|
|
|
|
extern struct rt_device serial_device;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Handler for OSKey Thread
|
|
|
|
|
*/
|
|
|
|
|
static HANDLE OSKey_Thread;
|
|
|
|
|
static DWORD OSKey_ThreadID;
|
|
|
|
|
|
|
|
|
|
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
|
|
|
|
|
void rt_hw_usart_init(void)
|
|
|
|
|
{
|
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
|
/*
|
|
|
|
|
* create serial thread that revice key input from keyboard
|
|
|
|
|
*/
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
|
|
OSKey_Thread = CreateThread(NULL,
|
2012-12-27 16:26:35 +08:00
|
|
|
|
0,
|
|
|
|
|
(LPTHREAD_START_ROUTINE)ThreadforKeyGet,
|
|
|
|
|
0,
|
|
|
|
|
CREATE_SUSPENDED,
|
|
|
|
|
&OSKey_ThreadID);
|
|
|
|
|
if (OSKey_Thread == NULL)
|
2012-11-04 13:52:14 +08:00
|
|
|
|
{
|
|
|
|
|
//Display Error Message
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SetThreadPriority(OSKey_Thread,
|
|
|
|
|
THREAD_PRIORITY_NORMAL);
|
|
|
|
|
SetThreadPriorityBoost(OSKey_Thread,
|
|
|
|
|
TRUE);
|
|
|
|
|
SetThreadAffinityMask(OSKey_Thread,
|
|
|
|
|
0x01);
|
|
|
|
|
/*
|
|
|
|
|
* Start OS get key Thread
|
|
|
|
|
*/
|
|
|
|
|
ResumeThread(OSKey_Thread);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2012-12-27 16:26:35 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe04b
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe048
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe04d
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)<EFBFBD><EFBFBD> 0xe050
|
2012-11-04 13:52:14 +08:00
|
|
|
|
*/
|
|
|
|
|
static int savekey(unsigned char key)
|
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
|
/* save on rx buffer */
|
|
|
|
|
{
|
|
|
|
|
rt_base_t level;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
|
/* disable interrupt */
|
2012-11-04 13:52:14 +08:00
|
|
|
|
//<2F><>ʱ<EFBFBD>ر<EFBFBD><D8B1>жϣ<D0B6><CFA3><EFBFBD>ΪҪ<CEAA><D2AA><EFBFBD><EFBFBD>uart<72><74><EFBFBD>ݽṹ
|
2012-12-27 16:26:35 +08:00
|
|
|
|
level = rt_hw_interrupt_disable();
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
|
/* save character */
|
|
|
|
|
serial_rx.rx_buffer[serial_rx.save_index] = key;
|
|
|
|
|
serial_rx.save_index ++;
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>save_index<65>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE)
|
|
|
|
|
serial_rx.save_index = 0;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>save_index<78><D7B7><EFBFBD><EFBFBD>read_index<65><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>read_index<65><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD>
|
2012-12-27 16:26:35 +08:00
|
|
|
|
/* if the next position is read index, discard this 'read char' */
|
|
|
|
|
if (serial_rx.save_index == serial_rx.read_index)
|
|
|
|
|
{
|
|
|
|
|
serial_rx.read_index ++;
|
|
|
|
|
if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
|
|
|
|
serial_rx.read_index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
|
//uart<72><74><EFBFBD>ݽṹ<DDBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>ж<EFBFBD>
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* invoke callback */
|
|
|
|
|
if (serial_device.rx_indicate != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_size_t rx_length;
|
|
|
|
|
|
|
|
|
|
/* get rx length */
|
|
|
|
|
rx_length = serial_rx.read_index > serial_rx.save_index ?
|
|
|
|
|
SERIAL_RX_BUFFER_SIZE - serial_rx.read_index + serial_rx.save_index :
|
|
|
|
|
serial_rx.save_index - serial_rx.read_index;
|
|
|
|
|
|
|
|
|
|
serial_device.rx_indicate(&serial_device, rx_length);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
}
|
|
|
|
|
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
|
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
|
unsigned char key;
|
2012-11-04 13:52:14 +08:00
|
|
|
|
|
|
|
|
|
(void)lpParam; //prevent compiler warnings
|
|
|
|
|
|
2012-12-27 16:26:35 +08:00
|
|
|
|
for (;;)
|
2012-11-04 13:52:14 +08:00
|
|
|
|
{
|
2012-12-27 16:26:35 +08:00
|
|
|
|
key = _getch();//getchar();
|
|
|
|
|
if (key == 0xE0)
|
|
|
|
|
{
|
|
|
|
|
key = _getch();
|
|
|
|
|
|
|
|
|
|
if (key == 0x48) //up key , 0x1b 0x5b 0x41
|
|
|
|
|
{
|
|
|
|
|
savekey(0x1b);
|
|
|
|
|
savekey(0x5b);
|
|
|
|
|
savekey(0x41);
|
|
|
|
|
}
|
|
|
|
|
else if (key == 0x50)//0x1b 0x5b 0x42
|
|
|
|
|
{
|
|
|
|
|
savekey(0x1b);
|
|
|
|
|
savekey(0x5b);
|
|
|
|
|
savekey(0x42);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
savekey(key);
|
2012-11-04 13:52:14 +08:00
|
|
|
|
}
|
|
|
|
|
} /*** ThreadforKeyGet ***/
|