Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Difference between revisions of "Rock/LED"

(python)
m (Rock/LED)
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.
  
 +
__TOC__
  
Below is program in different languages to flash the green led on RR.
+
= LEDs as class devices =
  
__TOC__
+
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 ==
+
== C ==
 
<syntaxhighlight lang="c" enclose="div">
 
<syntaxhighlight lang="c" enclose="div">
  
Line 20: Line 32:
 
     return 1;
 
     return 1;
 
   }
 
   }
   fprintf(fp, "%d", 175);
+
   fprintf(fp, "%d", 172);
 
   fclose(fp);
 
   fclose(fp);
  
Line 46: Line 58:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== python ==
+
== Python ==
 
<syntaxhighlight lang="python" enclose="div">
 
<syntaxhighlight lang="python" enclose="div">
  
Line 68: Line 80:
  
 
if __name__ == '__main__':
 
if __name__ == '__main__':
   initpin(175, 'out')
+
   initpin(172, 'out')
 
   while True:
 
   while True:
     setpin(175, 0)
+
     setpin(172, 0)
 
     time.sleep(1)
 
     time.sleep(1)
     setpin(175, 1)
+
     setpin(172, 1)
 
     time.sleep(1)
 
     time.sleep(1)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== shell ==
+
== Shell ==
 
<syntaxhighlight lang="bash" enclose="div">
 
<syntaxhighlight lang="bash" enclose="div">
  
Line 95: Line 107:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
== 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/.
 

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);
 
}

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)

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