⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dm.c

📁 iar公司的s3c44b0x评估板的源程序
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>

#include "44b0.h"
#include "dm.h"

#define MAXBUF 256
char sText[MAXBUF];

// Command Constants
#define WRITE_BUFFER_SIZE   256
#define CMD_STRING_SIZE     64

// Serial configuration structure
static UartCfgT SystemUartCfg =
{
    115200
};

// DM info structure
DM_SystemInfo_T DM_System;

int KeyButtonHit (int *key)
{
	int bhit = 1;

	switch (rPDATG & 0xf0)
	{
	case 0xE0:
	        DM_WaitMs(30);
			while (rPDATG & 0xf0 == 0xE0);
			*key = DMKEY_UP;	    //GPG4
			break;
	case 0xD0:
	        DM_WaitMs(30);
			while (rPDATG & 0xf0 == 0xD0);
			*key = DMKEY_DOWN;      //GPG5
			break;
	case 0xB0:
	        DM_WaitMs(30);
			while (rPDATG & 0xf0 == 0xB0);
			*key = DMKEY_ACTION;	//GPG6
			break;
	case 0x70:
	        DM_WaitMs(30);
			while (rPDATG & 0xf0 == 0x70);
			*key = DMKEY_BACK;		//GPG7
			break;
	default:
	    	bhit = 0;
		    break;
	}

	return bhit;
}

void DM_Printf(char * fmt, ...)
{
    char buffer[256];
    va_list ap;

    va_start(ap,fmt);
    vsprintf(buffer,fmt,ap);
    va_end(ap);

    DM_PrintSerial(buffer);
    return;
}

static void drawMenu(DM_Menu_T * menuP,int selItemRow)
{
    int i;
    int line = 0;

    // Menu Title.
    DM_PrintSerial(menuP->banner);

    // Skip line.
    line += 1;
    for (i=0; i < 16; i++)
    {
        int num = menuP->menuList[i].selectionChar & 0xff;
        char symbol = '*';
        if (menuP->menuList[i].displayedString == NULL)
            break;

        // Display the menu item.
		if (selItemRow == i)//selected menu item
		{
	        sprintf(sText,"*%X=%-s",num,menuP->menuList[i].displayedString);
		}
		else {
			symbol = ' ';
		}
        sprintf(sText,"%c%X=%-s",symbol,num,menuP->menuList[i].displayedString);
        
        DM_PrintSerial(sText);

        // Goto next line
        line++;
    }
}

static int findMenu(int testNum, DM_MenuItem_T * menuListP)
{
    int i = 0;

    do {
        if ((menuListP[i].selectionChar & 0xff) == testNum)
            return i;
    } while (menuListP[i++].displayedString != 0) ;
    return -1;
}

void DM_DoMenu(void * menuP,char * dummyP)
{
	short int menuItemCount=0;
	int key,index;
    static int menulevel=0;
	DM_MenuItem_T *menulistP = ((DM_Menu_T *)menuP)->menuList;

    while(menulistP[menuItemCount].displayedString != 0)
        menuItemCount++;

	index = 0;
   	menulevel ++;
	do
    {
        if (index < 0 || index >= menuItemCount) index = 0;
        
		drawMenu((DM_Menu_T *)menuP,index);

		//Wait for any key pressed
waitkey:
		key = DM_GetKey();
		
		if (ISCONTROLKEY(key))
		{
			if (key == DMKEY_UP)
			{
				index --;
				if (index < 0)
				{
					index = menuItemCount -1;
				}
				continue;
			}
			else if (key == DMKEY_DOWN)
			{
				index ++;
				if (index >= menuItemCount) 
				{
					index = 0;
				}
				continue;
			}
			else if (key == DMKEY_ACTION)
			{
	            ;
			}
			else if (key == DMKEY_BACK)
			{
             //to upper level menu
	         if (menulevel > 1)
				break;
			 else goto waitkey;	
			}
			else if (key == DMKEY_UNKNOWN)
	      	{
	        	goto waitkey;
	      	}
		}
		else
		{
			//user activate the menu handler thru selection char(e.g. press number)
			index = findMenu(DM_TranslateKey(key),menulistP);
			if (index == -1) goto waitkey;	//invalid selection, just do nothing
		}

    	if (menulistP[index].func)
        	menulistP[index].func(menulistP[index].arg,menulistP[index].cmdParamString);
    	else
    	{
            break;//exit the current menu
	    }

	}while(1);

   menulevel --;
}

void DM_Message(char * msg)
{
    DM_PrintSerial(msg);
}

int DM_InitSystem (void)
{
    // Open the timer device
    DM_System.timerP = &wdt;

    // Open the serial device
    DM_System.serialP = &Uart0;
    DM_System.serialP->setupUartFnP(DM_System.serialP,&SystemUartCfg);

    return 0;
}

int DM_TranslateKey(char key)
{
    if (key >= 'A' && key <= 'F')
    {
        return(key - 'A' + 10);
    }
    else
    {
        return(key - '0');
    }
}

int DM_GetKey(void)
{
    int key;
    UartContextT * ctxP = DM_System.serialP;

    ctxP->clearRxUartFnP(ctxP);

    while (1)
    {
		if (ctxP->checkUartFnP(ctxP,(char *)&key))
			break; 
    	DM_WaitMs(200);
    		//check key button first
    	if (KeyButtonHit(&key))
    		return key;
    }

    // Translate the key
    key &= 0xff;
    if (
        (
            key >= '0' && key <= '9'
        ) || (
            key >= 'A' && key <= 'F'
        ) || key == 0x08 || key == 0x0d
    ) return key;
    else if (key >= 'a' && key <= 'f')
        return key & ~0x20;

    // We didn't translate the key.
    return key;
}

void DM_PrintSerial(char *s)
{
    UartContextT * ctxP = DM_System.serialP;
    int len;
    char buff[WRITE_BUFFER_SIZE+2];

    len = strlen(s);
    if (len > WRITE_BUFFER_SIZE)
      len = WRITE_BUFFER_SIZE;
    strcpy(buff, s);
    buff[len++] = '\n';
    buff[len++] = '\r';

    ctxP->writeUartFnP(ctxP, buff, len);
}

static void parseargs(char *argstr, int *argc_p, char **argv, char** resid)
{
	int argc = 0;
	char c;
	enum ParseState stackedState;
	enum ParseState lastState = PS_WHITESPACE;

	/* tokenize the argstr */
	while ((c = *argstr) != 0) {
		enum ParseState newState;

		if (c == ';' && lastState != PS_STRING && lastState != PS_ESCAPE)
			break;

		if (lastState == PS_ESCAPE) {
			newState = stackedState;
		} else if (lastState == PS_STRING) {
			if (c == '"') {
				newState = PS_WHITESPACE;
				*argstr = 0;
			} else {
				newState = PS_STRING;
			}
		} else if ((c == ' ') || (c == '\t')) {
			/* whitespace character */
			*argstr = 0;
			newState = PS_WHITESPACE;
		} else if (c == '"') {
			newState = PS_STRING;
			*argstr++ = 0;
			argv[argc++] = argstr;
		} else if (c == '\\') {
			stackedState = lastState;
			newState = PS_ESCAPE;
		} else {
			/* token */
			if (lastState == PS_WHITESPACE) {
				argv[argc++] = argstr;
			}
			newState = PS_TOKEN;
		}

		lastState = newState;
		argstr++;
	}

	argv[argc] = NULL;
	if (argc_p != NULL)
		*argc_p = argc;

	if (*argstr == ';') {
		*argstr++ = '\0';
	}
	*resid = argstr;
}

void DM_TranslateMenuToCmd (void *arg, char *inputStringP)
{
    DM_CommandFunc_T *cmdFunc = (DM_CommandFunc_T *)arg;
    char buff[CMD_STRING_SIZE], *buffP;
	int argc;
	char *argv[16];
	char *resid;

    buffP = buff;
    buff[CMD_STRING_SIZE - 1] = '\0';

	do
	{
		memset(argv, 0, sizeof(argv));
		parseargs(buffP, &argc, argv, &resid);
		if (argc > 0)
		{
            cmdFunc(argc, (const char **)argv);
		}
		buffP = resid;
	} while (*buffP);

    return;
}

void DM_WaitUs (unsigned int usVal)
{
	WDTContextT *ctxP = DM_System.timerP;
	unsigned int ticks;

	ctxP->setup_fnp(ctxP, 1, WDTDIVIDER16);

	ticks = (UINT32)(usVal * (MCLK/1e6L)/2/16 + 0.5);

	while (ticks > 0xFF00)
	{
		ctxP->wait_fnp(ctxP, 0xFF00);
		ticks -= 0xFF00;
	}

	ctxP->wait_fnp(ctxP, ticks);

    return;
}

void DM_WaitMs (unsigned int msVal)
{
	WDTContextT *ctxP = DM_System.timerP;
	unsigned int ticks;

	ctxP->setup_fnp(ctxP, 249, WDTDIVIDER128);

	ticks = (UINT32)(msVal * (MCLK/1000)/250/128 + 0.5);

	while (ticks > 0xFF00)
	{
		ctxP->wait_fnp(ctxP, 0xFF00);
		ticks -= 0xFF00;
	}

	ctxP->wait_fnp(ctxP, ticks);

    return;
}

void DM_WaitS(unsigned int sVal)
{
	DM_WaitMs(sVal * 1000);
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -