Browse Source

Remove piFace, Gertboard, scrollPhat examples.

pull/158/head
Jim Parziale 2 years ago
parent
commit
6ab6fa00be
23 changed files with 255 additions and 2505 deletions
  1. +0
    -112
      devLib/piFace.c
  2. +0
    -32
      devLib/piFace.h
  3. +0
    -118
      devLib/piGlow.c
  4. +0
    -45
      devLib/piGlow.h
  5. +0
    -430
      devLib/scrollPhat.c
  6. +0
    -39
      devLib/scrollPhat.h
  7. +1
    -24
      examples/Gertboard/Makefile
  8. +0
    -96
      examples/Gertboard/gertboard.c
  9. +0
    -60
      examples/Gertboard/record.c
  10. +0
    -78
      examples/Gertboard/temperature.c
  11. +0
    -73
      examples/Gertboard/voltmeter.c
  12. +0
    -152
      examples/Gertboard/vumeter.c
  13. +0
    -82
      examples/PiGlow/Makefile
  14. +0
    -51
      examples/PiGlow/piGlow0.c
  15. +0
    -258
      examples/PiGlow/piGlow1.c
  16. +0
    -176
      examples/PiGlow/piglow.c
  17. +0
    -79
      examples/scrollPhat/Makefile
  18. +0
    -230
      examples/scrollPhat/scphat.c
  19. +0
    -115
      examples/scrollPhat/test.c
  20. +8
    -8
      wiringPi/wiringPi.c
  21. +1
    -1
      wiringPi/wiringPi.h
  22. +38
    -38
      wiringPiD/daemonise.c
  23. +207
    -208
      wiringPiD/wiringpid.c

+ 0
- 112
devLib/piFace.c View File

@@ -1,112 +0,0 @@
/*
* piFace.:
* This file to interface with the PiFace peripheral device which
* has an MCP23S17 GPIO device connected via the SPI bus.
*
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/


#include <stdio.h>
#include <stdint.h>

#include <wiringPi.h>
#include <mcp23s17.h>

#include "piFace.h"


/*
* myDigitalWrite:
* Perform the digitalWrite function on the PiFace board
*********************************************************************************
*/

void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
digitalWrite (pin + 16, value) ;
}


/*
* myDigitalRead:
* Perform the digitalRead function on the PiFace board
* With a slight twist - if we read from base + 8, then we
* read from the output latch...
*********************************************************************************
*/

int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
if ((pin - node->pinBase) >= 8)
return digitalRead (pin + 8) ;
else
return digitalRead (pin + 16 + 8) ;
}


/*
* myPullUpDnControl:
* Perform the pullUpDnControl function on the PiFace board
*********************************************************************************
*/

void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
{
pullUpDnControl (pin + 16 + 8, pud) ;
}


/*
* piFaceSetup
* We're going to create an instance of the mcp23s17 here, then
* provide our own read/write routines on-top of it...
* The supplied PiFace code (in Pithon) treats it as an 8-bit device
* where you write the output ports and read the input port using the
* same pin numbers, however I have had a request to be able to read
* the output port, so reading 8..15 will read the output latch.
*********************************************************************************
*/

int piFaceSetup (const int pinBase)
{
int i ;
struct wiringPiNodeStruct *node ;

// Create an mcp23s17 instance:

mcp23s17Setup (pinBase + 16, 0, 0) ;

// Set the direction bits

for (i = 0 ; i < 8 ; ++i)
{
pinMode (pinBase + 16 + i, OUTPUT) ; // Port A is the outputs
pinMode (pinBase + 16 + 8 + i, INPUT) ; // Port B inputs.
}

node = wiringPiNewNode (pinBase, 16) ;
node->digitalRead = myDigitalRead ;
node->digitalWrite = myDigitalWrite ;
node->pullUpDnControl = myPullUpDnControl ;

return 0 ;
}

+ 0
- 32
devLib/piFace.h View File

@@ -1,32 +0,0 @@
/*
* piFace.h:
* Control the PiFace Interface board for the Raspberry Pi
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#ifdef __cplusplus
extern "C" {
#endif

extern int piFaceSetup (const int pinBase) ;

#ifdef __cplusplus
}
#endif

+ 0
- 118
devLib/piGlow.c View File

@@ -1,118 +0,0 @@
/*
* piGlow.c:
* Easy access to the Pimoroni PiGlow board.
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <wiringPi.h>
#include <sn3218.h>

#include "piGlow.h"

#define PIGLOW_BASE 577

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 (PIGLOW_BASE + 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 (PIGLOW_BASE + 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 (PIGLOW_BASE + leg0 [ring], intensity) ;
analogWrite (PIGLOW_BASE + leg1 [ring], intensity) ;
analogWrite (PIGLOW_BASE + leg2 [ring], intensity) ;
}

/*
* piGlowSetup:
* Initialise the board & remember the pins we're using
*********************************************************************************
*/

void piGlowSetup (int clear)
{
sn3218Setup (PIGLOW_BASE) ;

if (clear)
{
piGlowLeg (0, 0) ;
piGlowLeg (1, 0) ;
piGlowLeg (2, 0) ;
}
}

+ 0
- 45
devLib/piGlow.h View File

@@ -1,45 +0,0 @@
/*
* piglow.h:
* Easy access to the Pimoroni PiGlow board.
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/


#define PIGLOW_RED 0
#define PIGLOW_ORANGE 1
#define PIGLOW_YELLOW 2
#define PIGLOW_GREEN 3
#define PIGLOW_BLUE 4
#define PIGLOW_WHITE 5


#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 (int clear) ;

#ifdef __cplusplus
}
#endif

+ 0
- 430
devLib/scrollPhat.c View File

@@ -1,430 +0,0 @@
/*
* scrollPhat.c:
* Simple driver for the Pimoroni Scroll Phat device
*
* Copyright (c) 2015 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#include <wiringPiI2C.h>

#include "scrollPhatFont.h"
#include "scrollPhat.h"

// Size

#define SP_WIDTH 11
#define SP_HEIGHT 5

// I2C

#define PHAT_I2C_ADDR 0x60

// Software copy of the framebuffer
// it's 8-bit deep although the display itself is only 1-bit deep.

static unsigned char frameBuffer [SP_WIDTH * SP_HEIGHT] ;

static int lastX, lastY ;
static int printDelayFactor ;
static int scrollPhatFd ;

static int putcharX ;

#undef DEBUG


/*
* delay:
* Wait for some number of milliseconds.
* This taken from wiringPi as there is no-need to include the whole of
* wiringPi just for the delay function.
*********************************************************************************
*/

static void delay (unsigned int howLong)
{
struct timespec sleeper, dummy ;

sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;

nanosleep (&sleeper, &dummy) ;
}



/*
* scrollPhatUpdate:
* Copy our software version to the real display
*********************************************************************************
*/

void scrollPhatUpdate (void)
{
register int x, y ;
register unsigned char data, pixel ;
unsigned char pixels [SP_WIDTH] ;

#ifdef DEBUG
printf ("+-----------+\n") ;
for (y = 0 ; y < SP_HEIGHT ; ++y)
{
putchar ('|') ;
for (x = 0 ; x < SP_WIDTH ; ++x)
{
pixel = frameBuffer [x + y * SP_WIDTH] ;
putchar (pixel == 0 ? ' ' : '*') ;
}
printf ("|\n") ;
}
printf ("+-----------+\n") ;
#endif

for (x = 0 ; x < SP_WIDTH ; ++x)
{
data = 0 ;
for (y = 0 ; y < SP_HEIGHT ; ++y)
{
pixel = frameBuffer [x + y * SP_WIDTH] ;
data = (data << 1) | ((pixel == 0) ? 0 : 1) ;
}
pixels [x] = data ;
}

for (x = 0 ; x < SP_WIDTH ; ++x)
wiringPiI2CWriteReg8 (scrollPhatFd, 1 + x, pixels [x]) ;

wiringPiI2CWriteReg8 (scrollPhatFd, 0x0C, 0) ;
}


/*
*********************************************************************************
* Standard Graphical Functions
*********************************************************************************
*/


/*
* scrollPhatPoint:
* Plot a pixel. Crude clipping - speed is not the essence here.
*********************************************************************************
*/

void scrollPhatPoint (int x, int y, int colour)
{
lastX = x ;
lastY = y ;

if ((x < 0) || (x >= SP_WIDTH) || (y < 0) || (y >= SP_HEIGHT))
return ;

frameBuffer [x + y * SP_WIDTH] = colour ;
}


/*
* scrollPhatLine: scrollPhatLineTo:
* Classic Bressenham Line code - rely on the point function to do the
* clipping for us here.
*********************************************************************************
*/

void scrollPhatLine (int x0, int y0, int x1, int y1, int colour)
{
int dx, dy ;
int sx, sy ;
int err, e2 ;

lastX = x1 ;
lastY = y1 ;

dx = abs (x1 - x0) ;
dy = abs (y1 - y0) ;

sx = (x0 < x1) ? 1 : -1 ;
sy = (y0 < y1) ? 1 : -1 ;

err = dx - dy ;
for (;;)
{
scrollPhatPoint (x0, y0, colour) ;

if ((x0 == x1) && (y0 == y1))
break ;

e2 = 2 * err ;

if (e2 > -dy)
{
err -= dy ;
x0 += sx ;
}

if (e2 < dx)
{
err += dx ;
y0 += sy ;
}
}

}

void scrollPhatLineTo (int x, int y, int colour)
{
scrollPhatLine (lastX, lastY, x, y, colour) ;
}


/*
* scrollPhatRectangle:
* A rectangle is a spoilt days fishing
*********************************************************************************
*/

void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled)
{
register int x ;

if (filled)
{
/**/ if (x1 == x2)
scrollPhatLine (x1, y1, x2, y2, colour) ;
else if (x1 < x2)
for (x = x1 ; x <= x2 ; ++x)
scrollPhatLine (x, y1, x, y2, colour) ;
else
for (x = x2 ; x <= x1 ; ++x)
scrollPhatLine (x, y1, x, y2, colour) ;
}
else
{
scrollPhatLine (x1, y1, x2, y1, colour) ;
scrollPhatLineTo (x2, y2, colour) ;
scrollPhatLineTo (x1, y2, colour) ;
scrollPhatLineTo (x1, y1, colour) ;
}
}


/*
* scrollPhatPutchar:
* Print a single character to the screen then advance the pointer by an
* appropriate ammount (variable width font).
* We rely on the clipping done by the pixel plot function to keep us
* out of trouble.
* Return the width + space
*********************************************************************************
*/

int scrollPhatPutchar (int c)
{
register int x, y ;

unsigned char line ;
unsigned char *fontPtr ;
unsigned char *p2 ;
int lineWidth, width, mask ;

// The font is printable characters, uppercase only...
// and somewhat varaible width...

c &= 0x7F ;
if (c > 0x60)
c -= 64 ;
else
c -= 32 ;

fontPtr = scrollPhatFont + c * fontHeight ;

// Work out width of this character
// There probably is a more efficient way to do this, but...

p2 = fontPtr ;
width = 0 ;
for (y = 0 ; y < fontHeight ; ++y)
{
mask = 0x80 ;
for (lineWidth = 8 ; lineWidth > 0 ; --lineWidth)
{
if ((*p2 & mask) != 0)
break ;
mask >>= 1 ;
}
if (lineWidth > width)
width = lineWidth ;

++p2 ;
}

if (width == 0) // Likely to be a blank or space character
width = 3 ;

for (y = fontHeight - 1 ; y >= 0 ; --y)
{
x = 0 ;
line = *fontPtr++ ;
for (mask = 1 << (width - 1) ; mask != 0 ; mask >>= 1)
{
scrollPhatPoint (putcharX + x, y, (line & mask)) ;
++x ;
}
}

// make a line of space

for (y = fontHeight - 1 ; y >= 0 ; --y)
scrollPhatPoint (putcharX + width, y, 0) ;

putcharX = putcharX + width + 1 ;

return width + 1 ;
}


/*
* scrollPhatPuts:
* Send a string to the display - and scroll it across.
* This is somewhat of a hack in that we print the entire string to the
* display and let the point clipping take care of what's off-screen...
*********************************************************************************
*/

void scrollPhatPuts (const char *str)
{
int i ;
int movingX = 0 ;
const char *s ;
int pixelLen ;

// Print it once, then we know the width in pixels...

putcharX = 0 ;
s = str ;
while (*s)
scrollPhatPutchar (*s++) ;

pixelLen = putcharX ;

// Now scroll it by printing it and moving left one pixel

movingX = 0 ;
for (i = 0 ; i < pixelLen ; ++i)
{
putcharX = movingX ;
s = str ;
while (*s)
scrollPhatPutchar (*s++) ;
--movingX ;
scrollPhatUpdate () ;
delay (printDelayFactor) ;
}
}


/*
* scrollPhatPrintf:
* Does what it says
*********************************************************************************
*/

void scrollPhatPrintf (const char *message, ...)
{
va_list argp ;
char buffer [1024] ;

va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;

scrollPhatPuts (buffer) ;
}


/*
* scrollPhatPrintSpeed:
* Change the print speed - mS per shift by 1 pixel
*********************************************************************************
*/

void scrollPhatPrintSpeed (const int pps)
{
if (pps < 0)
printDelayFactor = 0 ;
else
printDelayFactor = pps ;
}


/*
* scrollPhatClear:
* Clear the display
*********************************************************************************
*/

void scrollPhatClear (void)
{
register int i ;
register unsigned char *ptr = frameBuffer ;

for (i = 0 ; i < (SP_WIDTH * SP_HEIGHT) ; ++i)
*ptr++ = 0 ;

scrollPhatUpdate () ;
}


/*
* scrollPhatIntensity:
* Set the display brightness - percentage
*********************************************************************************
*/

void scrollPhatIntensity (const int percent)
{
wiringPiI2CWriteReg8 (scrollPhatFd, 0x19, (127 * percent) / 100) ;
}


/*
* scrollPhatSetup:
* Initialise the Scroll Phat display
*********************************************************************************
*/

int scrollPhatSetup (void)
{
if ((scrollPhatFd = wiringPiI2CSetup (PHAT_I2C_ADDR)) < 0)
return scrollPhatFd ;

wiringPiI2CWriteReg8 (scrollPhatFd, 0x00, 0x03) ; // Enable display, set to 5x11 mode
scrollPhatIntensity (10) ;
scrollPhatClear () ;
scrollPhatPrintSpeed (100) ;

return 0 ;
}

+ 0
- 39
devLib/scrollPhat.h View File

@@ -1,39 +0,0 @@
/*
* scrollPhat.h:
* Simple driver for the Pimoroni Scroll Phat device
*
* Copyright (c) 2015 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

extern void scrollPhatPoint (int x, int y, int colour) ;
extern void scrollPhatLine (int x0, int y0, int x1, int y1, int colour) ;
extern void scrollPhatLineTo (int x, int y, int colour) ;
extern void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled) ;
extern void scrollPhatUpdate (void) ;
extern void scrollPhatClear (void) ;

extern int scrollPhatPutchar (int c) ;
//extern void scrollPhatPutchar (int c) ;
extern void scrollPhatPuts (const char *str) ;
extern void scrollPhatPrintf (const char *message, ...) ;
extern void scrollPhatPrintSpeed (const int cps10) ;

extern void scrollPhatIntensity (const int percent) ;
extern int scrollPhatSetup (void) ;

+ 1
- 24
examples/Gertboard/Makefile View File

@@ -21,10 +21,7 @@ LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm
# Should not alter anything below this line
###############################################################################

SRC = gertboard.c \
buttons.c 7segments.c \
voltmeter.c temperature.c vumeter.c \
record.c
SRC = buttons.c 7segments.c

OBJ = $(SRC:.c=.o)

@@ -32,10 +29,6 @@ BINS = $(SRC:.c=)

all: $(BINS)

gertboard: gertboard.o
$Q echo [link]
$Q $(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS)

buttons: buttons.o
$Q echo [link]
$Q $(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS)
@@ -44,22 +37,6 @@ buttons: buttons.o
$Q echo [link]
$Q $(CC) -o $@ 7segments.o $(LDFLAGS) $(LDLIBS)

voltmeter: voltmeter.o
$Q echo [link]
$Q $(CC) -o $@ voltmeter.o $(LDFLAGS) $(LDLIBS)

temperature: temperature.o
$Q echo [link]
$Q $(CC) -o $@ temperature.o $(LDFLAGS) $(LDLIBS)

vumeter: vumeter.o
$Q echo [link]
$Q $(CC) -o $@ vumeter.o $(LDFLAGS) $(LDLIBS)

record: record.o
$Q echo [link]
$Q $(CC) -o $@ record.o $(LDFLAGS) $(LDLIBS)

.c.o:
$Q echo [CC] $<
$Q $(CC) -c $(CFLAGS) $< -o $@


+ 0
- 96
examples/Gertboard/gertboard.c View File

@@ -1,96 +0,0 @@
/*
* gertboard.c:
* Simple test for the SPI bus on the Gertboard
*
* Hardware setup:
* D/A port 0 jumpered to A/D port 0.
*
* We output a sine wave on D/A port 0 and sample A/D port 0. We then
* plot the input value on the terminal as a sort of vertical scrolling
* oscilloscope.
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <math.h>

// Gertboard D to A is an 8-bit unit.

#define B_SIZE 256

#include <wiringPi.h>
#include <gertboard.h>

int main (void)
{
double angle ;
int i, inputValue ;
int buffer [B_SIZE] ;
int cols ;
struct winsize w ;


printf ("Raspberry Pi Gertboard SPI test program\n") ;
printf ("=======================================\n") ;

ioctl (fileno (stdin), TIOCGWINSZ, &w);
cols = w.ws_col - 2 ;

// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root

wiringPiSetupSys () ;

// Initialise the Gertboard analog hardware at pin 100

gertboardAnalogSetup (100) ;

// Generate a Sine Wave and store in our buffer

for (i = 0 ; i < B_SIZE ; ++i)
{
angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
}

// Loop, output the sine wave on analog out port 0, read it into A-D port 0
// and display it on the screen

for (;;)
{
for (i = 0 ; i < B_SIZE ; ++i)
{
analogWrite (100, buffer [i]) ;

inputValue = analogRead (100) ;

// We don't need to wory about the scale or sign - the analog hardware is
// a 10-bit value, so 0-1023. Just scale this to our terminal

printf ("%*s\n", (inputValue * cols) / 1023, "*") ;
delay (2) ;
}
}

return 0 ;
}

+ 0
- 60
examples/Gertboard/record.c View File

@@ -1,60 +0,0 @@
/*
* record.c:
* Record some audio via the Gertboard
*
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
*/

#include <stdio.h>
#include <sys/time.h>

#include <wiringPi.h>
#include <gertboard.h>

#define B_SIZE 40000

int main ()
{
int i ;
struct timeval tStart, tEnd, tTaken ;
unsigned char buffer [B_SIZE] ;

printf ("\n") ;
printf ("Gertboard demo: Recorder\n") ;
printf ("========================\n") ;

// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root

wiringPiSetupSys () ;

// Initialise the Gertboard analog hardware at pin 100

gertboardAnalogSetup (100) ;

gettimeofday (&tStart, NULL) ;

for (i = 0 ; i < B_SIZE ; ++i)
buffer [i] = analogRead (100) >> 2 ;

gettimeofday (&tEnd, NULL) ;
timersub (&tEnd, &tStart, &tTaken) ;

printf ("Time taken for %d reads: %ld.%ld\n", B_SIZE, tTaken.tv_sec, tTaken.tv_usec) ;

gettimeofday (&tStart, NULL) ;

for (i = 0 ; i < B_SIZE ; ++i)
analogWrite (100, buffer [i]) ;

gettimeofday (&tEnd, NULL) ;
timersub (&tEnd, &tStart, &tTaken) ;

printf ("Time taken for %d writes: %ld.%ld\n", B_SIZE, tTaken.tv_sec, tTaken.tv_usec) ;

return 0 ;
}


+ 0
- 78
examples/Gertboard/temperature.c View File

@@ -1,78 +0,0 @@
/*
* temperature.c:
* Demonstrate use of the Gertboard A to D converter to make
* a simple thermometer using the LM35.
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>

#include <wiringPi.h>
#include <gertboard.h>

int main ()
{
int x1, x2 ;
double v1, v2 ;

printf ("\n") ;
printf ("Gertboard demo: Simple Thermemeter\n") ;
printf ("==================================\n") ;

// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root

wiringPiSetupSys () ;

// Initialise the Gertboard analog hardware at pin 100

gertboardAnalogSetup (100) ;

printf ("\n") ;
printf ("| Channel 0 | Channel 1 | Temperature 1 | Temperature 2 |\n") ;

for (;;)
{

// Read the 2 channels:

x1 = analogRead (100) ;
x2 = analogRead (101) ;

// Convert to a voltage:

v1 = (double)x1 / 1023.0 * 3.3 ;
v2 = (double)x2 / 1023.0 * 3.3 ;

// Print

printf ("| %6.3f | %6.3f |", v1, v2) ;

// Print Temperature of both channels by converting the LM35 reading
// to a temperature. Fortunately these are easy: 0.01 volts per C.

printf (" %4.1f | %4.1f |\r", v1 * 100.0, v2 * 100.0) ;
fflush (stdout) ;
}

return 0 ;
}


+ 0
- 73
examples/Gertboard/voltmeter.c View File

@@ -1,73 +0,0 @@
/*
* voltmeter.c:
* Demonstrate use of the Gertboard A to D converter to make
* a simple voltmeter.
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>

#include <wiringPi.h>
#include <gertboard.h>

int main ()
{
int x1, x2 ;
double v1, v2 ;

printf ("\n") ;
printf ("Gertboard demo: Simple Voltmeters\n") ;
printf ("=================================\n") ;

// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root

wiringPiSetupSys () ;

// Initialise the Gertboard analog hardware at pin 100

gertboardAnalogSetup (100) ;

printf ("\n") ;
printf ("| Channel 0 | Channel 1 |\n") ;

for (;;)
{

// Read the 2 channels:

x1 = analogRead (100) ;
x2 = analogRead (101) ;

// Convert to a voltage:

v1 = (double)x1 / 1023.0 * 3.3 ;
v2 = (double)x2 / 1023.0 * 3.3 ;

// Print

printf ("| %6.3f | %6.3f |\r", v1, v2) ;
fflush (stdout) ;
}

return 0 ;
}


+ 0
- 152
examples/Gertboard/vumeter.c View File

@@ -1,152 +0,0 @@
/*
* vumeter.c:
* Simple VU meter
*
* Heres the theory:
* We will sample at 4000 samples/sec and put the data into a
* low-pass filter with a depth of 1000 samples. This will give
* us 1/4 a second of lag on the signal, but I think it might
* produce a more pleasing output.
*
* The input of the microphone should be at mid-pont with no
* sound input, but we might have to sample that too, to get
* our reference zero...
*
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#include <wiringPi.h>
#include <gertboard.h>

#ifndef TRUE
#define TRUE (1==1)
#define FALSE (!TRUE)
#endif

#define B_SIZE 1000
#define S_SIZE 128

static int buffer [B_SIZE] ;
static int bPtr = 0 ;

/*
* ledPercent:
* Output the given value as a percentage on the LEDs
*********************************************************************************
*/

static void ledPercent (int percent)
{
unsigned int output = 0 ;

if (percent > 11) output |= 0x01 ;
if (percent > 22) output |= 0x02 ;
if (percent > 33) output |= 0x04 ;
if (percent > 44) output |= 0x08 ;
if (percent > 55) output |= 0x10 ;
if (percent > 66) output |= 0x20 ;
if (percent > 77) output |= 0x40 ;
if (percent > 88) output |= 0x80 ;

digitalWriteByte (output) ;
}

static unsigned int tPeriod, tNextSampleTime ;

/*
* sample:
* Get a sample from the Gertboard. If not enough time has elapsed
* since the last sample, then wait...
*********************************************************************************
*/

static void sample (void)
{
unsigned int tFuture ;

// Calculate the future sample time

tFuture = tPeriod + tNextSampleTime ;

// Wait until the next sample time

while (micros () < tNextSampleTime)
;
buffer [bPtr] = gertboardAnalogRead (0) ;

tNextSampleTime = tFuture ;
}


int main ()
{
int quietLevel, min, max ;
int i, sum ;
unsigned int tStart, tEnd ;

printf ("\n") ;
printf ("Gertboard demo: VU Meter\n") ;
printf ("========================\n") ;

wiringPiSetup () ;
gertboardSPISetup () ;

ledPercent (0) ;
for (i = 0 ; i < 8 ; ++i)
pinMode (i, OUTPUT) ;

for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr)
buffer [bPtr] = 99 ;

tPeriod = 1000000 / 1000 ;

printf ("Shhhh.... ") ; fflush (stdout) ;
delay (1000) ;
printf ("Sampling quiet... ") ; fflush (stdout) ;

tStart = micros () ;

tNextSampleTime = micros () ;
for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr)
sample () ;

tEnd = micros () ;

quietLevel = 0 ;
max = 0 ;
min = 1024 ;
for (i = 0 ; i < B_SIZE ; ++i)
{
quietLevel += buffer [i] ;
if (buffer [i] > max) max = buffer [i] ;
if (buffer [i] < min) min = buffer [i] ;
}
quietLevel /= B_SIZE ;

printf ("Done. Quiet level is: %d [%d:%d] [%d:%d]\n", quietLevel, min, max, quietLevel - min, max - quietLevel) ;

printf ("Time taken for %d reads: %duS\n", B_SIZE, tEnd - tStart) ;

for (bPtr = 0 ;;)
{
sample () ;
sum = 0 ;
for (i = 0 ; i < S_SIZE ; ++i)
sum += buffer [i] ;
sum /= S_SIZE ;
sum = abs (quietLevel - sum) ;
sum = (sum * 1000) / quietLevel ;
ledPercent (sum) ;
if (++bPtr > S_SIZE)
bPtr = 0 ;
}


return 0 ;
}

+ 0
- 82
examples/PiGlow/Makefile View File

@@ -1,82 +0,0 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://github.com/wiringPi/wiringPi
#
# Copyright (c) 2012-2015 Gordon Henderson
#################################################################################
# This file is part of wiringPi:
# A "wiring" 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 <http://www.gnu.org/licenses/>.
#################################################################################

ifneq ($V,1)
Q ?= @
endif

#DEBUG = -g -O0
DEBUG = -O3
CC ?= gcc
INCLUDE = -I/usr/local/include
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe $(EXTRA_CFLAGS)

LDFLAGS = -L/usr/local/lib
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm

# Should not alter anything below this line
###############################################################################

SRC = piGlow0.c piGlow1.c piglow.c

OBJ = $(SRC:.c=.o)

BINS = $(SRC:.c=)

all: $(BINS)

piGlow0: piGlow0.o
$Q echo [link]
$Q $(CC) -o $@ piGlow0.o $(LDFLAGS) $(LDLIBS)

piGlow1: piGlow1.o
$Q echo [link]
$Q $(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS)

piglow: piglow.o
$Q echo [link]
$Q $(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS)

.c.o:
$Q echo [CC] $<
$Q $(CC) -c $(CFLAGS) $< -o $@

clean:
$Q echo "[Clean]"
$Q rm -f $(OBJ) *~ core tags $(BINS)

tags: $(SRC)
$Q echo [ctags]
$Q ctags $(SRC)

install: piglow
$Q echo Installing piglow into /usr/local/bin
$Q cp -a piglow /usr/local/bin/piglow
$Q chmod 755 /usr/local/bin/piglow
$Q echo Done. Remember to load the I2C drivers!

depend:
makedepend -Y $(SRC)

# DO NOT DELETE

+ 0
- 51
examples/PiGlow/piGlow0.c View File

@@ -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://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <wiringPi.h>
#include <sn3218.h>

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

+ 0
- 258
examples/PiGlow/piGlow1.c View File

@@ -1,258 +0,0 @@
/*
* 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://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <poll.h>

#include <wiringPi.h>
#include <piGlow.h>

#define PIGLOW_BASE 533

#ifndef TRUE
# define TRUE (1==1)
# define FALSE (!TRUE)
#endif


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

#define LEG_STEPS 3

static int legSequence [] =
{
4, 12, 99,
99, 4, 12,
12, 99, 4,
} ;

#define RING_STEPS 16

static int ringSequence [] =
{
0, 0, 0, 0, 0, 64,
0, 0, 0, 0, 64, 64,
0, 0, 0, 64, 64, 0,
0, 0, 64, 64, 0, 0,
0, 64, 64, 0, 0, 0,
64, 64, 0, 0, 0, 0,
64, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
64, 0, 0, 0, 0, 0,
64, 64, 0, 0, 0, 0,
0, 64, 64, 0, 0, 0,
0, 0, 64, 64, 0, 0,
0, 0, 0, 64, 64, 0,
0, 0, 0, 0, 64, 64,
0, 0, 0, 0, 0, 64,
0, 0, 0, 0, 0, 0,
} ;

/*
* main:
* Our little demo prgoram
*********************************************************************************
*/

int main (void)
{
int i ;
int step, 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 (1) ;

// 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 (step = 0 ; step < LEG_STEPS ; ++step)
{
for (leg = 0 ; leg < 3 ; ++leg)
piGlowLeg (leg, legSequence [step * 3 + leg]) ;
delay (80) ;
}

for (i = 0 ; i < 10 ; ++i)
for (step = 0 ; step < RING_STEPS ; ++step)
{
for (ring = 0 ; ring < 6 ; ++ring)
piGlowRing (ring, ringSequence [step * 6 + ring]) ;
delay (80) ;
}

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

+ 0
- 176
examples/PiGlow/piglow.c View File

@@ -1,176 +0,0 @@
/*
* piglow.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://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef TRUE
# define TRUE (1==1)
# define FALSE (!TRUE)
#endif

#include <wiringPi.h>
#include <piGlow.h>

static void failUsage (void)
{
fprintf (stderr, "Usage examples:\n") ;
fprintf (stderr, " piglow off # All off\n") ;
fprintf (stderr, " piglow red 50 # Light the 3 red LEDs to 50%%\n") ;
fprintf (stderr, " colours are: red, yellow, orange, green, blue and white\n") ;
fprintf (stderr, " piglow all 75 # Light all to 75%%\n") ;
fprintf (stderr, " piglow leg 0 25 # Light leg 0 to 25%%\n") ;
fprintf (stderr, " piglow ring 3 100 # Light ring 3 to 100%%\n") ;
fprintf (stderr, " piglow led 2 5 100 # Light the single LED on Leg 2, ring 5 to 100%%\n") ;

exit (EXIT_FAILURE) ;
}

static int getPercent (char *typed)
{
int percent ;

percent = atoi (typed) ;
if ((percent < 0) || (percent > 100))
{
fprintf (stderr, "piglow: percent value out of range\n") ;
exit (EXIT_FAILURE) ;
}
return (percent * 255) / 100 ;
}


/*
* main:
* Our little demo prgoram
*********************************************************************************
*/

int main (int argc, char *argv [])
{
int percent ;
int ring, leg ;

// Always initialise wiringPi:
// Use the Sys method if you don't need to run as root

wiringPiSetupSys () ;

// Initialise the piGlow devLib

piGlowSetup (FALSE) ;

if (argc == 1)
failUsage () ;

if ((argc == 2) && (strcasecmp (argv [1], "off") == 0))
{
for (leg = 0 ; leg < 3 ; ++leg)
piGlowLeg (leg, 0) ;
return 0 ;
}

if (argc == 3)
{
percent = getPercent (argv [2]) ;

/**/ if (strcasecmp (argv [1], "red") == 0)
piGlowRing (PIGLOW_RED, percent) ;
else if (strcasecmp (argv [1], "yellow") == 0)
piGlowRing (PIGLOW_YELLOW, percent) ;
else if (strcasecmp (argv [1], "orange") == 0)
piGlowRing (PIGLOW_ORANGE, percent) ;
else if (strcasecmp (argv [1], "green") == 0)
piGlowRing (PIGLOW_GREEN, percent) ;
else if (strcasecmp (argv [1], "blue") == 0)
piGlowRing (PIGLOW_BLUE, percent) ;
else if (strcasecmp (argv [1], "white") == 0)
piGlowRing (PIGLOW_WHITE, percent) ;
else if (strcasecmp (argv [1], "all") == 0)
for (ring = 0 ; ring < 6 ; ++ring)
piGlowRing (ring, percent) ;
else
{
fprintf (stderr, "piglow: invalid colour\n") ;
exit (EXIT_FAILURE) ;
}
return 0 ;
}

if (argc == 4)
{
/**/ if (strcasecmp (argv [1], "leg") == 0)
{
leg = atoi (argv [2]) ;
if ((leg < 0) || (leg > 2))
{
fprintf (stderr, "piglow: leg value out of range\n") ;
exit (EXIT_FAILURE) ;
}
percent = getPercent (argv [3]) ;
piGlowLeg (leg, percent) ;
}
else if (strcasecmp (argv [1], "ring") == 0)
{
ring = atoi (argv [2]) ;
if ((ring < 0) || (ring > 5))
{
fprintf (stderr, "piglow: ring value out of range\n") ;
exit (EXIT_FAILURE) ;
}
percent = getPercent (argv [3]) ;
piGlowRing (ring, percent) ;
}
return 0 ;
}

if (argc == 5)
{
if (strcasecmp (argv [1], "led") != 0)
failUsage () ;

leg = atoi (argv [2]) ;
if ((leg < 0) || (leg > 2))
{
fprintf (stderr, "piglow: leg value out of range\n") ;
exit (EXIT_FAILURE) ;
}
ring = atoi (argv [3]) ;
if ((ring < 0) || (ring > 5))
{
fprintf (stderr, "piglow: ring value out of range\n") ;
exit (EXIT_FAILURE) ;
}
percent = getPercent (argv [4]) ;
piGlow1 (leg, ring, percent) ;
return 0 ;
}

failUsage () ;
return 0 ;
}



+ 0
- 79
examples/scrollPhat/Makefile View File

@@ -1,79 +0,0 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://github.com/wiringPi/wiringPi
#
# Copyright (c) 2012-2015 Gordon Henderson
#################################################################################
# This file is part of wiringPi:
# A "wiring" 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 <http://www.gnu.org/licenses/>.
#################################################################################

ifneq ($V,1)
Q ?= @
endif

#DEBUG = -g -O0
DEBUG = -O3
CC ?= gcc
INCLUDE = -I/usr/local/include
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe $(EXTRA_CFLAGS)

LDFLAGS = -L/usr/local/lib
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm

# Should not alter anything below this line
###############################################################################

SRC = scphat.c test.c

OBJ = $(SRC:.c=.o)

BINS = $(SRC:.c=)

all: $(BINS)

test: test.o
$Q echo [link]
$Q $(CC) -o $@ test.o $(LDFLAGS) $(LDLIBS)

scphat: scphat.o
$Q echo [link]
$Q $(CC) -o $@ scphat.o $(LDFLAGS) $(LDLIBS)


.c.o:
$Q echo [CC] $<
$Q $(CC) -c $(CFLAGS) $< -o $@

clean:
$Q echo "[Clean]"
$Q rm -f $(OBJ) *~ core tags $(BINS)

tags: $(SRC)
$Q echo [ctags]
$Q ctags $(SRC)

install: scphat
$Q echo Installing scphat into /usr/local/bin
$Q cp -a scphat /usr/local/bin/scphat
$Q chmod 755 /usr/local/bin/scphat
$Q echo Done. Remember to load the I2C drivers if needed.

depend:
makedepend -Y $(SRC)

# DO NOT DELETE

+ 0
- 230
examples/scrollPhat/scphat.c View File

@@ -1,230 +0,0 @@
/*
* scphat.c:
* Little program to allow use of the Pimoroni Scroll pHAT
* from the command-line.
*
* Copyright (c) 2015-2016 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <wiringPi.h>
#include <scrollPhat.h>

static char *progName ;


/*
* checkArgs:
* Count the arguments for each little function
*********************************************************************************
*/

static void checkArgs (char *command, int num, int arg, int argc)
{
if ((arg + num) < argc)
return ;

fprintf (stderr, "%s: Not enough data for %s command.\n", progName, command) ;
exit (EXIT_FAILURE) ;
}


/*
* doClear:
* Clear the display
*********************************************************************************
*/

static int doClear (void)
{
scrollPhatClear () ;
return 1 ;
}


/*
* doBright
*********************************************************************************
*/

static int doBright (int arg, int argc, char *argv [])
{
checkArgs ("bright", 1, arg, argc) ;
scrollPhatIntensity (atoi (argv [arg+1])) ;
return 2 ;
}



/*
* doPlot
*********************************************************************************
*/

static int doPlot (int arg, int argc, char *argv [])
{
checkArgs ("plot", 2, arg, argc) ;
scrollPhatPoint (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ;
scrollPhatUpdate () ;
return 3 ;
}


/*
* doLine
*********************************************************************************
*/

static int doLine (int arg, int argc, char *argv [])
{
checkArgs ("line", 4, arg, argc) ;
scrollPhatLine (atoi (argv [arg+1]), atoi (argv [arg+2]),
atoi (argv [arg+3]), atoi (argv [arg+4]), 1) ;
scrollPhatUpdate () ;
return 5 ;
}


/*
* doLineTo
*********************************************************************************
*/

static int doLineTo (int arg, int argc, char *argv [])
{
checkArgs ("lineto", 2, arg, argc) ;
scrollPhatLineTo (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ;
scrollPhatUpdate () ;
return 3 ;
}


/*
* doWait
*********************************************************************************
*/

static int doWait (int arg, int argc, char *argv [])
{
checkArgs ("wait", 1, arg, argc) ;
delay (atoi (argv [arg+1]) * 100) ;
scrollPhatUpdate () ;
return 2 ;
}


/*
* doSpeed
*********************************************************************************
*/

static int doSpeed (int arg, int argc, char *argv [])
{
checkArgs ("speed", 1, arg, argc) ;
scrollPhatPrintSpeed (atoi (argv [arg+1])) ;
return 2 ;
}


/*
* doScroll
*********************************************************************************
*/

static int doScroll (int arg, int argc, char *argv [])
{
checkArgs ("scroll", 1, arg, argc) ;
scrollPhatPuts (argv [arg+1]) ;
return 2 ;
}


static void failUsage (void)
{
fprintf (stderr, "Usage: %s command [paremters] ...\n", progName) ;
fprintf (stderr, " commands:\n") ;
fprintf (stderr, " clear/cls - Clear the display\n") ;
fprintf (stderr, " bright N - Set display brightness; 1-100\n") ;
fprintf (stderr, " plot X Y - Set a single pixel at location X Y; 0-10, 0-4\n") ;
fprintf (stderr, " line X1 Y1 X2 Y2 - Draw a line from the 2 points\n") ;
fprintf (stderr, " lineto X2 Y2 - Draw a line from the last point to the new one\n") ;
fprintf (stderr, " wait/delay N - Wait for N 10ths seconds\n") ;
fprintf (stderr, " speed N - Set scrolling speed (cps)\n") ;
fprintf (stderr, " scroll S - Scroll the given string\n") ;
fprintf (stderr, "\n") ;
fprintf (stderr, " Example: %s plot 0 0 wait 50 scroll \" Hello \"\n", progName) ;
exit (EXIT_FAILURE) ;
}


/*
* the works
*********************************************************************************
*/

int main (int argc, char *argv [])
{
int arg = 1 ;
char *command ;

progName = argv [0] ;

wiringPiSetupSys () ;

if (scrollPhatSetup () != 0)
{
fprintf (stderr, "%s: Unable to initialise the scrollPhat: %s\n", progName, strerror (errno)) ;
exit (EXIT_FAILURE) ;
}

progName = argv [0] ;

if (argc < 2)
{
fprintf (stderr, "%s: Nothing to do...\n", argv [0]) ;
failUsage () ;
}

while (arg != argc)
{
command = argv [arg] ;
/**/ if (strcasecmp (command, "clear") == 0) arg += doClear () ;
else if (strcasecmp (command, "cls") == 0) arg += doClear () ;
else if (strcasecmp (command, "bright") == 0) arg += doBright (arg, argc, argv) ;
else if (strcasecmp (command, "plot") == 0) arg += doPlot (arg, argc, argv) ;
else if (strcasecmp (command, "line") == 0) arg += doLine (arg, argc, argv) ;
else if (strcasecmp (command, "lineto") == 0) arg += doLineTo (arg, argc, argv) ;
else if (strcasecmp (command, "wait") == 0) arg += doWait (arg, argc, argv) ;
else if (strcasecmp (command, "delay") == 0) arg += doWait (arg, argc, argv) ;
else if (strcasecmp (command, "speed") == 0) arg += doSpeed (arg, argc, argv) ;
else if (strcasecmp (command, "scroll") == 0) arg += doScroll (arg, argc, argv) ;
else
{
fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [arg]) ;
failUsage () ;
}
}

return 0 ;
}

+ 0
- 115
examples/scrollPhat/test.c View File

@@ -1,115 +0,0 @@
/*
* test.c:
* Little test program for the Pimoroni Scroll pHAT.
*
* Copyright (c) 2015-2016 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <scrollPhat.h>


/*
* prompt:
* Simple prompt & wait
*********************************************************************************
*/

static void prompt (const char *p)
{
printf (" %s. Press ENTER: ", p) ;
(void)getchar () ;
}


/*
* the works
*********************************************************************************
*/

int main (void)
{
int x, y ;

printf ("\n") ;
printf ("Scroll Phat Test program\n") ;
printf ("========================\n") ;

if (scrollPhatSetup () != 0)
{
printf ("Unable to initialise the scrollPhat: %s\n", strerror (errno)) ;
exit (1) ;
}

printf ("-> Scroll Phat initialised OK\n") ;
printf ("... Basic display tests.\n\n") ;

prompt ("Display ought to be blank") ;

// Light all pixels using one point at a time

for (y = 0 ; y < 5 ; ++y)
for (x = 0 ; x < 12 ; ++x)
scrollPhatPoint (x, y, 1) ;
scrollPhatUpdate () ;

prompt ("Display ought to be all lit-up") ;

// Big rectangle
scrollPhatClear () ;
scrollPhatRectangle (0,0, 10, 4, 1, 0) ;
scrollPhatUpdate () ;

prompt ("There should now be a rectangle round the outside") ;

scrollPhatLine (0,0, 10,4, 1) ;
scrollPhatLine (0,4, 10,0, 1) ;
scrollPhatUpdate () ;

prompt ("Diagonal lines") ;

scrollPhatIntensity (1) ;

prompt ("Minimum brightness") ;
scrollPhatIntensity (100) ;

prompt ("Maximum brightness") ;
scrollPhatIntensity (10) ;

prompt ("Default brightness") ;
scrollPhatClear () ;
printf (" Message Test...Press Ctrl-C to exit: ") ;
fflush (stdout) ;

scrollPhatPrintSpeed (75) ;
for (;;)
scrollPhatPuts (" Welcome to the scroll phat from Pimoroni ") ;
return 0 ;
}

+ 8
- 8
wiringPi/wiringPi.c View File

@@ -1259,13 +1259,13 @@ struct wiringPiNodeStruct *wiringPiFindNode (int pin)
* Create a new GPIO node into the wiringPi handling system
*********************************************************************************
*/
static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return; }
static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return; }
static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW; }
static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return; }
static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return; }
static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0; }
static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return; }
static void pinModeDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int pin, UNUSED int mode) { return; }
static void pullUpDnControlDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int pin, UNUSED int pud) { return; }
static int digitalReadDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int UNUSED pin) { return LOW; }
static void digitalWriteDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int pin, UNUSED int value) { return; }
static void pwmWriteDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int pin, UNUSED int value) { return; }
static int analogReadDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int pin) { return 0; }
static void analogWriteDummy (UNUSED struct wiringPiNodeStruct *node, UNUSED int pin, UNUSED int value) { return; }

struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
{
@@ -1841,7 +1841,7 @@ int waitForInterrupt (int pin, int mS)
* fires.
*********************************************************************************
*/
static void *interruptHandler (UNU void *arg)
static void *interruptHandler (UNUSED void *arg)
{
int myPin;



+ 1
- 1
wiringPi/wiringPi.h View File

@@ -152,7 +152,7 @@ extern const char *piMemorySize [ 8];
// Use at your own risk.

// Threads
#define PI_THREAD(X) void *X (UNU void *dummy)
#define PI_THREAD(X) void *X (UNUSED void *dummy)

// Failure modes
#define WPI_FATAL true


+ 38
- 38
wiringPiD/daemonise.c View File

@@ -17,66 +17,66 @@

void daemonise (const char *pidFile)
{
pid_t pid ;
int i ;
FILE *fd ;
pid_t pid;
int i;
FILE *fd;

syslog (LOG_DAEMON | LOG_INFO, "Becoming daemon") ;
syslog (LOG_DAEMON | LOG_INFO, "Becoming daemon");

// Fork from the parent

if ((pid = fork ()) < 0)
// Fork from the parent
if ((pid = fork()) < 0)
{
syslog (LOG_DAEMON | LOG_ALERT, "Fork no. 1 failed: %m") ;
exit (EXIT_FAILURE) ;
syslog (LOG_DAEMON | LOG_ALERT, "Fork no. 1 failed: %m");
exit (EXIT_FAILURE);
}

if (pid > 0) // Parent - terminate
exit (EXIT_SUCCESS) ;
// Now running on the child - become session leader
if (pid > 0) // Parent - terminate
{
exit (EXIT_SUCCESS);
}

// Now running on the child - become session leader
if (setsid() < 0)
{
syslog (LOG_DAEMON | LOG_ALERT, "setsid failed: %m") ;
exit (EXIT_FAILURE) ;
syslog (LOG_DAEMON | LOG_ALERT, "setsid failed: %m");
exit (EXIT_FAILURE);
}

// Ignore a few signals

signal (SIGCHLD, SIG_IGN) ;
signal (SIGHUP, SIG_IGN) ;

// Fork again
// Ignore a few signals
signal (SIGCHLD, SIG_IGN);
signal (SIGHUP, SIG_IGN);

if ((pid = fork ()) < 0)
// Fork again
if ((pid = fork()) < 0)
{
syslog (LOG_DAEMON | LOG_ALERT, "Fork no. 2 failed: %m") ;
exit (EXIT_FAILURE) ;
syslog (LOG_DAEMON | LOG_ALERT, "Fork no. 2 failed: %m");
exit (EXIT_FAILURE);
}

if (pid > 0) // parent - terminate
exit (EXIT_SUCCESS) ;

// Tidying up - reset umask, change to / and close all files

umask (0) ;
chdir ("/") ;
if (pid > 0) // parent - terminate
{
exit (EXIT_SUCCESS);
}

for (i = 0 ; i < sysconf (_SC_OPEN_MAX) ; ++i)
close (i) ;
// Tidying up - reset umask, change to / and close all files
umask (0);
chdir ("/");

// Write PID into /var/run
for (i = 0; i < sysconf (_SC_OPEN_MAX); ++i)
{
close (i);
}

// Write PID into /var/run
if (pidFile != NULL)
{
if ((fd = fopen (pidFile, "w")) == NULL)
{
syslog (LOG_DAEMON | LOG_ALERT, "Unable to write PID file: %m") ;
exit (EXIT_FAILURE) ;
syslog (LOG_DAEMON | LOG_ALERT, "Unable to write PID file: %m");
exit (EXIT_FAILURE);
}

fprintf (fd, "%d\n", getpid ()) ;
fclose (fd) ;
fprintf (fd, "%d\n", getpid());
fclose (fd);
}
}

+ 207
- 208
wiringPiD/wiringpid.c View File

@@ -1,14 +1,14 @@
/*
* wiringPiD.c:
* Copyright (c) 2012-2017 Gordon Henderson
* Copyright(c) 2012-2017 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/WiringPi/
* https://github.com/WiringPi/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.
* (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
@@ -40,343 +40,342 @@
#include "daemonise.h"


#define PIDFILE "/var/run/wiringPiD.pid"

#define PIDFILE "/var/run/wiringPiD.pid"

// Globals

static const char *usage = "[-h] [-d] [-g | -1 | -z] [[-x extension:pin:params] ...] password" ;
static int doDaemon = FALSE ;
static const char *usage = "[-h] [-d] [-w | -p | -z] [-r <port>] [[-x extension:pin:params] ...] password";
static int doDaemon = FALSE;

//

static void logMsg (const char *message, ...)
static void logMsg(const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_list argp;
char buffer [1024];

va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
va_start(argp, message);
vsnprintf(buffer, 1023, message, argp);
va_end(argp);

if (doDaemon)
syslog (LOG_DAEMON | LOG_INFO, "%s", buffer) ;
syslog(LOG_DAEMON | LOG_INFO, "%s", buffer);
else
printf ("%s\n", buffer) ;
printf("%s\n", buffer);
}


/*
* sigHandler:
* setupSigHandler:
* Somehing has happened that would normally terminate the program so try
* to close down nicely.
* Somehing has happened that would normally terminate the program so try
* to close down nicely.
*********************************************************************************
*/

void sigHandler (int sig)
void sigHandler(int sig)
{
logMsg ("Exiting on signal %d: %s", sig, strsignal (sig)) ;
(void)unlink (PIDFILE) ;
exit (EXIT_FAILURE) ;
logMsg("Exiting on signal %d: %s", sig, strsignal(sig));
(void)unlink(PIDFILE);
exit(EXIT_FAILURE);
}

void setupSigHandler (void)
void setupSigHandler(void)
{
struct sigaction action ;

sigemptyset (&action.sa_mask) ;
action.sa_flags = 0 ;

// Ignore what we can

action.sa_handler = SIG_IGN ;

sigaction (SIGHUP, &action, NULL) ;
sigaction (SIGTTIN, &action, NULL) ;
sigaction (SIGTTOU, &action, NULL) ;

// Trap what we can to exit gracefully

action.sa_handler = sigHandler ;

sigaction (SIGINT, &action, NULL) ;
sigaction (SIGQUIT, &action, NULL) ;
sigaction (SIGILL, &action, NULL) ;
sigaction (SIGABRT, &action, NULL) ;
sigaction (SIGFPE, &action, NULL) ;
sigaction (SIGSEGV, &action, NULL) ;
sigaction (SIGPIPE, &action, NULL) ;
sigaction (SIGALRM, &action, NULL) ;
sigaction (SIGTERM, &action, NULL) ;
sigaction (SIGUSR1, &action, NULL) ;
sigaction (SIGUSR2, &action, NULL) ;
sigaction (SIGCHLD, &action, NULL) ;
sigaction (SIGTSTP, &action, NULL) ;
sigaction (SIGBUS, &action, NULL) ;
struct sigaction action;

sigemptyset(&action.sa_mask);
action.sa_flags = 0;

// Ignore what we can
action.sa_handler = SIG_IGN;

sigaction(SIGHUP, &action, NULL);
sigaction(SIGTTIN, &action, NULL);
sigaction(SIGTTOU, &action, NULL);

// Trap what we can to exit gracefully
action.sa_handler = sigHandler;

sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGABRT, &action, NULL);
sigaction(SIGFPE, &action, NULL);
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGALRM, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
sigaction(SIGBUS, &action, NULL);
}


/*
* The works...
* Main program.
*********************************************************************************
*/

int main (int argc, char *argv [])
int main(int argc, char *argv [])
{
int clientFd ;
char *p, *password ;
int i ;
int port = DEFAULT_SERVER_PORT ;
int wpiSetup = 0 ;
int clientFd;
char *p, *password;
int i;
int port = DEFAULT_SERVER_PORT;
int wpiSetup = 0;

if (argc < 2)
{
fprintf (stderr, "Usage: %s %s\n", argv [0], usage) ;
exit (EXIT_FAILURE) ;
fprintf(stderr, "Usage: %s %s\n", argv [0], usage);
exit(EXIT_FAILURE);
}

// Help?

if (strcasecmp (argv [1], "-h") == 0)
// Help
if (strcasecmp(argv [1], "-h") == 0)
{
printf ("Usage: %s %s\n", argv [0], usage) ;
return 0 ;
printf("Usage: %s %s\n", argv [0], usage);
return 0;
}

// Daemonize?
// Must come before the other args as e.g. some extensions
// open files which get closed on daemonise...

if (strcasecmp (argv [1], "-d") == 0)
// Daemonize
// Must come before the other args as e.g. some extensions
// open files which get closed on daemonise...
if (strcasecmp(argv [1], "-d") == 0)
{
if (geteuid () != 0)
if (geteuid() != 0)
{
fprintf (stderr, "%s: Must be root to run as a daemon.\n", argv [0]) ;
exit (EXIT_FAILURE) ;
fprintf(stderr, "%s: Must be root to run as a daemon.\n", argv [0]);
exit(EXIT_FAILURE);
}

doDaemon = TRUE ;
daemonise (PIDFILE) ;
doDaemon = TRUE;
daemonise(PIDFILE);

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
for (i = 2; i < argc; ++i)
{
argv [i - 1] = argv [i];
}
--argc;
}

// Scan all other arguments

while (*argv [1] == '-')
// Scan all other arguments
while(*argv [1] == '-')
{
// Look for wiringPi setup arguments:
// Same as the gpio command and rtb.

// Look for wiringPi setup arguments:
// Same as the gpio command and rtb.

// -g - bcm_gpio

if (strcasecmp (argv [1], "-g") == 0)
// -w - WiringPi mode
if (strcasecmp(argv [1], "-w") == 0)
{
if (wpiSetup == 0)
{
logMsg ("BCM_GPIO mode selected") ;
wiringPiSetupGpio () ;
logMsg("WiringPi GPIO mode selected");
wiringPiSetup();
}

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
++wpiSetup ;
continue ;
for (i = 2; i < argc; ++i)
{
argv [i - 1] = argv [i];
}
--argc;
++wpiSetup;
continue;
}

// -1 - physical pins

if (strcasecmp (argv [1], "-1") == 0)
// -p - physical pins
if (strcasecmp(argv [1], "-p") == 0)
{
if (wpiSetup == 0)
{
logMsg ("GPIO-PHYS mode selected") ;
wiringPiSetupPhys () ;
logMsg("GPIO-PHYS mode selected");
wiringPiSetupPhys();
}

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
++wpiSetup ;
continue ;
for (i = 2; i < argc; ++i)
{
argv [i - 1] = argv [i];
}
--argc;
++wpiSetup;
continue;
}

// -z - no wiringPi - blocks remotes accessing local pins

if (strcasecmp (argv [1], "-z") == 0)
// -z - no wiringPi - blocks remotes accessing local pins
if (strcasecmp(argv [1], "-z") == 0)
{
if (wpiSetup == 0)
logMsg ("No GPIO mode selected") ;

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
noLocalPins = TRUE ;
++wpiSetup ;
continue ;
}
logMsg("No-GPIO mode selected");

// -p to select the port
for (i = 2; i < argc; ++i)
{
argv [i - 1] = argv [i];
}
--argc;
noLocalPins = TRUE;
++wpiSetup;
continue;
}

if (strcasecmp (argv [1], "-p") == 0)
// -r to select the port
if (strcasecmp(argv [1], "-r") == 0)
{
if (argc < 3)
{
logMsg ("-p missing extension port") ;
exit (EXIT_FAILURE) ;
logMsg("-r missing extension port");
exit(EXIT_FAILURE);
}

logMsg ("Setting port to: %s", argv [2]) ;
logMsg("Setting port to: %s", argv [2]);

port = atoi (argv [2]) ;
if ((port < 1) || (port > 65535))
port = atoi(argv [2]);
if ((port < 1) ||(port > 65535))
{
logMsg ("Invalid server port: %d", port) ;
exit (EXIT_FAILURE) ;
logMsg("Invalid server port: %d", port);
exit(EXIT_FAILURE);
}

// Shift args down by 2

for (i = 3 ; i < argc ; ++i)
argv [i - 2] = argv [i] ;
argc -= 2 ;
// Shift args down by 2
for (i = 3; i < argc; ++i)
{
argv [i - 2] = argv [i];
}
argc -= 2;

continue ;
continue;
}

// Check for -x argument to load in a new extension
// -x extension:base:args
// Can load many modules to extend the daemon.

if (strcasecmp (argv [1], "-x") == 0)
// Check for -x argument to load in a new extension
// -x extension:base:args
// Can load many modules to extend the daemon.
if (strcasecmp(argv [1], "-x") == 0)
{
if (argc < 3)
{
logMsg ("-x missing extension name:data:etc.") ;
exit (EXIT_FAILURE) ;
logMsg("-x missing extension name:data:etc.");
exit(EXIT_FAILURE);
}

logMsg ("Loading extension: %s", argv [2]) ;
logMsg("Loading extension: %s", argv [2]);

if (!loadWPiExtension (argv [0], argv [2], TRUE))
if (!loadWPiExtension(argv [0], argv [2], TRUE))
{
logMsg ("Extension load failed: %s", strerror (errno)) ;
exit (EXIT_FAILURE) ;
logMsg("Extension load failed: %s", strerror(errno));
exit(EXIT_FAILURE);
}

// Shift args down by 2

for (i = 3 ; i < argc ; ++i)
argv [i - 2] = argv [i] ;
argc -= 2 ;
// Shift args down by 2
for (i = 3; i < argc; ++i)
{
argv [i - 2] = argv [i];
}
argc -= 2;

continue ;
continue;
}

logMsg ("Invalid parameter: %s", argv [1]) ;
exit (EXIT_FAILURE) ;
logMsg("Invalid parameter: %s", argv [1]);
exit(EXIT_FAILURE);
}

// Default to wiringPi mode

// Default to BCM GPIO mode
if (wpiSetup == 0)
{
logMsg ("WiringPi GPIO mode selected") ;
wiringPiSetup () ;
logMsg("BCM_GPIO mode selected");
wiringPiSetupGpio();
}

// Finally, should just be one arg left - the password...

// Finally, should just be one arg left - the password...
if (argc != 2)
{
logMsg ("No password supplied") ;
exit (EXIT_FAILURE) ;
logMsg("No password supplied");
exit(EXIT_FAILURE);
}

if (strlen (argv [1]) < 6)
if (strlen(argv [1]) < 6)
{
logMsg ("Password too short - at least 6 chars, not %d", strlen (argv [1])) ;
exit (EXIT_FAILURE) ;
logMsg("Password too short - at least 6 chars, not %d", strlen(argv [1]));
exit(EXIT_FAILURE);
}

if ((password = malloc (strlen (argv [1]) + 1)) == NULL)
if ((password = malloc(strlen(argv [1]) + 1)) == NULL)
{
logMsg ("Out of memory") ;
exit (EXIT_FAILURE) ;
logMsg("Out of memory");
exit(EXIT_FAILURE);
}
strcpy (password, argv [1]) ;

// Wipe out the password on the command-line in a vague attempt to try to
// hide it from snoopers
strcpy(password, argv [1]);

for (p = argv [1] ; *p ; ++p)
*p = ' ' ;
// Wipe out the password on the command-line in a vague attempt to try to
// hide it from snoopers
for (p = argv [1]; *p; ++p)
{
*p = ' ';
}

setupSigHandler () ;
setupSigHandler();
// Enter our big loop

// Loop forever until a signal terminates the process.
for (;;)
{

if (!doDaemon)
printf ("-=-\nWaiting for a new connection...\n") ;
{
printf("-=-\nWaiting for a new connection...\n");
}

if ((clientFd = setupServer (port)) < 0)
if ((clientFd = setupServer(port)) < 0)
{
logMsg ("Unable to setup server: %s", strerror (errno)) ;
exit (EXIT_FAILURE) ;
logMsg("Unable to setup server: %s", strerror(errno));
exit(EXIT_FAILURE);
}

logMsg ("New connection from: %s.", getClientIP ()) ;
logMsg("New connection from: %s.", getClientIP());

if (!doDaemon)
printf ("Sending Greeting.\n") ;
{
printf("Sending Greeting.\n");
}

if (sendGreeting (clientFd) < 0)
if (sendGreeting(clientFd) < 0)
{
logMsg ("Unable to send greeting message: %s", strerror (errno)) ;
closeServer (clientFd) ;
continue ;
logMsg("Unable to send greeting message: %s", strerror(errno));
closeServer(clientFd);
continue;
}

if (!doDaemon)
printf ("Sending Challenge.\n") ;
{
printf("Sending Challenge.\n");
}

if (sendChallenge (clientFd) < 0)
if (sendChallenge(clientFd) < 0)
{
logMsg ("Unable to send challenge message: %s", strerror (errno)) ;
closeServer (clientFd) ;
continue ;
logMsg("Unable to send challenge message: %s", strerror(errno));
closeServer(clientFd);
continue;
}

if (!doDaemon)
printf ("Waiting for response.\n") ;
{
printf("Waiting for response.\n");
}

if (getResponse (clientFd) < 0)
if (getResponse(clientFd) < 0)
{
logMsg ("Connection closed waiting for response: %s", strerror (errno)) ;
closeServer (clientFd) ;
continue ;
logMsg("Connection closed waiting for response: %s", strerror(errno));
closeServer(clientFd);
continue;
}

if (!passwordMatch (password))
if (!passwordMatch(password))
{
logMsg ("Password failure") ;
closeServer (clientFd) ;
continue ;
logMsg("Password failure");
closeServer(clientFd);
continue;
}

logMsg ("Password OK - Starting") ;
logMsg("Password OK - Starting");

runRemoteCommands (clientFd) ;
closeServer (clientFd) ;
runRemoteCommands(clientFd);
closeServer (clientFd);
}

return 0 ;
return EXIT_SUCCESS;
}

Loading…
Cancel
Save