00001
00028
00029 #include "pinapi.h"
00030 #include <dos.h>
00031 #include <string.h>
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034
00040 static char pinstring[9];
00041
00046 static int init = 0;
00047
00052 void init_pins() {
00053 union REGS inregs;
00054 union REGS outregs;
00055
00056 inregs.h.ah = 0x83;
00057 inregs.x.dx = 0x40;
00058 int86(0xA2, &inregs, &outregs);
00059
00060 outport(0xff70, inport(0xff70) | 0x0408); // Mode als Ausgang (Mode=1)
00061 outport(0xff72, inport(0xff72) & ~0x0408); // LED 1 & PIN 7 als Ausgang (Direction=0)
00062
00063 init = 1;
00064 }
00065
00066 bool getPinStatus(int pin)
00067 {
00068 byte pinset, bitmask;
00069
00070 if (!init) {
00071 init_pins();
00072 }
00073
00074 if ( (pin < 0) || (pin > 7) ) { /* false for non-existing pins */
00075 return 0;
00076 }
00077
00078 /* read in the actual status of pin 0 - 7*/
00079 pinset = inportb(0x600);
00080 bitmask = 1 << pin;
00081 return (pinset & bitmask);
00082 }
00083
00084 byte getAllPinStatus()
00085 {
00086 if (!init) {
00087 init_pins();
00088 }
00089
00090 return inportb(0x600);
00091 }
00092
00093 char* getAllPinStatus_string()
00094 {
00095 int pinset;
00096 int i;
00097 char pin_str[4];
00098 byte pinstatus;
00099 char tmp[2];
00100
00101 pinstring[8] = 0;
00102
00103 if (!init) {
00104 init_pins();
00105 }
00106
00107 pinstatus = getAllPinStatus();
00108 sprintf(pin_str, "%d", pinstatus);
00109 pinset = atoi(pin_str);
00110 for (i = 7; i >= 0; i--) {
00111 sprintf(tmp, "%d", div(pinset, (1<<i)));
00112 pinstring[i] = tmp[0];
00113 pinset = pinset % (1<<i);
00114 }
00115
00116 return pinstring;
00117 }
00118
00119 void setPin(int pin, bool set)
00120 {
00121 byte pinset;
00122 unsigned int pio0;
00123
00124 if (!init) {
00125 init_pins();
00126 }
00127
00128 if ( (pin < 0) || (pin > 7) ) { /* false for non-existing pins */
00129 return;
00130 }
00131
00132 pinset = inportb(0x600); /* read the current value */
00133
00134 switch(set){
00135 case 0:
00136 pinset &= ~(1<<pin);
00137 outportb(0x600, pinset);
00138 pio0 = inport(0xff74); /* PIO0 Register aus AM186 lesen */
00139 pio0 &= ~0x0400; /* Bit 10 in PIO0 Register ist IO7 */
00140 if (pinset & 0x80) {
00141 pio0 |= 0x0400;
00142 }
00143 outport(0xff74, pio0);
00144 break;
00145 case 1:
00146 pinset |= 1<<pin;
00147 outportb(0x600, pinset);
00148 pio0 = inport(0xff74); /* PIO0 Register aus AM186 lesen */
00149 pio0 &= ~0x0400; /* Bit 10 in PIO0 Register ist IO7 */
00150 if (pinset & 0x80) {
00151 pio0 |= 0x0400;
00152 }
00153 outport(0xff74, pio0);
00154 break;
00155 default: break;
00156 }
00157 }
00158
00159 void setAllPins(byte set)
00160 {
00161 byte pinset;
00162 unsigned int pio0;
00163
00164 if (!init) {
00165 init_pins();
00166 }
00167
00168 pinset = set;
00169 outportb(0x600, pinset);
00170 pio0 = inport(0xff74); /* PIO0 Register aus AM186 lesen */
00171 pio0 &= ~0x0400; /* Bit 10 in PIO0 Register ist IO7 */
00172 if (pinset & 0x80) {
00173 pio0 |= 0x0400;
00174 }
00175 outport(0xff74, pio0);
00176 }
00177
00178 void setAllPins_string(char* setString)
00179 {
00180 byte pinset = 0;
00181 int potenz = 0;
00182 int i;
00183
00184 if (!init) {
00185 init_pins();
00186 }
00187
00188 if ( (strlen(setString) > 8) || (strlen(setString) == 0) ) {
00189 return;
00190 }
00191
00192 for (i = 0; i < 8; i++, potenz++) {
00193 pinset += (setString[i] - 48) * (1 << potenz);
00194 }
00195
00196 setAllPins(pinset);
00197 }