diff options
-rw-r--r-- | trasmitter/clock.c | 137 |
1 files changed, 117 insertions, 20 deletions
diff --git a/trasmitter/clock.c b/trasmitter/clock.c index 2d8bd61..7ada0cf 100644 --- a/trasmitter/clock.c +++ b/trasmitter/clock.c @@ -1,27 +1,63 @@ #include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include <time.h> #include <sched.h> +#include <inttypes.h> -void main(int argc,char **argv) { - struct timespec begin_time,end_time,next_step,end_time2; - struct sched_param sp; +#define PIN "132" + +void init_pin() { + int fd = open("/sys/class/gpio/export",O_WRONLY); + char buf[16]; + int len = sprintf(buf,"%s\n",PIN); + write(fd,buf,len); + close(fd); + fd = open("/sys/class/gpio/gpio" PIN "/direction",O_WRONLY); + write(fd,"out\n",4); + close(fd); +} - const int width=520000; - const int pulses=30; +void deinit_pin() { + int fd = open("/sys/class/gpio/unexport",O_WRONLY); + char buf[16]; + int len = sprintf(buf,"%s\n",PIN); + write(fd,buf,len); + close(fd); +} + +int pin_fd; +void write_pin(uint8_t value) { + if (!pin_fd) { + pin_fd = open("/sys/class/gpio/gpio" PIN "/value",O_WRONLY); + } + char buf[16]; + int len = sprintf(buf,"%d\n",value); + write(pin_fd,buf,len); +} +void init_sched() { + struct sched_param sp; sp.sched_priority=99; // to make this work, sudo setcap CAP_SYS_NICE=eip clock sched_setscheduler(0,SCHED_RR,&sp); +} - clock_gettime(CLOCK_REALTIME,&begin_time); - clock_gettime(CLOCK_REALTIME,&next_step); - for (int i=0;i<pulses;++i) { - // do something slow - clock_gettime(CLOCK_REALTIME,&end_time2); - // end of "slow" +const int width=520000; +struct timespec next_step; +void send_array(const uint8_t values[],size_t len) { + if (next_step.tv_sec == 0) { + clock_gettime(CLOCK_REALTIME,&next_step); + } + uint8_t pin_value = 1; + for (int i=0; i<len; ++i) { + write_pin(pin_value); + pin_value = 1 - pin_value; - next_step.tv_nsec += width; + next_step.tv_nsec += width * values[i]; if (next_step.tv_nsec > 1000000000UL) { next_step.tv_nsec -= 1000000000UL; @@ -36,14 +72,75 @@ void main(int argc,char **argv) { printf("problems: %d\n",rc); } } - clock_gettime(CLOCK_REALTIME,&end_time); - clock_gettime(CLOCK_REALTIME,&end_time2); +} +void my_sleep(uint8_t seconds) { + next_step.tv_sec += seconds; + + int rc = clock_nanosleep(CLOCK_REALTIME, + TIMER_ABSTIME, + &next_step, + NULL); + if (rc) { + printf("problems: %d\n",rc); + } +} + +// alternate HIGH and LOW, first one of each array is HIGH +const uint8_t prologue[] = { + 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, + 2,3, 1,1, 2,2, 1,1, 1,1, 2,1, + 1,1, 1,1, 1,2, 1,1, 1,1, 1,1, + 1,1, 1,1, +}; +const size_t prologueSize = sizeof(prologue) / sizeof(uint8_t); + +const uint8_t epilogue[] = { + 1,2, 2,2, 1,3, // the last value doesn't much matter +}; +const size_t epilogueSize = sizeof(epilogue) / sizeof(uint8_t); + +const uint8_t onSignal[] = { + 2,2, 1,1, 1,1, 1,1, 1,1, 2,1, 1,1, +}; +const size_t onSize = sizeof(onSignal) / sizeof(uint8_t); + +const uint8_t offSignal[] = { + 1,1, 1,1, 1,1, 1,1, 1,1, 2,2, 2,1, +}; +const size_t offSize = sizeof(offSignal) / sizeof(uint8_t); + +void transmitSignal(const uint8_t signal[], size_t signalSize) { + send_array(prologue,prologueSize); + send_array(signal,signalSize); + send_array(epilogue,epilogueSize); +} + +void sendTrain(const uint8_t signal[], size_t signalSize) { + transmitSignal(offSignal,offSize); + my_sleep(1); + transmitSignal(signal,signalSize); + my_sleep(2); + transmitSignal(offSignal,offSize); +} + +int main(int argc,char **argv) { + if (argc != 2) { + fprintf(stderr,"thing 0/1\n"); + return 1; + } + + init_sched(); + init_pin(); + write_pin(0); + + if (argv[1][0] == '1') { + sendTrain(onSignal,onSize); + } + else { + sendTrain(offSignal,offSize); + } - printf("began at %ld.09%ld\n",begin_time.tv_sec,begin_time.tv_nsec); - printf("ended at %ld.09%ld\n",end_time.tv_sec,end_time.tv_nsec); - printf("ended at %ld.09%ld\n",end_time2.tv_sec,end_time2.tv_nsec); + deinit_pin(); - long elapsed = end_time.tv_nsec - begin_time.tv_nsec; - printf("elapsed: %ld\n",elapsed); - printf("error: %ld\n",elapsed-(pulses*width)); + return 0; } |