Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <time.h>
- #include <unistd.h>
- #include <stdint.h>
- #include <sys/io.h>
- #define DEBUG_LOG 0
- #define TRACE_LOG 1
- void debug_log(char *s, int t)
- {
- printf("\n%s",s);
- }
- #define PARALLEL_PORT_ADDR 0x378
- #define SENSOR_PORT_ADDR (PARALLEL_PORT_ADDR + 2)
- #define LOWPAPER_SENSOR_PORT (PARALLEL_PORT_ADDR + 1)
- #define MOTOR_PORT_ADDR (PARALLEL_PORT_ADDR + 2)
- #define BIT(x) (1 << x)
- #define SENSOR_CTRL_BIT BIT(2)
- #define SENSOR_VALUE(x) (((x) & SENSOR_CTRL_BIT) >> 2)
- #define LOWPAPER_SENSOR_BIT BIT(3)
- #define LOWPAPER_SENSOR_VALUE(x) (((x) & LOWPAPER_SENSOR_BIT) >> 3)
- #define MOTOR_CTRL_BIT BIT(0)
- #define SENSOR_CLEAR 1
- #define SENSOR_BLOCK 0
- #define MOTOR_START 1
- #define MOTOR_STOP 0
- int16_t readSensorVal(void)
- {
- static int16_t portValPrev = 0;
- int16_t portVal = 0;
- char charBuf[128] = {'\0'};
- outb(inb(SENSOR_PORT_ADDR) | SENSOR_CTRL_BIT, MOTOR_PORT_ADDR);
- usleep(5000);// Delay to make sure the value is reflected in any up-coming read
- portVal = inb(SENSOR_PORT_ADDR);
- if(portVal != portValPrev)
- {
- memset(charBuf, 0, sizeof(charBuf));
- sprintf(charBuf, "PERIPHERAL:JOURNALPRINTER: portVal=%#x, Sensor=%d, Motor=%d\n", portVal, (portVal & 0x04) >> 2, portVal & 0x01);
- debug_log(charBuf, DEBUG_LOG);
- portValPrev = portVal;
- }
- portVal = SENSOR_VALUE(portVal);
- return portVal;
- }
- void rewindMotorCtrl(uint8_t option)
- {
- uint8_t portVal = 0;
- portVal = inb(MOTOR_PORT_ADDR);
- if(option == MOTOR_START)
- portVal |= MOTOR_CTRL_BIT;
- else if(option == MOTOR_STOP)
- portVal &= ~MOTOR_CTRL_BIT;
- outb(portVal, MOTOR_PORT_ADDR);
- }
- int main(int argc,char *argv[])
- {
- int type = atoi(argv[1]);
- unsigned int SensorValue = 0;
- char debugBuf[1024] = {'\0'};
- if (ioperm(PARALLEL_PORT_ADDR, 3, 1))
- {
- perror("ioperm");
- exit(1);
- }
- printf("Type: %d", type);
- rewindMotorCtrl(type);
- SensorValue = readSensorVal();
- if(SensorValue == SENSOR_CLEAR)
- {
- memset(debugBuf,0,sizeof(debugBuf));
- sprintf(debugBuf,"SYSTEM: Sensor Clear: %d", SensorValue);
- debug_log(debugBuf,TRACE_LOG);
- }
- else
- {
- memset(debugBuf,0,sizeof(debugBuf));
- sprintf(debugBuf,"SYSTEM: Sensor Block: %d", SensorValue);
- debug_log(debugBuf,TRACE_LOG);
- }
- printf("\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment