NSLU2-Linux
view · edit · print · history

Deprecated

In SlugOS (DebianSlug?/uCslugC/slugos-ltu), there is now a new interface to the LEDS through SysFS?.

Thanks to kas11's work (see the OpenN2 project on sourceforge) Openslug developers can easily control the LEDs and Buzzer from their code using simple ioctls calls.

Here's the simple functions I used on my robot project:

For more details see the source code at
openembedded/packages/linux/openslug-kernel-2.6.9/nslu2-io.c in the CVS.

Feel free to correct my bad coding practices :)

N.B. Be sure to create /dev/leds as a character device with major 126 and minor 0.

leds.h


 void init_leds(void);
 void led_on(int);
 void led_off( int );
 void led_toggle(int num);

 // ioctls -- 'M" is used for sound cards...
 // we don't got one so it seems safe
 #define N2_LM_OFF               _IOW('M',32,long)
 #define N2_LM_ON                _IOW('M',33,long)
 #define N2_LM_BLINK             _IOW('M',34,long)
 #define N2_LM_ALT               _IOW('M',35,long)
 #define N2_LM_ALL_ON            _IO('M',36)
 #define N2_LM_ALL_OFF           _IO('M',37)

 // The LED names for switches
 #define LED_RS_RED              0
 #define LED_RS_GRN              1
 #define LED_DISK1               2
 #define LED_DISK2               3
 #define LED_ALL                 4

 #define PHYS_LEDS               4

leds.c


 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 #include <errno.h>
 #include <string.h>
 #include <endian.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include "leds.h"

 static int leds;
 static int e1;
 static int toggle[PHYS_LEDS];


 void init_leds(void)
 {
   if ((leds = open("/dev/leds", O_RDWR)) < 0) {
     e1 = errno;
     if (e1 != ENOENT) {

       fprintf(stderr,
         "Error: Could not open LEDS device file '/dev/leds' : %s\n",
         strerror(e1));
       if (e1 == EACCES)
         fprintf(stderr,"Run as root\n");
     }
   }

   // Init toggle array
   for (e1 = 0; e1 < PHYS_LEDS; e1++)
      toggle[e1] = 0;

   // reset all leds to off
   if (ioctl(leds, N2_LM_ALL_OFF, 0) < 0) {
     fprintf(stderr,
             "Error: Could not access the leds: %s\n",
             strerror(errno));
   }

   printf("\n\rleds initialized.");
 }

 void led_on( int num )
 {
   if (ioctl(leds, N2_LM_ON, num) < 0) {
     fprintf(stderr,
             "Error: Could not access the leds: %s\n",
             strerror(errno));
   }
 }

 void led_off( int num )
 {
   if (ioctl(leds, N2_LM_OFF, num) < 0) {
     fprintf(stderr,
             "Error: Could not access the leds: %s\n",
             strerror(errno));
   }
 }

 void led_toggle(int num)
 {
   if (toggle[num])
   {
     e1 = ioctl(leds, N2_LM_OFF, num);
     toggle[num] = 0;
   }
   else
   {
     e1 = ioctl(leds, N2_LM_ON, num);
     toggle[num] = 1;
   }

   if (e1 < 0) {
     fprintf(stderr,
             "Error: Could not access the leds: %s\n",
             strerror(errno));
   }
 }

buzzer.h


 void init_buzzer(void);
 void buzz(int freq);

 //stop multi-beep at end of audible
 #define N2_BZ_BEEP_STOP  _IO('M',0)

 //one beep at current defaults
 #define N2_BZ_BEEP       _IO('M',1)

 //param beeps at current defaults
 #define N2_BZ_BEEPS      _IOW('M',3,long)

 //set tone: range is high=250 to low=2000
 #define N2_BZ_TONESET    _IOW('M',4,long)

 //ontime for multi-beeps in jiffies
 #define N2_BZ_ONTIME     _IOW('M',5,long)

 //offtime for multi-beeps in jiffies
 #define N2_BZ_SILENTTIME _IOW('M',6,long)

 //number of repeats for multi-beeps 0 = forever
 #define N2_BZ_REPEATCNT  _IOW('M',7,long)

 //combine all params in a long
 #define N2_BZ_COMBINED   _IOW('M',8,long)

buzzer.c


 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 #include <errno.h>
 #include <string.h>
 #include <endian.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <asm/system.h>
 #include <sys/ioctl.h>
 #include "buzzer.h"

 static int buzzer;
 static int e1;

 void init_buzzer(void)
 {
   if ((buzzer = open("/dev/buzzer", O_RDWR)) < 0) {
     e1 = errno;
     if (e1 != ENOENT) {
       fprintf(stderr,
         "Error: Could not open BUZZER device buzzer '/dev/buzzer' : %s\n",
         strerror(e1));
       if (e1 == EACCES)
         fprintf(stderr,"Run as root\n");
     }
   }

   printf("\n\rbuzzer initialized.");
 }

 void buzz(int freq)
 {
   // Set frequency
   if (ioctl(buzzer, N2_BZ_TONESET, freq) < 0 )
   {
     fprintf(stderr,
             "Error: Could not access the buzzer: %s\n",
             strerror(errno));
   }

   // do the beep
   if (ioctl(buzzer, N2_BZ_BEEP, 0) < 0)
   {
     fprintf(stderr,
             "Error: Could not access the buzzer: %s\n",
             strerror(errno));
   }
 }

Comments: My openslug seems to use a different standard. Sometimes the light turn off when I tell them to turn on and such. :( -T0ny

view · edit · print · history · Last edited by repvik.
Based on work by t0ny, repvik, and tman.
Originally by jbowler.
Page last modified on June 08, 2006, at 11:45 AM