Skip to content

Instantly share code, notes, and snippets.

@krishnan793
Last active August 29, 2015 14:16
Show Gist options
  • Save krishnan793/29c75261168040c99102 to your computer and use it in GitHub Desktop.
Save krishnan793/29c75261168040c99102 to your computer and use it in GitHub Desktop.
BeagleBoneBlack Push Button + LED
#include<stdio.h>
#include<unistd.h>
void Read(char *switch_value, char *val);
void Write(char *switch_value, char *val);
void Blink(char *led_value, int delay);
int main()
{
char *led_direction="/sys/class/gpio/gpio23/direction";
char *switch_direction="/sys/class/gpio/gpio26/direction";
char *led_value="/sys/class/gpio/gpio23/value";
char *switch_value="/sys/class/gpio/gpio26/value";
char *cmd_out="out";
char *cmd_in="in";
char *export="/sys/class/gpio/export";
char *unexport="/sys/class/gpio/unexport";
char val[10];
Write(export,"23");
Write(export,"26");
Write(led_direction,cmd_out);
Write(switch_direction,cmd_in);
while(1)
{
Read(switch_value,val);
printf("%s\033[F\033[J",val);
if(!strcmp(val,"1\n"))
Blink(led_value,100000);
else
Write(led_value,"0");
usleep(100000);
}
}
void Read(char *url, char *val)
{
FILE *path=fopen(url,"r");
fread(val,2,1,path);
fclose(path);
}
void Write(char *url, char *val)
{
FILE *path=fopen(url,"w");
fwrite(val,1,sizeof(val),path);
fclose(path);
}
void Blink(char *led_value, int delay)
{
int i;
for(i=0;i<10;i++){
Write(led_value,"1");
usleep(delay);
Write(led_value,"0");
usleep(delay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment