Difference between revisions of "Rock/LED"
< Rock
(→LEDs as class devices) |
(→LEDs as class devices) |
||
Line 12: | Line 12: | ||
Read the documentation for more information: https://www.kernel.org/doc/Documentation/leds/leds-class.txt | Read the documentation for more information: https://www.kernel.org/doc/Documentation/leds/leds-class.txt | ||
+ | |||
+ | The default status of the three on board leds are: | ||
+ | RED: On when there is power | ||
+ | GREEN: Flash twice per second after the kernel booting, it's called heartbeat. | ||
+ | BLUE: Flash when the uSD card is reading/writing. | ||
= LEDs as GPIOs = | = LEDs as GPIOs = |
Revision as of 06:07, 1 September 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 (Ubuntu 14.04 server/desktop), 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
The default status of the three on board leds are:
RED: On when there is power GREEN: Flash twice per second after the kernel booting, it's called heartbeat. BLUE: Flash when the uSD card is reading/writing.
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