From cbf6d642b5a593b971fcc8181cb692c539353d04 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 23 Jul 2013 20:25:46 +0100 Subject: [PATCH] Added in PiGlow devLib and a couple of examples for the PiGlow bumped version. --- devLib/Makefile | 6 +- devLib/piGlow.c | 117 ++++++++++++++++++++++++ devLib/piGlow.h | 36 ++++++++ examples/PiGlow/Makefile | 69 ++++++++++++++ examples/PiGlow/piGlow0.c | 51 +++++++++++ examples/PiGlow/piGlow1.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++ examples/piglow.c | 51 ----------- gpio/gpio.c | 2 +- 8 files changed, 502 insertions(+), 53 deletions(-) create mode 100644 devLib/piGlow.c create mode 100644 devLib/piGlow.h create mode 100644 examples/PiGlow/Makefile create mode 100644 examples/PiGlow/piGlow0.c create mode 100644 examples/PiGlow/piGlow1.c delete mode 100644 examples/piglow.c diff --git a/devLib/Makefile b/devLib/Makefile index a106d93..a3c0d42 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -42,7 +42,8 @@ LIBS = SRC = ds1302.c maxdetect.c piNes.c \ gertboard.c piFace.c \ - lcd128x64.c lcd.c + lcd128x64.c lcd.c \ + piGlow.c OBJ = $(SRC:.c=.o) @@ -86,6 +87,7 @@ install-headers: @install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include @install -m 0644 lcd128x64.h $(DESTDIR)$(PREFIX)/include @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 piGlow.h $(DESTDIR)$(PREFIX)/include .PHONEY: install install: $(DYNAMIC) install-headers @@ -111,6 +113,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/piFace.h @rm -f $(DESTDIR)$(PREFIX)/include/lcd128x64.h @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h + @rm -f $(DESTDIR)$(PREFIX)/include/piGlow.h @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.* @ldconfig @@ -128,3 +131,4 @@ gertboard.o: gertboard.h piFace.o: piFace.h lcd128x64.o: font.h lcd128x64.h lcd.o: lcd.h +piGlow.o: piGlow.h diff --git a/devLib/piGlow.c b/devLib/piGlow.c new file mode 100644 index 0000000..2036b7d --- /dev/null +++ b/devLib/piGlow.c @@ -0,0 +1,117 @@ +/* + * piGlow.c: + * Easy access to the Pimoroni PiGlow board. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#include "piGlow.h" + +static int myBase ; + +static int leg0 [6] = { 6, 7, 8, 5, 4, 9 } ; +static int leg1 [6] = { 17, 16, 15, 13, 11, 10 } ; +static int leg2 [6] = { 0, 1, 2, 3, 14, 12 } ; + + +/* + * piGlow1: + * Light up an individual LED + ********************************************************************************* + */ + +void piGlow1 (const int leg, const int ring, const int intensity) +{ + int *legLeds ; + + if ((leg < 0) || (leg > 2)) return ; + if ((ring < 0) || (ring > 5)) return ; + + /**/ if (leg == 0) + legLeds = leg0 ; + else if (leg == 1) + legLeds = leg1 ; + else + legLeds = leg2 ; + + analogWrite (myBase + legLeds [ring], intensity) ; +} + +/* + * piGlowLeg: + * Light up all 6 LEDs on a leg + ********************************************************************************* + */ + +void piGlowLeg (const int leg, const int intensity) +{ + int i ; + int *legLeds ; + + if ((leg < 0) || (leg > 2)) + return ; + + /**/ if (leg == 0) + legLeds = leg0 ; + else if (leg == 1) + legLeds = leg1 ; + else + legLeds = leg2 ; + + for (i = 0 ; i < 6 ; ++i) + analogWrite (myBase + legLeds [i], intensity) ; +} + + +/* + * piGlowRing: + * Light up 3 LEDs in a ring. Ring 0 is the outermost, 5 the innermost + ********************************************************************************* + */ + +void piGlowRing (const int ring, const int intensity) +{ + if ((ring < 0) || (ring > 5)) + return ; + + analogWrite (myBase + leg0 [ring], intensity) ; + analogWrite (myBase + leg1 [ring], intensity) ; + analogWrite (myBase + leg2 [ring], intensity) ; +} + +/* + * piGlowSetup: + * Initialise the board & remember the pins we're using + ********************************************************************************* + */ + +void piGlowSetup (const int pinBase) +{ + sn3218Setup (myBase = pinBase) ; + +// Turn all off to start with + + piGlowLeg (0, 0) ; + piGlowLeg (1, 0) ; + piGlowLeg (2, 0) ; +} diff --git a/devLib/piGlow.h b/devLib/piGlow.h new file mode 100644 index 0000000..5f107d6 --- /dev/null +++ b/devLib/piGlow.h @@ -0,0 +1,36 @@ +/* + * piglow.h: + * Easy access to the Pimoroni PiGlow board. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern void piGlow1 (const int leg, const int ring, const int intensity) ; +extern void piGlowLeg (const int leg, const int intensity) ; +extern void piGlowRing (const int ring, const int intensity) ; +extern void piGlowSetup (const int pinBase) ; + +#ifdef __cplusplus +} +#endif diff --git a/examples/PiGlow/Makefile b/examples/PiGlow/Makefile new file mode 100644 index 0000000..a09895b --- /dev/null +++ b/examples/PiGlow/Makefile @@ -0,0 +1,69 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012-2013 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# Wiring Compatable library for the Raspberry Pi +# +# wiringPi is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wiringPi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with wiringPi. If not, see . +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = piGlow0.c piGlow1.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +piGlow0: piGlow0.o + @echo [link] + @$(CC) -o $@ piGlow0.o $(LDFLAGS) $(LDLIBS) + +piGlow1: piGlow1.o + @echo [link] + @$(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS) + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + @echo "[Clean]" + @rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/PiGlow/piGlow0.c b/examples/PiGlow/piGlow0.c new file mode 100644 index 0000000..d3fe4b9 --- /dev/null +++ b/examples/PiGlow/piGlow0.c @@ -0,0 +1,51 @@ +/* + * piglow.c: + * Very simple demonstration of the PiGlow board. + * This uses the SN3218 directly - soon there will be a new PiGlow + * devLib device which will handle the PiGlow board on a more easy + * to use manner... + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#define LED_BASE 533 + +int main (void) +{ + int i, j ; + + wiringPiSetupSys () ; + + sn3218Setup (LED_BASE) ; + + for (;;) + { + for (i = 0 ; i < 256 ; ++i) + for (j = 0 ; j < 18 ; ++j) + analogWrite (LED_BASE + j, i) ; + + for (i = 255 ; i >= 0 ; --i) + for (j = 0 ; j < 18 ; ++j) + analogWrite (LED_BASE + j, i) ; + } +} diff --git a/examples/PiGlow/piGlow1.c b/examples/PiGlow/piGlow1.c new file mode 100644 index 0000000..068564d --- /dev/null +++ b/examples/PiGlow/piGlow1.c @@ -0,0 +1,223 @@ +/* + * piGlow1.c: + * Very simple demonstration of the PiGlow board. + * This uses the piGlow devLib. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#define PIGLOW_BASE 533 + + +/* + * keypressed: clearKeypressed: + * Simple but effective ways to tell if the enter key has been pressed + ********************************************************************************* + */ + +static int keypressed (void) +{ + struct pollfd polls ; + + polls.fd = fileno (stdin) ; + polls.events = POLLIN ; + + return poll (&polls, 1, 0) != 0 ; +} + +static void clearKeypressed (void) +{ + while (keypressed ()) + (void)getchar () ; +} + + +/* + * pulseLed: + * Pulses the LED at position leg, ring from off to a max. value, + * then off again + ********************************************************************************* + */ + +static void pulseLed (int leg, int ring) +{ + int i ; + + for (i = 0 ; i < 140 ; ++i) + { + piGlow1 (leg, ring, i) ; + delay (1) ; + } + delay (10) ; + for (i = 140 ; i >= 0 ; --i) + { + piGlow1 (leg, ring, i) ; + delay (1) ; + } +} + +/* + * pulseLeg: + * Same as above, but a whole leg at a time + ********************************************************************************* + */ + +static void pulseLeg (int leg) +{ + int i ; + + for (i = 0 ; i < 140 ; ++i) + { + piGlowLeg (leg, i) ; delay (1) ; + } + delay (10) ; + for (i = 140 ; i >= 0 ; --i) + { + piGlowLeg (leg, i) ; delay (1) ; + } +} + + +/* + * pulse Ring: + * Same as above, but a whole ring at a time + ********************************************************************************* + */ + +static void pulseRing (int ring) +{ + int i ; + + for (i = 0 ; i < 140 ; ++i) + { + piGlowRing (ring, i) ; delay (1) ; + } + delay (10) ; + for (i = 140 ; i >= 0 ; --i) + { + piGlowRing (ring, i) ; delay (1) ; + } +} + + +/* + * main: + * Our little demo prgoram + ********************************************************************************* + */ + +int main (void) +{ + int i ; + int ring, leg ; + +// Always initialise wiringPi: +// Use the Sys method if you don't need to run as root + + wiringPiSetupSys () ; + +// Initialise the piGlow devLib with our chosen pin base + + piGlowSetup (PIGLOW_BASE) ; + +// LEDs, one at a time + + printf ("LEDs, one at a time\n") ; + for (; !keypressed () ;) + for (leg = 0 ; leg < 3 ; ++leg) + { + for (ring = 0 ; ring < 6 ; ++ring) + { + pulseLed (leg, ring) ; + if (keypressed ()) + break ; + } + if (keypressed ()) + break ; + } + clearKeypressed () ; + +// Rings, one at a time + + printf ("Rings, one at a time\n") ; + for (; !keypressed () ;) + for (ring = 0 ; ring < 6 ; ++ring) + { + pulseRing (ring) ; + if (keypressed ()) + break ; + } + clearKeypressed () ; + +// Legs, one at a time + + printf ("Legs, one at a time\n") ; + for (; !keypressed () ;) + for (leg = 0 ; leg < 3 ; ++leg) + { + pulseLeg (leg) ; + if (keypressed ()) + break ; + } + clearKeypressed () ; + + delay (1000) ; + +// Sequence - alternating rings, legs and random + + printf ("Sequence now\n") ; + + for (; !keypressed () ;) + { + for (i = 0 ; i < 20 ; ++i) + for (leg = 2 ; leg >= 0 ; --leg) + { + piGlowLeg (leg, 100) ; + delay (100) ; + piGlowLeg (leg, 0) ; + } + + for (i = 0 ; i < 20 ; ++i) + for (ring = 5 ; ring >= 0 ; --ring) + { + piGlowRing (ring, 100) ; + delay (50) ; + piGlowRing (ring, 0) ; + } + + for (i = 0 ; i < 1000 ; ++i) + { + leg = random () % 3 ; + ring = random () % 6 ; + piGlow1 (leg, ring, random () % 256) ; + delay (5) ; + piGlow1 (leg, ring, 0) ; + } + } + + return 0 ; +} diff --git a/examples/piglow.c b/examples/piglow.c deleted file mode 100644 index d3fe4b9..0000000 --- a/examples/piglow.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * piglow.c: - * Very simple demonstration of the PiGlow board. - * This uses the SN3218 directly - soon there will be a new PiGlow - * devLib device which will handle the PiGlow board on a more easy - * to use manner... - * - * Copyright (c) 2013 Gordon Henderson. - *********************************************************************** - * This file is part of wiringPi: - * https://projects.drogon.net/raspberry-pi/wiringpi/ - * - * wiringPi is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * wiringPi is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with wiringPi. If not, see . - *********************************************************************** - */ - -#include -#include - -#define LED_BASE 533 - -int main (void) -{ - int i, j ; - - wiringPiSetupSys () ; - - sn3218Setup (LED_BASE) ; - - for (;;) - { - for (i = 0 ; i < 256 ; ++i) - for (j = 0 ; j < 18 ; ++j) - analogWrite (LED_BASE + j, i) ; - - for (i = 255 ; i >= 0 ; --i) - for (j = 0 ; j < 18 ; ++j) - analogWrite (LED_BASE + j, i) ; - } -} diff --git a/gpio/gpio.c b/gpio/gpio.c index dced95c..ebbb377 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.09" +#define VERSION "2.10" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ;