joycon-slave/main.c

237 lines
5.9 KiB
C

#include "Beep.h"
#include "STC15F2K60S2.H"
#include "adc.h"
#include "const.h"
#include "displayer.h"
#include "key.h"
#include "sys.h"
#include "uart1.h"
#include "uart2.h"
#include "universal_decode_table.h"
code unsigned long SysClock = 11059200; //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
char onlineStatus = 0; // 0: offline, 1: online, 2: trying handshake
char buf1[8], buf2[8] = {0}, match[1] = {PROTOCOL_HEADER}, deviceNum = 2, status;
void tryHandshake() // try handshaking with master
{
if (onlineStatus != 1) // already online, or not confirmed device num
{
return;
}
buf2[0] = PROTOCOL_HEADER;
buf2[1] = DEVICE_SPECIAL_PROTOCOL;
buf2[2] = KEY_REGISTER_SLAVE;
buf2[3] = KEY_ACTION_SLAVE_REQUEST;
buf2[4] = deviceNum;
Uart2Print(buf2, 8);
#ifdef DEBUG
Uart1Print(buf2, 8);
#endif
}
void registerHandshake() // receive handshake confirmation from master
{
if (onlineStatus != 1 ||
buf1[1] != DEVICE_SPECIAL_PROTOCOL ||
buf1[2] != KEY_REGISTER_SLAVE ||
buf1[3] != KEY_ACTION_SLAVE_OK ||
buf1[4] != deviceNum) // not confirmed device num or not proper message
{
return;
}
SetBeep(500, 10);
buf2[4] = 0x0;
onlineStatus = 2;
return;
}
void navEvent()
{
switch (onlineStatus)
{
case 0: // set device number
status = GetAdcNavAct(enumAdcNavKeyUp); // increase device number
if (status == enumKeyPress)
{
deviceNum++;
if (deviceNum > DEVICE_NUM_MAX)
{
deviceNum = DEVICE_NUM_MIN;
}
return;
}
status = GetAdcNavAct(enumAdcNavKeyDown); // reduce device number
if (status == enumKeyPress)
{
deviceNum--;
if (deviceNum < DEVICE_NUM_MIN)
{
deviceNum = DEVICE_NUM_MAX;
}
return;
}
status = GetAdcNavAct(enumAdcNavKeyCenter); // confirm device number
if (status == enumKeyPress)
{
onlineStatus = 1;
return;
}
break;
case 2: // send key event
buf2[0] = PROTOCOL_HEADER;
buf2[1] = deviceNum;
status = GetAdcNavAct(enumAdcNavKeyUp);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY_UP;
buf2[3] = KEY_ACTION_PRESS;
goto navSend;
case enumKeyRelease:
buf2[2] = KEY_UP;
buf2[3] = KEY_ACTION_RELEASE;
goto navSend;
}
status = GetAdcNavAct(enumAdcNavKeyDown);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY_DOWN;
buf2[3] = KEY_ACTION_PRESS;
goto navSend;
case enumKeyRelease:
buf2[2] = KEY_DOWN;
buf2[3] = KEY_ACTION_RELEASE;
goto navSend;
}
status = GetAdcNavAct(enumAdcNavKeyLeft);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY_LEFT;
buf2[3] = KEY_ACTION_PRESS;
goto navSend;
case enumKeyRelease:
buf2[2] = KEY_LEFT;
buf2[3] = KEY_ACTION_RELEASE;
goto navSend;
}
status = GetAdcNavAct(enumAdcNavKeyRight);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY_RIGHT;
buf2[3] = KEY_ACTION_PRESS;
goto navSend;
case enumKeyRelease:
buf2[2] = KEY_RIGHT;
buf2[3] = KEY_ACTION_RELEASE;
goto navSend;
}
status = GetAdcNavAct(enumAdcNavKeyCenter);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY_OK;
buf2[3] = KEY_ACTION_PRESS;
goto navSend;
case enumKeyRelease:
buf2[2] = KEY_OK;
buf2[3] = KEY_ACTION_RELEASE;
goto navSend;
}
status = GetAdcNavAct(enumAdcNavKey3);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY3;
buf2[3] = KEY_ACTION_PRESS;
goto navSend;
case enumKeyRelease:
buf2[2] = KEY3;
buf2[3] = KEY_ACTION_RELEASE;
goto navSend;
}
break;
}
navSend:
Uart2Print(buf2, 8);
#ifdef DEBUG
Uart1Print(buf2, 8);
#endif
return;
}
void displaySet()
{
Seg7Print(deviceNum, 0, 0, 0, 0, 0, 0, 0);
}
void keyEvent()
{
buf2[0] = PROTOCOL_HEADER;
buf2[1] = deviceNum;
status = GetKeyAct(enumKey1);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY1;
buf2[3] = KEY_ACTION_PRESS;
goto keySend;
case enumKeyRelease:
buf2[2] = KEY1;
buf2[3] = KEY_ACTION_RELEASE;
goto keySend;
}
status = GetKeyAct(enumKey2);
switch (status)
{
case enumKeyPress:
buf2[2] = KEY2;
buf2[3] = KEY_ACTION_PRESS;
goto keySend;
case enumKeyRelease:
buf2[2] = KEY2;
buf2[3] = KEY_ACTION_RELEASE;
goto keySend;
}
keySend:
Uart2Print(buf2, 8);
#ifdef DEBUG
Uart1Print(buf2, 8);
#endif
return;
}
void main()
{
MySTC_Init();
Uart2Init(4800, Uart2Usedfor485ModBus);
DisplayerInit();
BeepInit();
AdcInit(ADCexpEXT);
LedPrint(0);
SetDisplayerArea(0, 1);
Seg7Print(deviceNum, 0, 0, 0, 0, 0, 0, 0);
KeyInit();
SetUart2Rxd(buf1, 8, match, 1);
#ifdef DEBUG
Uart1Init(4800);
SetUart1Rxd(buf1, 8, match, 1);
SetEventCallBack(enumEventUart1Rxd, registerHandshake);
Uart1Print(buf2, 8);
#endif
SetEventCallBack(enumEventUart2Rxd, registerHandshake);
SetEventCallBack(enumEventKey, keyEvent);
SetEventCallBack(enumEventNav, navEvent);
SetEventCallBack(enumEventSys100mS, displaySet);
SetEventCallBack(enumEventSys1S, tryHandshake);
while (1)
{
MySTC_OS();
}
}