📄 astrocam.c
字号:
/* * This is the Astrocam source code. written by Steffen Wendzel * * (c) 2001, 2002, 2003 Steffen Wendzel - astrocam@gmx.net * */ #include <stdio.h>#include <fcntl.h>#include <stddef.h>#include <errno.h>#include <signal.h>#ifdef WIN32 /* i was thinking about 'evil empire-os support' but... :-) */ #include <conio.h> #include <time.h>#else #include <unistd.h> #include <sys/time.h> #include <sys/times.h> #include <syslog.h>#endif#if defined(__linux__) #include <asm/io.h>#endif#if defined( __NetBSD__) || defined(__OpenBSD__) #include <sys/types.h> #include <sys/syscall.h> #include <i386/sysarch.h> #include <i386/pio.h> #define BSD 1#endif#if defined(__FreeBSD__) #include <sys/types.h> #include <sys/syscall.h> #include <machine/sysarch.h> #include <machine/cpufunc.h>#endif#if defined(__svr4__) /* under work, may it comes with v. 3.4 */#endif#include "astrocam.h"#define VERSION "2.3.2"#define PIDFILE "/var/log/acam_pid"#define MAX_MOT 3#define MAX_PHASEN 4#define SLEEP 100#define PA_L 0#define PA_R 4#define PB_L 0#define PB_R 32#define CTRL_MOT_X 0#define CTRL_MOT_Y 64#define CTRL_MOT_Z 128/* some errors */#define ERROR_INIT perror("AstroCamInit");#define ERROR_EXIT perror("AstroCamShutdown");long pos[MAX_MOT];int mot_power[MAX_MOT], i=0;#ifdef __FreeBSD__int devfd;#endifunsigned int iobase;/* my syslog function */void logit(char *string);static unsigned char SMC_mot_ctrl[MAX_MOT] ={ CTRL_MOT_X, CTRL_MOT_Y, CTRL_MOT_Z};static unsigned char SMC_phase[MAX_PHASEN] ={ PA_L + PB_L, PA_R + PB_L, PA_R + PB_R, PA_L + PB_R};void SMC_outportb(unsigned char b){ #ifdef linux outb(b, iobase); outb(0, iobase + 2); outb(1, iobase + 2); #endif #if defined(BSD) || defined(__FreeBSD__) outb(iobase, b); outb(iobase + 2, 0); outb(iobase + 2, 1); #endif #ifdef WIN32 _outp(iobase, b); _outp(iobase + 2, 0); _outp(iobase + 2, 1); #endif}void AstroCam_Step(int mot, long n){ unsigned char ctrl = SMC_mot_ctrl[mot]; if(n>0){ for(i=0;i<n;i++){ pos[mot]++; SMC_outportb(ctrl + SMC_phase[pos[mot] % MAX_PHASEN]); usleep(SLEEP); } }else{ for(i=0; i>n; i--){ pos[mot]--; SMC_outportb(ctrl + SMC_phase[pos[mot] % MAX_PHASEN]); usleep(SLEEP); } }}#ifdef BSD void SMC_ioport(unsigned int iobase) { u_long iomap[0x20]; struct i386_set_ioperm_args ioperm; /* code part found in usenet. actually i realy love * syscall() and sysctl() */ ioperm.iomap = iomap; syscall(SYS_sysarch, I386_GET_IOPERM, (char *) &ioperm); iomap[iobase >> 5] &= ~(1 << (iobase & 0x1f)); syscall(SYS_sysarch, I386_SET_IOPERM, (char *) &ioperm); }#endifint AstroCamInit(unsigned int io){ int i=0; iobase = io; #ifdef BSD if(getuid()==0){ SMC_ioport(iobase); SMC_ioport(iobase + 1); SMC_ioport(iobase + 2); }else{ ERROR_INIT fflush(stderr); return -1; } #endif #ifdef linux if(ioperm(iobase, 3, 1)){ ERROR_INIT fflush(stderr); return -1; } #endif #ifdef __FreeBSD__ devfd=open("/dev/io", O_WRONLY); if(devfd<0){ ERROR_INIT fflush(stderr); return -1; } #endif for(;i<MAX_MOT; i++){ mot_power[i]=-1; pos[i]=0x61a80; } return 0;}int AstroCamShutdown(){ #ifdef linux if(ioperm(iobase, 3, 0)){ ERROR_EXIT return -1; } #endif #ifdef __FreeBSD__ if(close(devfd)!=0) { ERROR_EXIT return -1; } #endif return 0;}void rew(FILE *fd){ fseek(fd, 0, SEEK_END); rewind(fd);}void prflush(){ printf("."); fflush(stdout);}void dm(pid_t *mypid){ setsid(); umask(0); *mypid=getpid();}void welcome(){printf("\n\x1B[32mAstroCam Daemon " VERSION " \x1B[0m [ " "Written and Copyright 2001,2002,2003 by Steffen Wendzel\n" "astrocam@doomed-reality.org ]\x1B[32m running...\x1B[0m\n");}void sighndl(int signo){ logit("signal response - shutdown now !"); unlink(PIDFILE); if(AstroCamShutdown()<0) perror("AstroCamShutdown(): "); exit(1);}void logit(char *string){ openlog("astrocam", LOG_PID, LOG_DAEMON); syslog(LOG_ERR, string); closelog();}int chkrunstat(){ int file; if((file = open(PIDFILE, O_RDONLY))<0) return 0; else{ fprintf(stderr, "It seems that the astrocam daemon is allready " "running. Please wait for resatart.\n"); return -1; }}int restartac(){ int file; char *buf; if((buf=(char *)malloc(11*sizeof(char)))==NULL){ perror("malloc "); return -1; } if((file=open(PIDFILE, O_RDONLY))<0) return -1; if(read(file, buf, 10)<1) return -1; kill(atoi(buf), SIGTERM); unlink(PIDFILE); free(buf); close(file); return 0;}int main(int argc, char* argv[]){ /* Flags: free/fclose */ FILE *file, *fdpid; // OK OK pid_t mypid; char *buf; // OK int i,j; i=j=0x0; if(chkrunstat()<0) if(restartac()<0){ fprintf(stderr, "--- Alert: Cant restart astrocam daemon, please make sure that " "there is no other astrocam process and remove /var/log/acam_pid if " "sure. Try to start astrocam again after this steps. ---\n"); return -1; } if(AstroCamInit(iobase)!=0){ perror("AstroCamInit() "); return -1; } if((file = fopen("/etc/acam", "r"))==NULL){ perror("fopen() "); return -1; } signal(SIGINT|SIGQUIT|SIGTERM, sighndl); if((mypid=fork())<0){ perror("fork() "); return -1; }else if(mypid!=0){ exit(1); }else{ dm(&mypid); printf("\nstoring astrocam proccess id in " PIDFILE "! [%d]", mypid); } if((buf=(char *)malloc(11*sizeof(char)))==NULL){ perror("malloc "); return -1; } snprintf(buf, 10*sizeof(char), "%d ", mypid); if((fdpid=fopen(PIDFILE, "w+"))==NULL){ perror("fopen() "); return -1; } fputs(buf, fdpid); fclose(fdpid); welcome(); while((i=getc(file))!=EOF) { if(i!=10) { j=0x0; if(i==0x4c) // L while(j<30){AstroCam_Step(3, 1);j++;} if(i==0x6c) // l while(j<10){AstroCam_Step(3, 1);j++;} if(i==0x52) // R while(j<30){AstroCam_Step(0, -1);j++;} if(i==0x72) // r while(j<10){AstroCam_Step(0, -1);j++;} if(i==0x75) // u while(j<10){AstroCam_Step(1, 1);j++;} if(i==0x64) // d while(j<10){AstroCam_Step(1, -1);j++;} } rew(file); usleep(1500000); //prflush(); } /* we _should_ never be here here */ fclose(file); if(AstroCamShutdown()<0) perror("AstroCamShutdown() "); free(buf); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -