Difference between revisions of "Rock/LED"
< Rock
Line 1: | Line 1: | ||
The RR has 3 LEDs, green(172), blue(174), red(175). All can be programmable. | The RR has 3 LEDs, green(172), blue(174), red(175). All can be programmable. | ||
− | |||
− | < | + | |
+ | Below is program in different languages to flash the green led on RR. | ||
+ | |||
+ | __TOC__ | ||
+ | |||
+ | == c == | ||
+ | <syntaxhighlight lang="c" enclose="div"> | ||
+ | |||
+ | #include <stdio.h> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | FILE *fp = fopen("/sys/class/gpio/export", "w"); | ||
+ | if( !fp ) | ||
+ | { | ||
+ | printf("error open export file"); | ||
+ | return 1; | ||
+ | } | ||
+ | fprintf(fp, "%d", 175); | ||
+ | fclose(fp); | ||
+ | |||
+ | fp = fopen("/sys/class/gpio/gpio175/direction", "w"); | ||
+ | |||
+ | if( !fp ) | ||
+ | { | ||
+ | printf("error open export file"); | ||
+ | return 1; | ||
+ | } | ||
+ | fprintf(fp, "%s", "out"); | ||
+ | fclose(fp); | ||
+ | |||
+ | fp = fopen("/sys/class/gpio/gpio175/value", "w"); | ||
+ | |||
+ | if( !fp ) | ||
+ | { | ||
+ | printf("error open export file"); | ||
+ | return 1; | ||
+ | } | ||
+ | fprintf(fp, "%d", 1); | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | == python == | ||
+ | <syntaxhighlight lang="python" enclose="div"> | ||
+ | |||
+ | import time | ||
+ | |||
+ | def initpin(pinnum, mode): | ||
+ | ''' | ||
+ | pinnum: pin number, eg. 172, 175 etc. | ||
+ | mode: pin mode, valid values: in or out | ||
+ | ''' | ||
+ | with open('/sys/class/gpio/unexport', 'w') as f: | ||
+ | f.write(str(pinnum)) | ||
+ | |||
+ | with open('/sys/class/gpio/export', 'w') as f: | ||
+ | f.write(str(pinnum)) | ||
+ | |||
+ | with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f: | ||
+ | f.write(str(mode)) | ||
+ | |||
+ | def setpin(pinnum, value): | ||
+ | |||
+ | with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f: | ||
+ | f.write(str(value)) | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | initpin(175, 'out') | ||
+ | while True: | ||
+ | setpin(175, 0) | ||
+ | time.sleep(1) | ||
+ | setpin(175, 1) | ||
+ | time.sleep(1) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == shell == | ||
+ | <syntaxhighlight lang="bash" enclose="div"> | ||
+ | |||
#!/bin/sh | #!/bin/sh | ||
Line 18: | Line 96: | ||
sleep 1 | sleep 1 | ||
done | done | ||
− | </ | + | |
+ | </syntaxhighlight> | ||
== LED class and triggers == | == LED class and triggers == | ||
on radxa-rock/android-kk branch, 3 LEDs are configured as LED class devices. you can control LEDs via /sys/class/leds/. | on radxa-rock/android-kk branch, 3 LEDs are configured as LED class devices. you can control LEDs via /sys/class/leds/. |
Revision as of 03:55, 21 April 2014
The RR has 3 LEDs, green(172), blue(174), red(175). All can be programmable.
Below is program in different languages to flash the green led on RR.
Contents
c
#include <stdio.h>
int main()
{
FILE *fp = fopen("/sys/class/gpio/export", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 175);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/direction", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%s", "out");
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/value", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 1);
}
int main()
{
FILE *fp = fopen("/sys/class/gpio/export", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 175);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/direction", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%s", "out");
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/value", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 1);
}
python
import time
def initpin(pinnum, mode):
'''
pinnum: pin number, eg. 172, 175 etc.
mode: pin mode, valid values: in or out
'''
with open('/sys/class/gpio/unexport', 'w') as f:
f.write(str(pinnum))
with open('/sys/class/gpio/export', 'w') as f:
f.write(str(pinnum))
with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f:
f.write(str(mode))
def setpin(pinnum, value):
with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f:
f.write(str(value))
if __name__ == '__main__':
initpin(175, 'out')
while True:
setpin(175, 0)
time.sleep(1)
setpin(175, 1)
time.sleep(1)
def initpin(pinnum, mode):
'''
pinnum: pin number, eg. 172, 175 etc.
mode: pin mode, valid values: in or out
'''
with open('/sys/class/gpio/unexport', 'w') as f:
f.write(str(pinnum))
with open('/sys/class/gpio/export', 'w') as f:
f.write(str(pinnum))
with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f:
f.write(str(mode))
def setpin(pinnum, value):
with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f:
f.write(str(value))
if __name__ == '__main__':
initpin(175, 'out')
while True:
setpin(175, 0)
time.sleep(1)
setpin(175, 1)
time.sleep(1)
shell
#!/bin/sh
# enable the gpio 172 -> green led
echo 172 > /sys/class/gpio/export
# set the direction to output
echo "out" > /sys/class/gpio/gpio172/direction
while true;
do
echo 0 > /sys/class/gpio/gpio172/value #led on
sleep 1
echo 1 > /sys/class/gpio/gpio172/value #led off
sleep 1
done
# enable the gpio 172 -> green led
echo 172 > /sys/class/gpio/export
# set the direction to output
echo "out" > /sys/class/gpio/gpio172/direction
while true;
do
echo 0 > /sys/class/gpio/gpio172/value #led on
sleep 1
echo 1 > /sys/class/gpio/gpio172/value #led off
sleep 1
done
LED class and triggers
on radxa-rock/android-kk branch, 3 LEDs are configured as LED class devices. you can control LEDs via /sys/class/leds/.