This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
TRANSMITTER="/usr/local/bin/transmitter" | |
SHAREDIR="/usr/local/share" | |
RUNDIR="/var/run" | |
GPIO="4" | |
COUNT=5 | |
if [ ! $# -eq 2 ]; then | |
echo "Usage: $0 accessory <on|off|state>" | |
exit 1 | |
fi | |
case "$2" in | |
"on" | "off" ) | |
for i in $(seq 1 $COUNT); do | |
sudo $TRANSMITTER $GPIO ${SHAREDIR}/${1}-${2} | |
done | |
echo ${2} | sudo tee ${RUNDIR}/${1}-state ;; | |
"state" ) grep -s -q "on" ${RUNDIR}/${1}-state ;; | |
* ) exit 2;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <wiringPi.h> | |
// gcc -o transmitter transmitter.c -lwiringPi -lpthread | |
#define BUF_LEN 1024 | |
// 38kHz Frequency, 26.3usec, 1/3 duty | |
#define T 26 | |
#define PULSE_HIGH 8 | |
#define PULSE_LOW 18 | |
void pulse(int pin, unsigned int msec) { | |
const unsigned int count = msec / T; | |
for (unsigned int i = 0; i < count; i++) { | |
digitalWrite(pin, 1); | |
delayMicroseconds(PULSE_HIGH); | |
digitalWrite(pin, 0); | |
delayMicroseconds(PULSE_LOW); | |
} | |
} | |
void space(int pin, unsigned int msec) { | |
digitalWrite(pin, 0); | |
delayMicroseconds(msec); | |
} | |
unsigned int *load(FILE *fp, unsigned int *len) { | |
char buf[BUF_LEN]; | |
char sign; | |
unsigned int line = 0; | |
while (fgets(buf, BUF_LEN, fp) != NULL) | |
line++; | |
if (line == 0) { | |
fprintf(stderr, "File empty."); | |
return NULL; | |
} | |
unsigned int *data = (unsigned int *)calloc(line + 1, sizeof(unsigned int)); | |
unsigned int *d = data; | |
rewind(fp); | |
fscanf(fp, "%c%*4c %u\n", &sign, d); | |
if (sign == 'p') // pulse | |
*d++; | |
for (int i = 1; i < line; i++) { | |
fscanf(fp, "%*5c %u\n", &(*d++)); | |
} | |
*len = line; | |
return data; | |
} | |
void emit(unsigned int *data, unsigned int len, int pin) { | |
for (int i = 0; i < len; i += 2) { | |
pulse(pin, data[i]); | |
space(pin, data[i + 1]); | |
} | |
} | |
int send(FILE *fp, int pin) { | |
unsigned int len; | |
unsigned int *data = load(fp, &len); | |
fclose(fp); | |
if (data == NULL) | |
return 1; | |
emit(data, len, pin); | |
free(data); | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc != 3) { | |
printf("Usage: %s gpio-pin timing-file\n", argv[0]); | |
return 1; | |
} | |
if (wiringPiSetup() == -1) { | |
fprintf(stderr, "WiringPi setup error"); | |
return 1; | |
} | |
pinMode(atoi(argv[1]), OUTPUT); | |
FILE *fp = fopen(argv[2], "r"); | |
if (fp == NULL) { | |
fprintf(stderr, "File open error: %s", argv[2]); | |
return 1; | |
} | |
return send(fp, atoi(argv[1])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment