net_radio_nec.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pigpio.h>
#include <unistd.h>
int inPin = 27;//Physical 13
int level;
int p_count = 1;
int flag = 0;
char i_c = '0';
char stringCom[12] = "mpc play 1";
char stringComStop[10] = "mpc stop";
int stringDat[9];
int stringDatCount[35];
int i = 0;
uint32_t tick;
uint32_t startTick;
uint32_t endTick;
uint32_t diffTick;
int count = 0;
int start = 0;
int last = 0;
int res = 0;
int ad1 = 0;
int ad2 = 0;
int da1 = 0;
int da2 = 0;
int resU = 0;
int decode (int da1)
{
flag = 1;
switch (da1)
{
case 84 : {//AUX
p_count = 1;
}
break;
case 17 : {//A.TAPE
p_count = 1;
}
break;
case 63 : {//PLAY
p_count = 1;
flag = 1;
}
break;
case 77 : {//STOP
system (stringComStop);
flag = 0;
}
break;
case 12 : {//OFF
system (stringComStop);
flag = 0;
}
break;
case 38 : p_count ++;//RIGHT ARROW
break;
case 40 : p_count --;//LEFT ARROW
break;
case 49: {//1
p_count = 1;
}
break;
case 50: {//2
p_count = 2;
}
break;
case 51: {//3
p_count = 3;
}
break;
case 52: {//4
p_count = 4;
}
break;
case 53: {//5
p_count = 5;
}
break;
case 54: {//6
p_count = 6;
}
break;
case 55: {//7
p_count = 7;
}
break;
case 56: {//8
p_count = 8;
}
break;
case 57: {//9
p_count = 9;
}
break;
default: {
flag = 0;
return 0;
}
}
if (flag == 1)
{
if (p_count > 9)
{
p_count = 1;
}
if (p_count < 1)
{
p_count = 9;
}
i_c = p_count + 48;//int to char max 9
stringCom[9] = i_c;
system (stringCom);
}
return 0;
}
void func (int gpio, int level, uint32_t tick;)
{
endTick = gpioTick();
diffTick = endTick - startTick;
// printf ("GPIO %d became %d at %d \n ", inPin, level, diffTick);
if (count > 0 && diffTick > 50000)//test for bad reception
{
count = 0;
}
startTick = gpioTick();
if (diffTick > 10000 && diffTick < 15000)
count = 0;
if (diffTick < 3000)
count ++;
if (diffTick > 1500)
{
stringDatCount[count] = 1;
}
else
{
stringDatCount[count] = 0;
}
if (count > 31)
{
// printf ("count 32 %d \n ",count);
// for (i = 1; i < 33; i++ )
// printf (" bit %d \n ", stringDatCount[i]);
count = 0;
res = 0;
for (i = 1; i < 33; i++)
{
switch (i)
{
case 1 : res = stringDatCount[i] * 1;
break;
case 2 : res = res + (stringDatCount[i] * 2);
break;
case 3 : res = res + (stringDatCount[i] * 4);
break;
case 4 : res = res + (stringDatCount[i] * 8);
break;
case 5 : res = res + (stringDatCount[i] * 16);
break;
case 6 : res = res + (stringDatCount[i] * 32);
break;
case 7 : res = res + (stringDatCount[i] * 64);
break;
case 8 : {res = res + (stringDatCount[i] * 128);
ad1 = res;}
break;
case 9 : res = stringDatCount[i] * 1;
break;
case 10 : res = res + (stringDatCount[i] * 2);
break;
case 11 : res = res + (stringDatCount[i] * 4);
break;
case 12 : res = res + (stringDatCount[i] * 8);
break;
case 13 : res = res + (stringDatCount[i] * 16);
break;
case 14 : res = res + (stringDatCount[i] * 32);
break;
case 15 : res = res + (stringDatCount[i] * 64);
break;
case 16 : {res = res + (stringDatCount[i] * 128);
ad2 = res; res = 0;}
break;
case 17 : res = stringDatCount[i] * 1;
break;
case 18 : res = res + (stringDatCount[i] * 2);
break;
case 19 : res = res + (stringDatCount[i] * 4);
break;
case 20 : res = res + (stringDatCount[i] * 8);
break;
case 21 : res = res + (stringDatCount[i] * 16);
break;
case 22 : res = res + (stringDatCount[i] * 32);
break;
case 23 : res = res + (stringDatCount[i] * 64);
break;
case 24 : {res = res + (stringDatCount[i] * 128);
da1 = res; res = 0;}
break;
case 25 : res = stringDatCount[i] * 1;
break;
case 26 : res = res + (stringDatCount[i] * 2);
break;
case 27 : res = res + (stringDatCount[i] * 4);
break;
case 28 : res = res + (stringDatCount[i] * 8);
break;
case 29 : res = res + (stringDatCount[i] * 16);
break;
case 30 : res = res + (stringDatCount[i] * 32);
break;
case 31 : res = res + (stringDatCount[i] * 64);
break;
case 32 : {res = res + (stringDatCount[i] * 128);
da2 = res;}
break;
}
resU = da1 - (255 - da2);
}
// printf ("Resultat %d %d %d %d %d\n ", ad1, ad2, da1, da2, resU);
if (resU == 0)//test for bad reception
decode (da1);
}
}
void main (void)
{
count = 0;
int cfg = gpioCfgGetInternals();
cfg |= PI_CFG_NOSIGHANDLER; // (1<<10)
gpioCfgSetInternals(cfg);
int status = gpioInitialise();
if (gpioInitialise() < 0)
startTick = gpioTick();
gpioSetMode (inPin, PI_INPUT);
gpioSetISRFunc (inPin, FALLING_EDGE, 0, func);
while (1){
sleep (0.01);
}
gpioTerminate();
}
Compile med: gcc -o net_radio_nec net_radio_nec.c -lpigpio
, pigpio se http://abyz.me.uk/rpi/pigpio/download.html
Bemærk:
dette er ekspermentel software der er ingen garanti for brugbarhed, det
kan derimod sandsynligvis være skadeligt, kun til brug i Danmark.