Difference between revisions of "Rock/LED"
< Rock
(→python) |
m (→Rock/LED) |
||
Line 1: | Line 1: | ||
− | The RR has 3 LEDs | + | The RR has 3 LEDs: green(172), blue(174), red(175). |
+ | All can be programmable. | ||
+ | __TOC__ | ||
− | + | = LEDs as class devices = | |
− | + | On the latest kernel images (radxa-rock/android-kk branch), the 3 LEDs are configured as LED class devices. | |
+ | |||
+ | You can control them via '''/sys/class/leds/''' | ||
+ | |||
+ | Read the documentation for more information: https://www.kernel.org/doc/Documentation/leds/leds-class.txt | ||
+ | |||
+ | = LEDs as GPIOs = | ||
+ | |||
+ | On the previous kernel images, the LEDs can be controled as GPIOs. | ||
+ | |||
+ | Below is program in different languages to flash the green led on RR. | ||
− | == | + | == C == |
<syntaxhighlight lang="c" enclose="div"> | <syntaxhighlight lang="c" enclose="div"> | ||
Line 20: | Line 32: | ||
return 1; | return 1; | ||
} | } | ||
− | fprintf(fp, "%d", | + | fprintf(fp, "%d", 172); |
fclose(fp); | fclose(fp); | ||
Line 46: | Line 58: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == Python == |
<syntaxhighlight lang="python" enclose="div"> | <syntaxhighlight lang="python" enclose="div"> | ||
Line 68: | Line 80: | ||
if __name__ == '__main__': | if __name__ == '__main__': | ||
− | initpin( | + | initpin(172, 'out') |
while True: | while True: | ||
− | setpin( | + | setpin(172, 0) |
time.sleep(1) | time.sleep(1) | ||
− | setpin( | + | setpin(172, 1) |
time.sleep(1) | time.sleep(1) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == Shell == |
<syntaxhighlight lang="bash" enclose="div"> | <syntaxhighlight lang="bash" enclose="div"> | ||
Line 95: | Line 107: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− |
Revision as of 08:09, 28 July 2014
The RR has 3 LEDs: green(172), blue(174), red(175).
All can be programmable.
LEDs as class devices
On the latest kernel images (radxa-rock/android-kk branch), the 3 LEDs are configured as LED class devices.
You can control them via /sys/class/leds/
Read the documentation for more information: https://www.kernel.org/doc/Documentation/leds/leds-class.txt
LEDs as GPIOs
On the previous kernel images, the LEDs can be controled as GPIOs.
Below is program in different languages to flash the green led on RR.
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", 172);
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", 172);
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/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(172, 'out')
while True:
setpin(172, 0)
time.sleep(1)
setpin(172, 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/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(172, 'out')
while True:
setpin(172, 0)
time.sleep(1)
setpin(172, 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