Knowledge Base Nr: 00123 BC440_set.c - http://www.swe-kaiser.de

Downloads:

Beck Steuerung BC440-WEB Beispielprogramm

  
/****************************************************************************
browser request e.g. http://192.168.200.8/io or http://192.168.200.8/io?01

If the browserrequest is send without any arguments, we only send the IO status
Possible arguments are:
00 means reset DK40 Output 0
01 set DK40 Output 0
...
61 means set DK40 Output 6
****************************************************************************/

#include <DOS.H>
#include <STDIO.H>
#include <STRING.H>
//needed for sleep call
#include "TCPIPAPI.H"
//cgi types, constants, ...
#include "CGI.H"
//#include "rtos.h"
#include "hwapi.h"

//needed interrupt vectors
#define TCPIPVECT 0xAC
#define CGIVECT 0xAB

/***************************************************************************
the name of the page for the browserrequest
e.g. http://192.168.200.5/io?11
****************************************************************************/
char * HtmlPageName = "io";

CGI_Entry example;

char * HtmlPage = "<HTML><HEAD><TITLE>BC440</TITLE></HEAD><BODY>"
"<P>E0:%02X E1:%02X A0:%02X %s</P>"
"</BODY></HTML>";

// the value that we write to address 0x602 is used for several functions
// since we can not read the current value and modify a few bits
// we must save the byte we write
static unsigned char val601; // the value written to address 0x601
static unsigned char val602; // the value written to address 0x602
// When we modify it, it is better to disable the interrupts
// This way, we will not run in to trouble is at some later time we use the same
// resources from several tasks.

//Get the inputs at E0
unsigned int getE0(void)
{
return inportb(0x600);
}

//Get the inputs at E1
unsigned int getE1(void)
{
unsigned int rslt;

rslt=0x000F&inportb(0x603); // bits 0..3
if (!hal_read_pio(0)) rslt |= 0x0010; // bit 4
if (!hal_read_pio(1)) rslt |= 0x0020; // bit 5
if (!hal_read_pio(4)) rslt |= 0x0040; // bit 6
if (!hal_read_pio(3)) rslt |= 0x0080; // bit 7
return rslt;
}

//Set the outputs at A0
void setA0(unsigned int bits)
{
outportb(0x601, bits);
val601 = bits;
}

//Get the outputs at A0 (not readable so get the value from our last write!)
unsigned int getA0(void)
{
return val601;
}

// Check if the outputs have a overload
int outputOverload(void) // ret 1 if overload on outputs A0
{
return (0x01&inportb(0x602));
}

// Check if a voltage is present for the output group
int noOutputPowerAvail(void) // ret 1 if no power is connected
{
return (0x10&inportb(0x602));
}

// Return the position of the switch
int getSwitch(void) // return position of the switch
{
int rslt;
disable(); // could be, we use several threads, so we must protect the
// variable val602
outportb(0x602,val602|0x04); // set bit 2
rslt=inportb(0x602);
outportb(0x602,val602); // reset bit 2
enable();

return rslt&0x0F;
}

// Set the color of the LED
void setLED(int val) // set LED. 0: off, 1: green, 2: Red, 3: Yellow
{
disable();
val602 &= (~3); // first reset the bits 0 and 1
val602 |= val;
outportb(0x602,val602);
enable();
}

/****************************************************************************
CGI function, the webserver executes this function, if a
browser request e.g. http://192.168.200.8/io comes in,

If the browserrequest is send without any arguments, we send
the predefined page in memory (htmlpage).

The arguments are made simple:
00 means reset DK40 Output 0
01 set DK40 Output 0
...
61 means set DK40 Output 6

****************************************************************************/

void huge _pascal DK40_Func(rpCgiPtr CgiRequest)
{
int bit;
int tmp;

unsigned char e0, e1, a0;
char szPage[1000];
char* szResult;

szResult = "read - ok";
if(CgiRequest->fArgumentBufferLength==2L)
{
tmp = CgiRequest->fArgumentBufferPtr[0] - 48;
if((tmp>=0) && (tmp<=7))
{
//read the current value
a0 = getA0();

bit = tmp;
tmp = CgiRequest->fArgumentBufferPtr[1] - 48;
switch(tmp)
{
case 0: a0 &= ~(1<<bit);
setA0(a0);
szResult = "set - ok";
break;

case 1: a0 |= 1<<bit;
setA0(a0);
szResult = "reset - ok";
break;

default: szResult = "set/reset - illegal value";
break;
}//switch(tmp)
}
else
{
szResult = "set/reset - illegal pin";
}
}

e0 = getE0();
e1 = getE1();
a0 = getA0();

sprintf(szPage, HtmlPage, e0, e1, a0, szResult);

CgiRequest->fHttpResponse = CgiHttpOk;
CgiRequest->fResponseBufferPtr = szPage;
CgiRequest->fResponseBufferLength = strlen(szPage);
}

int main(void)
{
static union REGS inregs;
static union REGS outregs;

printf("\r\nStarting BC440 CGI Example\r\n\r\n");

//set all DK40 outputs to zero
outportb(0x600,0x00);

//install example cgi function
//init CGI_entry example;
example.PathPtr = HtmlPageName; //name of the page
example.method = CgiHttpGet; //http method
example.CgiFuncPtr = DK40_Func; //function

inregs.h.ah = CGI_INSTALL;
inregs.x.dx = FP_SEG(&example);
inregs.x.si = FP_OFF(&example);

int86(CGIVECT,&inregs,&outregs);

if(outregs.x.dx == (unsigned int)CGI_ERROR)
{
printf("\r\nInstalling CGI function %s failed --> exit program\r\n",example.PathPtr);
return;
}

// set pio 0, 1, 3, & 4 to input
pfe_enable_pio(0,1);
pfe_enable_pio(1,1);
pfe_enable_pio(3,1);
pfe_enable_pio(4,1);

// enable PCS6 and ALE
pfe_enable_pcs(6);
pfe_enable_bus(0xFFFF,1); // allow read and write

//Main loop: Do nothing at all
while(1)
{
//sleep a second
inregs.h.ah = API_SLEEP;
inregs.x.bx = 300; //milliseconds
int86(TCPIPVECT,&inregs,&outregs);
}
}