/* ************************************************************************** */
/* Userif.c - User interface (UI) */
/* -------------------------------------------------------------------------- */
/* Written By: Raising Technology Co., Ltd. */
/* ************************************************************************** */
#include "MyApp.h"
#include "nRF24L01.h"
#include "Userif.h"
#include "ieep.h"
//==============================================================================
#pragma udata access accessram
//Key Flags & related variables -----------------------------------------------
near union {
BYTE value;
struct {
unsigned KeyPress:1; //when key is pressed, this flag will be set.
unsigned KeyRepeat:1; //when key is pressed and held more than a specified time, this flag will be set.
unsigned KeyDown:1; //Sets to 1 when the user depresses a key. It repeats while the user keeps the key depressed.
unsigned KeyUp:1; //Sets to 1 when the user releases a key.
unsigned Bit4:1;
unsigned Bit5:1;
unsigned Bit6:1;
unsigned Bit7:1;
};
} KeyFlags;
near BYTE KeyDowningCounter; //for repeat key control
near BYTE KeyCode; //key code for recognizing which key is pressed
#pragma udata
//===== Macros for cursor control ====================================================
#define SetAddrSettingCursor(index) SetCursor(6+(index/2)*3+(index&1),2)
#define SetIDSettingCursor(index) SetCursor(6+(index/2)*3+(index&1),1)
//===== Global Variables ===========================================================
static BYTE State; //Current UI state (UI = user interface)
static BYTE PrevState=0xff; //Previous UI State
static BYTE SettingNo=0; //to select an RF parameter for setting. --> 0:Device ID, 1:RF Channel, 2:TX Address
static BYTE SettingItemIndex = 0; //the setting index of an selected item(RF parameter).
static BYTE RFSettingChanged = 0; //if RF parameters have changed, set RFSettingChanged = 1
//================================================================================
//==== External functions ========================================================
//the following functions are put in module's source code. (Ex.) SHT2X.c
extern void DisplayMainMenu(void); //for Main menu display
extern void NormalMode_KEY_A(void); //when KEY_A was pressed in STATE_NORMAL_MODE, call this function.
extern void NormalMode_KEY_B(void); //when KEY_B was pressed in STATE_NORMAL_MODE, call this function.
extern void NormalMode_KEY_C(void); //when KEY_C was pressed in STATE_NORMAL_MODE, call this function.
extern void NormalMode_KEY_D(void); //when KEY_D was pressed in STATE_NORMAL_MODE, call this function.
extern void Return2NormalMode(void); //when KEY_D was pressed in STATE_SETTING_MODE, call this function.
//================================================================================
// ========= Program description ==========================================================
// This program manages user interface which inlcudes key operation and LCD display.
// For key operation, it uses a State Machine facility to manage all the key events.
// For LCD display, it uses all the APIs in LCM.c to display the desired information
// on LCD display window.
//
// Reagrding State Machine, there are several states as below.
// 有關於 State Machine 的運作, 概述如下:
// 系統開機時,呼叫StateInit()以完成State Machine機制初始化,並進入預設的State(通常
// 是STATE_NORMAL_MODE).
// 在進入某一State時,該State相對應的初始化程式會先被執行,再執行對應的Process.
// 例如執行 NormalModeProc()之前,會先執行NormalModeInit(),其餘依此類推.此一機制是
// 透過呼叫 ChangeState(...) 來實現的.
// 由於同一按鍵在不同的情況下,會有不同的處理方式,在State Machine的架構下,不同的情況
// 會有不同的State值,按鍵輸入各有該State所屬的Process負責處理,因此大大簡化了按鍵處理
// 程式的複雜度.
// 以下是各State簡要說明:
// STATE_NORMAL_MODE :
// 初始化程序: NormalModeInit(), 處理程序(Process):NormalModeProc()
// (1)開機時,如果所有的 RF 參數都已於先前設定完成,會進入這個狀態(State).
// (2)離開 RF 參數設定狀態(State)(即STATE_SETTING_MODE)時, 按KEY_D鍵離開,
// 會進入這個狀態(State).
// (3)大致上各感測模組都會在這個狀態(State)下進行各項操作.
// STATE_SETTING_MODE:
// 初始化程序: SettingModeInit(), 處理程序(Process):SettingModeProc()
// 要設定各項 RF 參數時,都須進入至在此狀態(State)下進行操作. 如果開機時,
// RF參數尚未設定完成,系統直接進入至在此狀態(State),以讓使用者先行設定這些
// 參數,再進行其他操作,這個狀況通常會發生在MCU內部EEPROM是空白時.
// STATE_SET_ID :
// 初始化程序: SetIDInit(), 處理程序(Process):SetIDProc()
// 設定 Device ID 時,進入至在此狀態(State)下進行操作.
// STATE_SET_RF_CHANNEL :
// 初始化程序: SetRFChannelInit(), 處理程序(Process):SetRFChannelProc()
// 設定 RF Channel 時,進入至在此狀態(State)下進行操作.
// STATE_SET_TX_ADDRESS :
// 初始化程序: SetTXAddressInit(), 處理程序(Process):SetTXAddressProc()
// 設定 RF TX Address 時,進入至在此狀態(State)下進行操作.
// STATE_SET_RX_ADDRESS :
// 初始化程序: SetRXAddressInit(), 處理程序(Process):SetrXAddressProc()
// 設定 RF RX Pipe0~5 Address 時,進入至在此狀態(State)下進行操作.
#if 0
//================================================================================
// Function : DisplayDeviceName
// Description: Display device name on LCD
// Input : None
// Return : None
//================================================================================
void DisplayDeviceName(void)
{
SetCursor(4,0);
DspStrROM(Dev_GetDeviceName(MyID[0]));
}
#endif
//================================================================================
// Function : NormalModeInit
// Description: Initialization function for STATE_NORMAL_MODE state
// While UI state enter into STATE_NORMAL_MODE, this function will be
// called first to do state initilization.
// Input : None
// Return : None
//================================================================================
void NormalModeInit(void)
{
DisplayMainMenu(); //Display UI main menu
if (RFSettingChanged) {
L01_TX_Mode(); //re-configure RF module to TX mode, becuase RF parameters have been changed.
RFSettingChanged = 0;
}
}
//================================================================================
// Function : NormalModeProc
// Description: Key event process for STATE_NORMAL_MODE state
// Input : None
// Return : None
//================================================================================
void NormalModeProc(void)
{
if (KeyFlags.KeyRepeat==1) return; //don't accept repeat key
//if (KeyFlags.KeyRepeat==1 && KeyCode!=KEY_UP && KeyCode!=KEY_DOWN) return;
switch (KeyCode)
{
case KEY_A:
NormalMode_KEY_A(); //this fucntion process KEY_A event
break;
case KEY_B:
NormalMode_KEY_B(); //this fucntion process KEY_B event
break;
case KEY_C:
NormalMode_KEY_C(); //this fucntion process KEY_C event
break;
case KEY_D:
NormalMode_KEY_D(); //this fucntion process KEY_D event
break;
}
}
//================================================================================
// Function : DisplaySettingArrow
// Description: Display an arrow to indicate which item is setting now.
// Input : ArrowOn 0:clear the arrow 1:display the arrow
// Return : None
//================================================================================
void DisplaySettingArrow(BYTE ArrowOn)
{
if (SettingNo==0) SetCursor(0,1); //set cursor position for setting Device ID
else if (SettingNo==1) SetCursor(13,1); //set cursor position for setting RF Channel
else SetCursor(0,2);