Created
April 5, 2022 22:19
-
-
Save mimura1133/0ecbced19e7397cb8ab5f5ddc3459493 to your computer and use it in GitHub Desktop.
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
// | |
// CKlau 社製 KVM スイッチの「有線リモコン」から信号を読み出すサンプル。 | |
// 2022 Satoshi Mimura ( @mimura1133 ) | |
// | |
// 対象の製品例 : https://www.amazon.co.jp/dp/B07HRBBLWN?th=1 | |
// | |
// GPIO 17 番に信号線、GND に GND を接続すると動くと思います。 | |
// また GPIO 22 番から内部で取得した信号を吐き出すようになっているので、オシロスコープでデバッグできます。 | |
// | |
// 夜中に一気に書き上げたので、デバッグコードやら洗練されてない書き方が多々ありますが | |
// もしご参考等になるようであれば是非ご自由にお使いください。 | |
// | |
#include <stdio.h> | |
#include <pigpio.h> | |
#define scancnt 400 | |
#define prec 15 | |
#define prec_shift (prec/2) | |
int main(void) | |
{ | |
if(gpioInitialise() < 0) | |
{ | |
fprintf(stderr, "pin-error\n"); | |
return 1; | |
} | |
gpioSetMode(17,PI_PUD_DOWN); | |
gpioSetMode(17,PI_INPUT); | |
gpioSetMode(22,PI_OUTPUT); | |
while(1) | |
{ | |
char rawdata[scancnt] = {0}; | |
int i = 0; | |
double lasttime = 0; | |
char data[(scancnt/prec)+1] = {0}; | |
while(gpioRead(17) == 1) { } | |
lasttime = 0; | |
i = 0; | |
while(1) | |
{ | |
char s; | |
if((time_time() - lasttime) < 0.000005) | |
continue; | |
s = gpioRead(17); | |
if(i == 0 && s == 0) | |
continue; | |
rawdata[i++] = s; | |
if(i >= scancnt) | |
break; | |
lasttime = time_time(); | |
} | |
for(i = 0; i < (scancnt / prec); i++) | |
data[i] = rawdata[i*prec + prec_shift]; | |
for(i = 0; i < sizeof(data);i++) | |
printf("%d,",data[i]); | |
if( | |
data[0] == 1 && | |
data[1] == 1 && | |
data[2] == 0 && | |
data[3] == 1 && | |
data[4] == 0 && | |
data[5] == 1 && | |
data[6] == 0 && | |
data[7] == 1 && | |
data[8] == 0 && | |
data[9] == 1 && | |
data[10] == 1 | |
) | |
{ | |
printf("[FOUND: PREAMBLE],"); | |
if( | |
data[17] == 0 && | |
data[18] == 1 && | |
data[19] == 0 && | |
data[20] == 0 | |
) | |
{ | |
printf("[SELECT : 1]\n"); | |
} | |
else if( | |
data[17] == 0 && | |
data[18] == 0 && | |
data[19] == 1 && | |
data[20] == 1 | |
) | |
{ | |
printf("[SELECT : 2]\n"); | |
} | |
else if( | |
data[17] == 0 && | |
data[18] == 0 && | |
data[19] == 1 && | |
data[20] == 0 | |
) | |
{ | |
printf("[SELECT : 3]\n"); | |
} | |
else if( | |
data[17] == 0 && | |
data[18] == 0 && | |
data[19] == 0 && | |
data[20] == 1 | |
) | |
{ | |
printf("[SELECT : 4]\n"); | |
} | |
} | |
time_sleep(0.5); | |
} | |
gpioTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment