#include "header.h"
struct __filter
{
int state;
int result;
int timecount;
int lastoutput;
}filter={0,0,0,0};
#define TIMECOUNT 10
int Filter(unsigned char (*bt)(void), struct __filter *p)
{
switch(p->state)
{
case 0:
if(bt()==1)//bt=pass function to function
{
p->timecount=0;
p->state++;
}
p->result=0;
break;
case 1:
if(bt()==0)
{
p->state--;
}
if(++p->timecount>=TIMECOUNT)
{
p->state++;
}
p->result=0;
break;
case 2:
if(bt()==0)
{
p->state++;
p->timecount=0;
}
p->result=1;
break;
case 3:
if(bt()==1)
{
p->state--;
}
if(++p->timecount>=TIMECOUNT)
{
p->state=0;
}
p->result=1;
break;
default:
p->state=0;//refault state to 0
break;
}
return p->result;
}
//Filter(unsigned char (*bt)(void), struct __filter *p
int Toggle(int (*f)(unsigned char(*)(void),struct __filter *),
unsigned char (*bt)(void),
struct __filter *p,
int *result)
{
int output;
output=f(bt,p);
if(output!=p->lastoutput)
{
p->lastoutput=output;
printf("\r\nToggle: %d",output);
*result=output;
return 1;
}
return 0;
}
//for task call
void TestFilter(void *parameter)
{
int output;
if(Toggle(Filter,ButtonPHY_Output,parameter,&output))
{
printf("\r\noutput: %d",output);
}
}
#define DATA_MAX 128
struct __infix
{
unsigned char item[DATA_MAX];
int index;
}infix={"",0};
struct __stackop
{
int item[DATA_MAX];
int sp;
}stackop={{0},0};
void StackOpInit(void)
{
stackop.sp=0;
}
int StackOpIsEmpty(void)
{
return stackop.sp==0;
}
int StackOpIsFull(void)
{
return stackop.sp >= DATA_MAX;
}
int StackOpPush(int newdata)
{
if(StackOpIsFull())
return 0;
stackop.item[stackop.sp]=newdata;
stackop.sp++;
return 1;
}
int StackOpPop(int *output)
{
if(StackOpIsEmpty())
return 0;
stackop.sp--;
*output=stackop.item[stackop.sp];
return 1;
}
int StackOpTop(void)
{
if(StackOpIsEmpty())
return 0;
return stackop.item[stackop.sp-1];
}
int IsOperator(int data)
{
if(data=='+')
return 1;
if(data=='-')
return 1;
if(data=='*')
return 1;
if(data=='/')
return 1;
if(data=='^')
return 1;
//if(data=='log10')
// return 1;
return 0;
}
int Priority(int data)
{
if(data=='+')
return 1;
if(data=='-')
return 1;
if(data=='*')
return 2;
if(data=='/')
return 2;
if(data=='^')
return 3;
//if(data=='log10')
// return 4;
return 0;
}
struct __postfix
{
unsigned char item[DATA_MAX];
int index;
}postfix={"",0};
void InfixToPostfix(void)
{
int i;
int P;
int data;
// initial
StackOpInit();
postfix.index=0;
for(i=0;i<infix.index;i++)
{
P=infix.item[i];
if(IsOperator(P))
{
while(Priority(StackOpTop()) >= Priority(P))
{
StackOpPop(&data);
postfix.item[postfix.index]=data;
postfix.index++;
}
StackOpPush(P);
}
else
if(P=='(')
{
StackOpPush(P);
}
else
if(P==')')
{
while(StackOpPop(&data))
{
if(data=='(')
break;
postfix.item[postfix.index]=data;
postfix.index++;
}
}
else
{
//operand
postfix.item[postfix.index]=P;
postfix.index++;
}
}
while(StackOpPop(&data))
{
postfix.item[postfix.index]=data;
postfix.index++;
}
//end buffer/string
postfix.item[postfix.index]='\0';
}
int CalculatePostfix(void)
{
int i;
int P;
int data;
int result;
// type: int
StackOpInit();
for (i=0;i<postfix.index;i++)
{
P=postfix.item[i];
if(IsOperator(P))
{//found operator
int operand1,operand2;
// 1 Operator 2 Operand
// 1 Operator 1 Operand -> final project
StackOpPop(&operand2);
StackOpPop(&operand1);
switch(P)
{
case'+': data=operand1+operand2;
break;
case'-': data=operand1-operand2;
break;
case'*': data=operand1*operand2;
break;
case'/': data=operand1/operand2;
break;
case'^': data=pow(operand1,operand2);
break;
default: data=0;break;
}
StackOpPush(data);
}
else
{//found operand
char buffer[10];
buffer[0]=P;
buffer[1]='\0';
StackOpPush(atoi(buffer));
}
}
StackOpPop(&result);
return result;
}
void Calculator(void *parameter)
{
unsigned char datain;
if(getchar(&datain))
{
switch(datain)
{
//enter
case 13:
if(infix.index>0)
{
printf("\r\ninfix: %s",infix.item);
InfixToPostfix();
printf("\r\npostfix: %s",postfix.item);
printf("\r\nAns: %d",CalculatePostfix());
}
infix.index=0;//clear index for new recive
printf("\r\nz:\\> ");
break;
//backspace
case 8:
//echo
//step
if(infix.index>0)
{
printf("%c",8);
printf(" ");
printf("%c",8);
infix.index--;
}
break;
default:
//echo
printf("%c",datain);
//save
infix.item[infix.index]=datain;
infix.index++;
//delete item
infix.item[infix.index]='\0';
break;
}
}
}
void main(void)
{
//-------------------------------------------------------
//- driver initial
LEDPhy_Init();
ButtonPHY_Init();
//- end driver initial
//-------------------------------------------------------
BaseTimeInit();
Sci1Init();
AdcInit();
DacInit();
//- app with task
BoardMessage();
TaskInit();
LedInit();
//create task
TaskCreate(TestFilter,&filter,10,"Filter");
TaskCreate(Calculator,0,10,"Cal"); //want to repeat must to create T
//- task monitor
TaskMonitor();
printf("\r\nCalculator");
for(;;)
{
TaskRun();
}
}
void BoardMessage(void)
{
//- wellcom message
printf(STR_PARTITION);
printf(VERSION);
printf(SW_VERSION_STR);
printf(MODIFY_BY);
printf(COPY_BY);
printf("\r\n");
}