first commit

This commit is contained in:
liuls2002
2023-08-11 15:53:17 +08:00
commit 2fe3003e20
213 changed files with 37226 additions and 0 deletions

165
examples/COPYING.LESSER Normal file
View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View File

@@ -0,0 +1,221 @@
/*
* 7segments.c:
* Simple test program to see if we can drive a 7-segment LED
* display using the GPIO and little else on the Raspberry Pi
*
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
*/
#undef PHOTO_HACK
#include <wiringPi.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
/*
* Segment mapping
*
* --a--
* | |
* f b
* | |
* --g--
* | |
* e c
* | |
* --d-- p
*/
// GPIO Pin Mapping
static int digits [6] = { 7, 11, 10, 13, 12, 14 } ;
static int segments [7] = { 6, 5, 4, 3, 2, 1, 0 } ;
static const int segmentDigits [] =
{
// a b c d e f g Segments
// 6 5 4 3 2 1 0, // wiringPi pin No.
1, 1, 1, 1, 1, 1, 0, // 0
0, 1, 1, 0, 0, 0, 0, // 1
1, 1, 0, 1, 1, 0, 1, // 2
1, 1, 1, 1, 0, 0, 1, // 3
0, 1, 1, 0, 0, 1, 1, // 4
1, 0, 1, 1, 0, 1, 1, // 5
1, 0, 1, 1, 1, 1, 1, // 6
1, 1, 1, 0, 0, 0, 0, // 7
1, 1, 1, 1, 1, 1, 1, // 8
1, 1, 1, 1, 0, 1, 1, // 9
1, 1, 1, 0, 1, 1, 1, // A
0, 0, 1, 1, 1, 1, 1, // b
1, 0, 0, 1, 1, 1, 0, // C
0, 1, 1, 1, 1, 0, 1, // d
1, 0, 0, 1, 1, 1, 1, // E
1, 0, 0, 0, 1, 1, 1, // F
0, 0, 0, 0, 0, 0, 0, // blank
} ;
// display:
// A global variable which is written to by the main program and
// read from by the thread that updates the display. Only the first
// 6 characters are used.
char display [8] ;
/*
* displayDigits:
* This is our thread that's run concurrently with the main program.
* Essentially sit in a loop, parsing and displaying the data held in
* the "display" global.
*********************************************************************************
*/
PI_THREAD (displayDigits)
{
int digit, segment ;
int index, d, segVal ;
piHiPri (50) ;
for (;;)
{
for (digit = 0 ; digit < 6 ; ++digit)
{
for (segment = 0 ; segment < 7 ; ++segment)
{
d = toupper (display [digit]) ;
/**/ if ((d >= '0') && (d <= '9')) // Digit
index = d - '0' ;
else if ((d >= 'A') && (d <= 'F')) // Hex
index = d - 'A' + 10 ;
else
index = 16 ; // Blank
segVal = segmentDigits [index * 7 + segment] ;
digitalWrite (segments [segment], segVal) ;
}
digitalWrite (digits [digit], 1) ;
delay (2) ;
digitalWrite (digits [digit], 0) ;
}
}
}
/*
* setup:
* Initialise the hardware and start the thread
*********************************************************************************
*/
void setup (void)
{
int i, c ;
wiringPiSetup () ;
// 7 segments
for (i = 0 ; i < 7 ; ++i)
{ digitalWrite (segments [i], 0) ; pinMode (segments [i], OUTPUT) ; }
// 6 digits
for (i = 0 ; i < 6 ; ++i)
{ digitalWrite (digits [i], 0) ; pinMode (digits [i], OUTPUT) ; }
strcpy (display, " ") ;
piThreadCreate (displayDigits) ;
delay (10) ; // Just to make sure it's started
// Quick countdown LED test sort of thing
c = 999999 ;
for (i = 0 ; i < 10 ; ++i)
{
sprintf (display, "%06d", c) ;
delay (400) ;
c -= 111111 ;
}
strcpy (display, " ") ;
delay (400) ;
#ifdef PHOTO_HACK
sprintf (display, "%s", "123456") ;
for (;;)
delay (1000) ;
#endif
}
/*
* teenager:
* No explanation needed. (Nor one given!)
*********************************************************************************
*/
void teenager (void)
{
char *message = " feedbeef babe cafe b00b " ;
int i ;
for (i = 0 ; i < strlen (message) - 4 ; ++i)
{
strncpy (display, &message [i], 6) ;
delay (200) ;
}
delay (1000) ;
for (i = 0 ; i < 3 ; ++i)
{
strcpy (display, " ") ;
delay (150) ;
strcpy (display, " b00b ") ;
delay (250) ;
}
delay (1000) ;
strcpy (display, " ") ;
delay (1000) ;
}
/*
*********************************************************************************
* main:
* Let the fun begin
*********************************************************************************
*/
int main (void)
{
struct tm *t ;
time_t tim ;
setup () ;
teenager () ;
tim = time (NULL) ;
for (;;)
{
while (time (NULL) == tim)
delay (5) ;
tim = time (NULL) ;
t = localtime (&tim) ;
sprintf (display, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
delay (500) ;
}
return 0 ;
}

View File

@@ -0,0 +1,78 @@
#
# Makefile:
# Gertboard - Examples using wiringPi
#
# Copyright (c) 2013 Gordon Henderson
#################################################################################
ifneq ($V,1)
Q ?= @
endif
#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 = gertboard.c \
buttons.c 7segments.c \
voltmeter.c temperature.c vumeter.c \
record.c
OBJ = $(SRC:.c=.o)
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)
7segments: 7segments.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 $@
clean:
$Q echo [Clean]
$Q rm -f $(OBJ) *~ core tags $(BINS)
tags: $(SRC)
$Q echo [ctags]
$Q ctags $(SRC)
depend:
makedepend -Y $(SRC)
# DO NOT DELETE

View File

@@ -0,0 +1,83 @@
/*
* buttons.c:
* Read the Gertboard buttons. Each one will act as an on/off
* tiggle switch for 3 different LEDs
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
// Array to keep track of our LEDs
int leds [] = { 0, 0, 0 } ;
// scanButton:
// See if a button is pushed, if so, then flip that LED and
// wait for the button to be let-go
void scanButton (int button)
{
if (digitalRead (button) == HIGH) // Low is pushed
return ;
leds [button] ^= 1 ; // Invert state
digitalWrite (4 + button, leds [button]) ;
while (digitalRead (button) == LOW) // Wait for release
delay (10) ;
}
int main (void)
{
int i ;
printf ("Raspberry Pi Gertboard Button Test\n") ;
wiringPiSetup () ;
// Setup the outputs:
// Pins 3, 4, 5, 6 and 7 output:
// We're not using 3 or 4, but make sure they're off anyway
// (Using same hardware config as blink12.c)
for (i = 3 ; i < 8 ; ++i)
{
pinMode (i, OUTPUT) ;
digitalWrite (i, 0) ;
}
// Setup the inputs
for (i = 0 ; i < 3 ; ++i)
{
pinMode (i, INPUT) ;
pullUpDnControl (i, PUD_UP) ;
leds [i] = 0 ;
}
for (;;)
{
for (i = 0 ; i < 3 ; ++i)
scanButton (i) ;
delay (1) ;
}
}

View File

@@ -0,0 +1,96 @@
/*
* 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
* oscilloscipe.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <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 ;
}

View File

@@ -0,0 +1,60 @@
/*
* 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 ;
}

View File

@@ -0,0 +1,78 @@
/*
* 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. <projects@drogon.net>
***********************************************************************
* 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 <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 ;
}

View File

@@ -0,0 +1,73 @@
/*
* voltmeter.c:
* Demonstrate use of the Gertboard A to D converter to make
* a simple voltmeter.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <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 ;
}

View File

@@ -0,0 +1,152 @@
/*
* 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 ;
}

197
examples/Makefile Normal file
View File

@@ -0,0 +1,197 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://projects.drogon.net/wiring-pi
#
# Copyright (c) 2012-2015 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 <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
LDFLAGS = -L/usr/local/lib
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt
# Should not alter anything below this line
###############################################################################
SRC = blink.c blink8.c blink12.c \
blink12drcs.c \
pwm.c \
speed.c wfi.c isr.c isr-osc.c \
lcd.c lcd-adafruit.c clock.c \
nes.c \
softPwm.c softTone.c \
delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \
lowPower.c \
max31855.c \
rht03.c \
w25q64_test.c oled_demo.c spiSpeed.c \
watchdog.c spidev_test.c spidev_test_linux3_4.c
OBJ = $(SRC:.c=.o)
BINS = $(SRC:.c=)
all:
$Q cat README.TXT
$Q echo " $(BINS)" | fmt
$Q echo ""
really-all: $(BINS)
blink: blink.o
$Q echo [link]
$Q $(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
blink8: blink8.o
$Q echo [link]
$Q $(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS)
blink12drcs: blink12drcs.o
$Q echo [link]
$Q $(CC) -o $@ blink12drcs.o $(LDFLAGS) $(LDLIBS)
blink12: blink12.o
$Q echo [link]
$Q $(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS)
speed: speed.o
$Q echo [link]
$Q $(CC) -o $@ speed.o $(LDFLAGS) $(LDLIBS)
lcd: lcd.o
$Q echo [link]
$Q $(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS)
lcd-adafruit: lcd-adafruit.o
$Q echo [link]
$Q $(CC) -o $@ lcd-adafruit.o $(LDFLAGS) $(LDLIBS)
clock: clock.o
$Q echo [link]
$Q $(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS)
wfi: wfi.o
$Q echo [link]
$Q $(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS)
isr: isr.o
$Q echo [link]
$Q $(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS)
isr-osc: isr-osc.o
$Q echo [link]
$Q $(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS)
nes: nes.o
$Q echo [link]
$Q $(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS)
rht03: rht03.o
$Q echo [link]
$Q $(CC) -o $@ rht03.o $(LDFLAGS) $(LDLIBS)
pwm: pwm.o
$Q echo [link]
$Q $(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS)
softPwm: softPwm.o
$Q echo [link]
$Q $(CC) -o $@ softPwm.o $(LDFLAGS) $(LDLIBS)
softTone: softTone.o
$Q echo [link]
$Q $(CC) -o $@ softTone.o $(LDFLAGS) $(LDLIBS)
delayTest: delayTest.o
$Q echo [link]
$Q $(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS)
serialRead: serialRead.o
$Q echo [link]
$Q $(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS)
serialTest: serialTest.o
$Q echo [link]
$Q $(CC) -o $@ serialTest.o $(LDFLAGS) $(LDLIBS)
okLed: okLed.o
$Q echo [link]
$Q $(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS)
tone: tone.o
$Q echo [link]
$Q $(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS)
ds1302: ds1302.o
$Q echo [link]
$Q $(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS)
max31855: max31855.o
$Q echo [link]
$Q $(CC) -o $@ max31855.o $(LDFLAGS) $(LDLIBS)
w25q64_test: w25q64_test.o
$Q echo [link]
$Q $(CC) -o $@ w25q64_test.o $(LDFLAGS) $(LDLIBS)
watchdog: watchdog.o
$Q echo [link]
$Q $(CC) -o $@ watchdog.o $(LDFLAGS) $(LDLIBS)
spidev_test: spidev_test.o
$Q echo [link]
$Q $(CC) -o $@ spidev_test.o $(LDFLAGS) $(LDLIBS)
spidev_test_linux3_4: spidev_test_linux3_4.o
$Q echo [link]
$Q $(CC) -o $@ spidev_test_linux3_4.o $(LDFLAGS) $(LDLIBS)
oled_demo: oled_demo.o
$Q echo [link]
$Q $(CC) -o $@ oled_demo.o $(LDFLAGS) $(LDLIBS)
spiSpeed: spiSpeed.o
$Q echo [link]
$Q $(CC) -o $@ spiSpeed.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)
depend:
makedepend -Y $(SRC)
# DO NOT DELETE

88
examples/PiFace/Makefile Normal file
View File

@@ -0,0 +1,88 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://projects.drogon.net/wiring-pi
#
# Copyright (c) 2012 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
LDFLAGS = -L/usr/local/lib
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm
# Should not alter anything below this line
###############################################################################
SRC = blink.c buttons.c reaction.c ladder.c metro.c motor.c
OBJ = $(SRC:.c=.o)
BINS = $(SRC:.c=)
all: $(BINS)
blink: blink.o
$Q echo [link]
$Q $(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
buttons: buttons.o
$Q echo [link]
$Q $(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS)
reaction: reaction.o
$Q echo [link]
$Q $(CC) -o $@ reaction.o $(LDFLAGS) $(LDLIBS)
ladder: ladder.o
$Q echo [link]
$Q $(CC) -o $@ ladder.o $(LDFLAGS) $(LDLIBS)
metro: metro.o
$Q echo [link]
$Q $(CC) -o $@ metro.o $(LDFLAGS) $(LDLIBS)
motor: motor.o
$Q echo [link]
$Q $(CC) -o $@ motor.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)
depend:
makedepend -Y $(SRC)
# DO NOT DELETE

59
examples/PiFace/blink.c Normal file
View File

@@ -0,0 +1,59 @@
/*
* blink.c:
* Simple "blink" test for the PiFace interface board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <piFace.h>
// Use 200 as the pin-base for the PiFace board, and pick a pin
// for the LED that's not connected to a relay
#define PIFACE 200
#define LED (PIFACE+2)
int main (int argc, char *argv [])
{
printf ("Raspberry Pi PiFace Blink\n") ;
printf ("=========================\n") ;
// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root
wiringPiSetupSys () ;
// Setup the PiFace board
piFaceSetup (PIFACE) ;
for (;;)
{
digitalWrite (LED, HIGH) ; // On
delay (500) ; // mS
digitalWrite (LED, LOW) ; // Off
delay (500) ;
}
return 0 ;
}

103
examples/PiFace/buttons.c Normal file
View File

@@ -0,0 +1,103 @@
/*
* buttons.c:
* Simple test for the PiFace interface board.
*
* Read the buttons and output the same to the LEDs
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <piFace.h>
int outputs [4] = { 0,0,0,0 } ;
// Use 200 as the pin-base for the PiFace board
#define PIFACE_BASE 200
/*
* scanButton:
* Read the guiven button - if it's pressed, then flip the state
* of the correspoinding output pin
*********************************************************************************
*/
void scanButton (int button)
{
if (digitalRead (PIFACE_BASE + button) == LOW)
{
outputs [button] ^= 1 ;
digitalWrite (PIFACE_BASE + button, outputs [button]) ;
printf ("Button %d pushed - output now: %s\n",
button, (outputs [button] == 0) ? "Off" : "On") ;
}
while (digitalRead (PIFACE_BASE + button) == LOW)
delay (1) ;
}
/*
* start here
*********************************************************************************
*/
int main (void)
{
int pin, button ;
printf ("Raspberry Pi wiringPi + PiFace test program\n") ;
printf ("===========================================\n") ;
printf ("\n") ;
printf (
"This program reads the buttons and uses them to toggle the first 4\n"
"outputs. Push a button once to turn an output on, and push it again to\n"
"turn it off again.\n\n") ;
// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root
wiringPiSetupSys () ;
piFaceSetup (PIFACE_BASE) ;
// Enable internal pull-ups & start with all off
for (pin = 0 ; pin < 8 ; ++pin)
{
pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
digitalWrite (PIFACE_BASE + pin, 0) ;
}
// Loop, scanning the buttons
for (;;)
{
for (button = 0 ; button < 4 ; ++button)
scanButton (button) ;
delay (5) ;
}
return 0 ;
}

337
examples/PiFace/ladder.c Normal file
View File

@@ -0,0 +1,337 @@
/*
* ladder.c:
*
* Gordon Henderson, June 2012
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <wiringPi.h>
#include <piFace.h>
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
#undef DEBUG
#define NUM_LEDS 8
// Map the LEDs to the hardware pins
// using PiFace pin numbers here
#define PIFACE 200
const int ledMap [NUM_LEDS] =
{
// 0, 1, 2, 3, 4, 5, 6, 7, 8
200, 201, 202, 203, 204, 205, 206, 207
} ;
// Some constants for our circuit simulation
const double vBatt = 9.0 ; // Volts (ie. a PP3)
const double capacitor = 0.001 ; // 1000uF
const double rCharge = 2200.0 ; // ohms
const double rDischarge = 68000.0 ; // ohms
const double timeInc = 0.01 ; // Seconds
double vCharge, vCap, vCapLast ;
/*
* setup:
* Program the GPIO correctly and initialise the lamps
***********************************************************************
*/
void setup (void)
{
int i ;
wiringPiSetupSys () ;
if (piFaceSetup (200) == -1)
exit (1) ;
// Enable internal pull-ups
for (i = 0 ; i < 8 ; ++i)
pullUpDnControl (PIFACE + i, PUD_UP) ;
// Calculate the actual charging voltage - standard calculation of
// vCharge = r2 / (r1 + r2) * vBatt
//
//
// -----+--- vBatt
// |
// R1
// |
// +---+---- vCharge
// | |
// R2 C
// | |
// -----+---+-----
vCharge = rDischarge / (rCharge + rDischarge) * vBatt ;
// Start with no charge
vCap = vCapLast = 0.0 ;
}
/*
* introLeds
* Put a little pattern on the LEDs to start with
*********************************************************************************
*/
void introLeds (void)
{
int i, j ;
printf ("Pi Ladder\n") ;
printf ("=========\n\n") ;
printf (" vBatt: %6.2f volts\n", vBatt) ;
printf (" rCharge: %6.0f ohms\n", rCharge) ;
printf (" rDischarge: %6.0f ohms\n", rDischarge) ;
printf (" vCharge: %6.2f volts\n", vCharge) ;
printf (" capacitor: %6.0f uF\n", capacitor * 1000.0) ;
// Flash 3 times:
for (j = 0 ; j < 3 ; ++j)
{
for (i = 0 ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 1) ;
delay (500) ;
for (i = 0 ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 0) ;
delay (100) ;
}
// All On
for (i = 0 ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 1) ;
delay (500) ;
// Countdown...
for (i = NUM_LEDS - 1 ; i >= 0 ; --i)
{
digitalWrite (ledMap [i], 0) ;
delay (100) ;
}
delay (500) ;
}
/*
* winningLeds
* Put a little pattern on the LEDs to start with
*********************************************************************************
*/
void winningLeds (void)
{
int i, j ;
// Flash 3 times:
for (j = 0 ; j < 3 ; ++j)
{
for (i = 0 ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 1) ;
delay (500) ;
for (i = 0 ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 0) ;
delay (100) ;
}
// All On
for (i = 0 ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 1) ;
delay (500) ;
// Countup...
for (i = 0 ; i < NUM_LEDS ; ++i)
{
digitalWrite (ledMap [i], 0) ;
delay (100) ;
}
delay (500) ;
}
/*
* chargeCapacitor: dischargeCapacitor:
* Add or remove charge to the capacitor.
* Standard capacitor formulae.
*********************************************************************************
*/
void chargeCapacitor (void)
{
vCap = (vCapLast - vCharge) *
exp (- timeInc / (rCharge * capacitor)) + vCharge ;
#ifdef DEBUG
printf ("+vCap: %7.4f\n", vCap) ;
#endif
vCapLast = vCap ;
}
void dischargeCapacitor (void)
{
vCap = vCapLast *
exp (- timeInc / (rDischarge * capacitor)) ;
#ifdef DEBUG
printf ("-vCap: %7.4f\n", vCap) ;
#endif
vCapLast = vCap ;
}
/*
* ledBargraph:
* Output the supplied number as a bargraph on the LEDs
*********************************************************************************
*/
void ledBargraph (double value, int topLedOn)
{
int topLed = (int)floor (value / vCharge * (double)NUM_LEDS) + 1 ;
int i ;
if (topLed > NUM_LEDS)
topLed = NUM_LEDS ;
if (!topLedOn)
--topLed ;
for (i = 0 ; i < topLed ; ++i)
digitalWrite (ledMap [i], 1) ;
for (i = topLed ; i < NUM_LEDS ; ++i)
digitalWrite (ledMap [i], 0) ;
}
/*
* ledOnAction:
* Make sure the leading LED is on and check the button
*********************************************************************************
*/
void ledOnAction (void)
{
if (digitalRead (PIFACE) == LOW)
{
chargeCapacitor () ;
ledBargraph (vCap, TRUE) ;
}
}
/*
* ledOffAction:
* Make sure the leading LED is off and check the button
*********************************************************************************
*/
void ledOffAction (void)
{
dischargeCapacitor () ;
// Are we still pushing the button?
if (digitalRead (PIFACE) == LOW)
{
vCap = vCapLast = 0.0 ;
ledBargraph (vCap, FALSE) ;
// Wait until we release the button
while (digitalRead (PIFACE) == LOW)
delay (10) ;
}
}
/*
***********************************************************************
* The main program
***********************************************************************
*/
int main (void)
{
unsigned int then, ledOnTime, ledOffTime ;
unsigned int ourDelay = (int)(1000.0 * timeInc) ;
setup () ;
introLeds () ;
// Setup the LED times - TODO reduce the ON time as the game progresses
ledOnTime = 1000 ;
ledOffTime = 1000 ;
// This is our Gate/Squarewave loop
for (;;)
{
// LED ON:
(void)ledBargraph (vCap, TRUE) ;
then = millis () + ledOnTime ;
while (millis () < then)
{
ledOnAction () ;
delay (ourDelay) ;
}
// Have we won yet?
// We need vCap to be in the top NUM_LEDS of the vCharge
if (vCap > ((double)(NUM_LEDS - 1) / (double)NUM_LEDS * vCharge)) // Woo hoo!
{
winningLeds () ;
while (digitalRead (PIFACE) == HIGH)
delay (10) ;
while (digitalRead (PIFACE) == LOW)
delay (10) ;
vCap = vCapLast = 0.0 ;
}
// LED OFF:
(void)ledBargraph (vCap, FALSE) ;
then = millis () + ledOffTime ;
while (millis () < then)
{
ledOffAction () ;
delay (ourDelay) ;
}
}
return 0 ;
}

111
examples/PiFace/metro.c Normal file
View File

@@ -0,0 +1,111 @@
/*
* metronome.c:
* Simple test for the PiFace interface board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiringPi.h>
#include <piFace.h>
#define PIFACE 200
/*
* middleA:
* Play middle A (on the relays - yea!)
*********************************************************************************
*/
static void middleA (void)
{
unsigned int next ;
for (;;)
{
next = micros () + 1136 ;
digitalWrite (PIFACE + 0, 0) ;
digitalWrite (PIFACE + 1, 0) ;
while (micros () < next)
delayMicroseconds (1) ;
next = micros () + 1137 ;
digitalWrite (PIFACE + 0, 1) ;
digitalWrite (PIFACE + 1, 1) ;
while (micros () < next)
delayMicroseconds (1) ;
}
}
int main (int argc, char *argv [])
{
int bpm, msPerBeat, state = 0 ;
unsigned int end ;
printf ("Raspberry Pi PiFace Metronome\n") ;
printf ("=============================\n") ;
piHiPri (50) ;
wiringPiSetupSys () ; // Needed for timing functions
piFaceSetup (PIFACE) ;
if (argc != 2)
{
printf ("Usage: %s <beates per minute>\n", argv [0]) ;
exit (1) ;
}
if (strcmp (argv [1], "a") == 0)
middleA () ;
bpm = atoi (argv [1]) ;
if ((bpm < 40) || (bpm > 208))
{
printf ("%s range is 40 through 208 beats per minute\n", argv [0]) ;
exit (1) ;
}
msPerBeat = 60000 / bpm ;
// Main loop:
// Put some random LED pairs up for a few seconds, then blank ...
for (;;)
{
end = millis () + msPerBeat ;
digitalWrite (PIFACE + 0, state) ;
digitalWrite (PIFACE + 1, state) ;
while (millis () < end)
delayMicroseconds (500) ;
state ^= 1 ;
}
return 0 ;
}

120
examples/PiFace/motor.c Normal file
View File

@@ -0,0 +1,120 @@
/*
* motor.c:
* Use the PiFace board to demonstrate an H bridge
* circuit via the 2 relays.
* Then add on an external transsitor to help with PWM.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <wiringPi.h>
#include <piFace.h>
#include <softPwm.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int outputs [2] = { 0,0 } ;
#define PIFACE_BASE 200
#define PWM_OUT_PIN 204
#define PWM_UP 202
#define PWM_DOWN 203
void scanButton (int button)
{
if (digitalRead (PIFACE_BASE + button) == LOW)
{
outputs [button] ^= 1 ;
digitalWrite (PIFACE_BASE + button, outputs [button]) ;
printf ("Button %d pushed - output now: %s\n",
button, (outputs [button] == 0) ? "Off" : "On") ;
}
while (digitalRead (PIFACE_BASE + button) == LOW)
delay (1) ;
}
int main (void)
{
int pin, button ;
int pwmValue = 0 ;
printf ("Raspberry Pi PiFace - Motor control\n") ;
printf ("==================================\n") ;
printf ("\n") ;
printf (
"This program is designed to be used with a motor connected to the relays\n"
"in an H-Bridge type configuration with optional speeed control via PWM.\n"
"\n"
"Use the leftmost buttons to turn each relay on and off, and the rigthmost\n"
"buttons to increase ot decrease the PWM output on the control pin (pin\n"
"4)\n\n") ;
wiringPiSetup () ;
piFaceSetup (PIFACE_BASE) ;
softPwmCreate (PWM_OUT_PIN, 100, 100) ;
// Enable internal pull-ups & start with all off
for (pin = 0 ; pin < 8 ; ++pin)
{
pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
digitalWrite (PIFACE_BASE + pin, 0) ;
}
for (;;)
{
for (button = 0 ; button < 2 ; ++button)
scanButton (button) ;
if (digitalRead (PWM_UP) == LOW)
{
pwmValue += 10 ;
if (pwmValue > 100)
pwmValue = 100 ;
softPwmWrite (PWM_OUT_PIN, pwmValue) ;
printf ("PWM -> %3d\n", pwmValue) ;
while (digitalRead (PWM_UP) == LOW)
delay (5) ;
}
if (digitalRead (PWM_DOWN) == LOW)
{
pwmValue -= 10 ;
if (pwmValue < 0)
pwmValue = 0 ;
softPwmWrite (PWM_OUT_PIN, pwmValue) ;
printf ("PWM -> %3d\n", pwmValue) ;
while (digitalRead (PWM_DOWN) == LOW)
delay (5) ;
}
delay (5) ;
}
return 0 ;
}

194
examples/PiFace/reaction.c Normal file
View File

@@ -0,0 +1,194 @@
/*
* reaction.c:
* Simple test for the PiFace interface board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <wiringPi.h>
#include <piFace.h>
int outputs [4] = { 0,0,0,0 } ;
#define PIFACE 200
/*
* light:
* Light up the given LED - actually lights up a pair
*********************************************************************************
*/
void light (int led, int value)
{
led *= 2 ;
digitalWrite (PIFACE + led + 0, value) ;
digitalWrite (PIFACE + led + 1, value) ;
}
/*
* lightAll:
* All On or Off
*********************************************************************************
*/
void lightAll (int onoff)
{
light (0, onoff) ;
light (1, onoff) ;
light (2, onoff) ;
light (3, onoff) ;
}
/*
* waitForNoButtons:
* Wait for all buttons to be released
*********************************************************************************
*/
void waitForNoButtons (void)
{
int i, button ;
for (;;)
{
button = 0 ;
for (i = 0 ; i < 4 ; ++i)
button += digitalRead (PIFACE + i) ;
if (button == 4)
break ;
}
}
void scanButton (int button)
{
if (digitalRead (PIFACE + button) == LOW)
{
outputs [button] ^= 1 ;
digitalWrite (PIFACE + button, outputs [button]) ;
}
while (digitalRead (PIFACE + button) == LOW)
delay (1) ;
}
int main (void)
{
int i, j ;
int led, button ;
unsigned int start, stop ;
printf ("Raspberry Pi PiFace Reaction Timer\n") ;
printf ("==================================\n") ;
if (piFaceSetup (PIFACE) == -1)
exit (1) ;
// Enable internal pull-ups
for (i = 0 ; i < 8 ; ++i)
pullUpDnControl (PIFACE + i, PUD_UP) ;
// Main game loop:
// Put some random LED pairs up for a few seconds, then blank ...
for (;;)
{
printf ("Press any button to start ... \n") ; fflush (stdout) ;
for (;;)
{
led = rand () % 4 ;
light (led, 1) ;
delay (10) ;
light (led, 0) ;
button = 0 ;
for (j = 0 ; j < 4 ; ++j)
button += digitalRead (PIFACE + j) ;
if (button != 4)
break ;
}
waitForNoButtons () ;
printf ("Wait for it ... ") ; fflush (stdout) ;
led = rand () % 4 ;
delay (rand () % 500 + 1000) ;
light (led, 1) ;
start = millis () ;
for (button = -1 ; button == -1 ; )
{
for (j = 0 ; j < 4 ; ++j)
if (digitalRead (PIFACE + j) == 0) // Pushed
{
button = j ;
break ;
}
}
stop = millis () ;
button = 3 - button ; // Correct for the buttons/LEDs reversed
light (led, 0) ;
waitForNoButtons () ;
light (led, 1) ;
if (button == led)
{
printf ("You got it in %3d mS\n", stop - start) ;
}
else
{
printf ("Missed: You pushed %d - LED was %d\n", button, led) ;
for (;;)
{
light (button, 1) ;
delay (100) ;
light (button, 0) ;
delay (100) ;
i = 0 ;
for (j = 0 ; j < 4 ; ++j)
i += digitalRead (PIFACE + j) ;
if (i != 4)
break ;
}
waitForNoButtons () ;
}
light (led, 0) ;
delay (4000) ;
}
return 0 ;
}

82
examples/PiGlow/Makefile Normal file
View File

@@ -0,0 +1,82 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://projects.drogon.net/wiring-pi
#
# 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
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

51
examples/PiGlow/piGlow0.c Normal file
View File

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

258
examples/PiGlow/piGlow1.c Normal file
View File

@@ -0,0 +1,258 @@
/*
* 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 <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 ;
}

176
examples/PiGlow/piglow.c Normal file
View File

@@ -0,0 +1,176 @@
/*
* 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://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 <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 ;
}

18
examples/README.TXT Normal file
View File

@@ -0,0 +1,18 @@
wiringPi Examples
=================
There are now too many examples to compile them all in a sensible time,
and you probably don't want to compile or run them all anyway, so they
have been separated out.
To compile an individual example, just type
make exampleName
To really compile everything:
make really-all
The individual tests are:

61
examples/blink-thread.c Normal file
View File

@@ -0,0 +1,61 @@
/*
* blink-thread.c:
* Standard "blink" program in wiringPi. Blinks an LED connected
* to the first GPIO pin.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
// LED Pin - wiringPi pin 0 is BCM_GPIO 17.
#define LED 0
PI_THREAD (blinky)
{
for (;;)
{
digitalWrite (LED, HIGH) ; // On
delay (500) ; // mS
digitalWrite (LED, LOW) ; // Off
delay (500) ;
}
}
int main (void)
{
printf ("Raspberry Pi blink\n") ;
wiringPiSetup () ;
pinMode (LED, OUTPUT) ;
piThreadCreate (blinky) ;
for (;;)
{
printf ("Hello, world\n") ;
delay (600) ;
}
return 0 ;
}

31
examples/blink.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include <wiringPi.h>
#define NUM 17 //26pin
//#define NUM 18 //26pin
//#define NUM 20 //for Orange Pi Zero 2
//#define NUM 19 //for Orange Pi 4
//#define NUM 28 //40pin
int main (void)
{
int i = 0;
wiringPiSetup () ;
for (i = 0; i < NUM; i++)
pinMode (i, OUTPUT) ;
for ( ;; )
{
for (i = 0; i < NUM; i++)
digitalWrite (i, HIGH) ; // On
delay (2000) ; // mS
for (i = 0; i < NUM; i++)
digitalWrite (i, LOW) ; // Off
delay (2000) ;
}
return 0;
}

30
examples/blink.rtb Normal file
View File

@@ -0,0 +1,30 @@
// blink.rtb:
// Blink program in Return to Basic
//
// Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
//**********************************************************************
// 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 <http://www.gnu.org/licenses/>.
***********************************************************************
//
PinMode (0, 1) // Output
CYCLE
DigitalWrite (0, 1) // Pin 0 ON
WAIT (0.5) // 0.5 seconds
DigitalWrite (0, 0)
WAIT (0.5)
REPEAT
END

37
examples/blink.sh Normal file
View File

@@ -0,0 +1,37 @@
#!/bin/sh -e
#
# blink.sh:
# Standard "blink" program in wiringPi. Blinks an LED connected
# to the first GPIO pin.
#
# Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
#######################################################################
# 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 <http://www.gnu.org/licenses/>.
#######################################################################
# LED Pin - wiringPi pin 0 is BCM_GPIO 17.
PIN=0
gpio mode $PIN out
while true; do
gpio write $PIN 1
sleep 0.5
gpio write $PIN 0
sleep 0.5
done

111
examples/blink12.c Normal file
View File

@@ -0,0 +1,111 @@
/*
* blink12.c:
* Simple sequence over the first 12 GPIO pins - LEDs
* Aimed at the Gertboard, but it's fairly generic.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
// Simple sequencer data
// Triplets of LED, On/Off and delay
int data [] =
{
0, 1, 1,
1, 1, 1,
0, 0, 0, 2, 1, 1,
1, 0, 0, 3, 1, 1,
2, 0, 0, 4, 1, 1,
3, 0, 0, 5, 1, 1,
4, 0, 0, 6, 1, 1,
5, 0, 0, 7, 1, 1,
6, 0, 0, 11, 1, 1,
7, 0, 0, 10, 1, 1,
11, 0, 0, 13, 1, 1,
10, 0, 0, 12, 1, 1,
13, 0, 1,
12, 0, 1,
0, 0, 1, // Extra delay
// Back again
12, 1, 1,
13, 1, 1,
12, 0, 0, 10, 1, 1,
13, 0, 0, 11, 1, 1,
10, 0, 0, 7, 1, 1,
11, 0, 0, 6, 1, 1,
7, 0, 0, 5, 1, 1,
6, 0, 0, 4, 1, 1,
5, 0, 0, 3, 1, 1,
4, 0, 0, 2, 1, 1,
3, 0, 0, 1, 1, 1,
2, 0, 0, 0, 1, 1,
1, 0, 1,
0, 0, 1,
0, 0, 1, // Extra delay
0, 9, 0, // End marker
} ;
int main (void)
{
int pin ;
int dataPtr ;
int l, s, d ;
printf ("Raspberry Pi - 12-LED Sequence\n") ;
printf ("==============================\n") ;
printf ("\n") ;
printf ("Connect LEDs up to the first 8 GPIO pins, then pins 11, 10, 13, 12 in\n") ;
printf (" that order, then sit back and watch the show!\n") ;
wiringPiSetup () ;
for (pin = 0 ; pin < 14 ; ++pin)
pinMode (pin, OUTPUT) ;
dataPtr = 0 ;
for (;;)
{
l = data [dataPtr++] ; // LED
s = data [dataPtr++] ; // State
d = data [dataPtr++] ; // Duration (10ths)
if (s == 9) // 9 -> End Marker
{
dataPtr = 0 ;
continue ;
}
digitalWrite (l, s) ;
delay (d * 100) ;
}
return 0 ;
}

125
examples/blink12drcs.c Normal file
View File

@@ -0,0 +1,125 @@
/*
* blink12drcs.c:
* Simple sequence over the first 12 GPIO pins - LEDs
* Aimed at the Gertboard, but it's fairly generic.
* This version uses DRC totalk to the ATmega on the Gertboard
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <drcSerial.h>
#define GERT_BASE 100
static int pinMap [] =
{
0, 1, 2, 3, // Pi Native
GERT_BASE + 2, GERT_BASE + 3, GERT_BASE + 4, GERT_BASE + 5,
GERT_BASE + 6, GERT_BASE + 7, GERT_BASE + 8, GERT_BASE + 9,
} ;
// Simple sequencer data
// Triplets of LED, On/Off and delay
int data [] =
{
0, 1, 1,
1, 1, 1,
0, 0, 0, 2, 1, 1,
1, 0, 0, 3, 1, 1,
2, 0, 0, 4, 1, 1,
3, 0, 0, 5, 1, 1,
4, 0, 0, 6, 1, 1,
5, 0, 0, 7, 1, 1,
6, 0, 0, 8, 1, 1,
7, 0, 0, 9, 1, 1,
8, 0, 0, 10, 1, 1,
9, 0, 0, 11, 1, 1,
10, 0, 1,
11, 0, 1,
0, 0, 1, // Extra delay
// Back again
11, 1, 1,
10, 1, 1,
11, 0, 0, 9, 1, 1,
10, 0, 0, 8, 1, 1,
9, 0, 0, 7, 1, 1,
8, 0, 0, 6, 1, 1,
7, 0, 0, 5, 1, 1,
6, 0, 0, 4, 1, 1,
5, 0, 0, 3, 1, 1,
4, 0, 0, 2, 1, 1,
3, 0, 0, 1, 1, 1,
2, 0, 0, 0, 1, 1,
1, 0, 1,
0, 0, 1,
0, 0, 1, // Extra delay
0, 9, 0, // End marker
} ;
int main (void)
{
int pin ;
int dataPtr ;
int l, s, d ;
printf ("Raspberry Pi - 12-LED Sequence\n") ;
printf ("==============================\n") ;
printf ("\n") ;
printf ("Connect LEDs up to the first 4 Pi pins and 8 pins on the ATmega\n") ;
printf (" from PD2 through PB1 in that order,\n") ;
printf (" then sit back and watch the show!\n") ;
wiringPiSetup () ;
drcSetupSerial (GERT_BASE, 20, "/dev/ttyAMA0", 115200) ;
for (pin = 0 ; pin < 12 ; ++pin)
pinMode (pinMap [pin], OUTPUT) ;
dataPtr = 0 ;
for (;;)
{
l = data [dataPtr++] ; // LED
s = data [dataPtr++] ; // State
d = data [dataPtr++] ; // Duration (10ths)
if (s == 9) // 9 -> End Marker
{
dataPtr = 0 ;
continue ;
}
digitalWrite (pinMap [l], s) ;
delay (d * analogRead (GERT_BASE) / 4) ;
}
return 0 ;
}

115
examples/blink6drcs.c Normal file
View File

@@ -0,0 +1,115 @@
/*
* blink6drcs.c:
* Simple sequence over 6 pins on a remote DRC board.
* Aimed at the Gertduino, but it's fairly generic.
* This version uses DRC to talk to the ATmega on the Gertduino
*
* Copyright (c) 2012-2014 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <drcSerial.h>
#define GERT_BASE 100
static int pinMap [] =
{
GERT_BASE + 6, GERT_BASE + 5, GERT_BASE + 3, GERT_BASE + 10, GERT_BASE + 9, GERT_BASE + 13,
} ;
// Simple sequencer data
// Triplets of LED, On/Off and delay
int data [] =
{
0, 1, 1,
1, 1, 1,
0, 0, 0, 2, 1, 1,
1, 0, 0, 3, 1, 1,
2, 0, 0, 4, 1, 1,
3, 0, 0, 5, 1, 1,
4, 0, 1,
5, 0, 1,
0, 0, 1, // Extra delay
// Back again
5, 1, 1,
4, 1, 1,
5, 0, 0, 3, 1, 1,
4, 0, 0, 2, 1, 1,
3, 0, 0, 1, 1, 1,
2, 0, 0, 0, 1, 1,
1, 0, 1,
0, 0, 1,
0, 0, 1, // Extra delay
0, 9, 0, // End marker
} ;
int main (void)
{
int pin ;
int dataPtr ;
int l, s, d ;
printf ("Raspberry Pi - 6-LED Sequence\n") ;
printf ("=============================\n") ;
printf ("\n") ;
printf (" Use the 2 buttons to temporarily speed up the sequence\n") ;
wiringPiSetupSys () ; // Not using the Pi's GPIO here
drcSetupSerial (GERT_BASE, 20, "/dev/ttyAMA0", 115200) ;
for (pin = 0 ; pin < 6 ; ++pin)
pinMode (pinMap [pin], OUTPUT) ;
pinMode (GERT_BASE + 16, INPUT) ; // Buttons
pinMode (GERT_BASE + 17, INPUT) ;
pullUpDnControl (GERT_BASE + 16, PUD_UP) ;
pullUpDnControl (GERT_BASE + 17, PUD_UP) ;
dataPtr = 0 ;
for (;;)
{
l = data [dataPtr++] ; // LED
s = data [dataPtr++] ; // State
d = data [dataPtr++] ; // Duration (10ths)
if (s == 9) // 9 -> End Marker
{
dataPtr = 0 ;
continue ;
}
digitalWrite (pinMap [l], s) ;
delay (d * digitalRead (GERT_BASE + 16) * 15 + digitalRead (GERT_BASE + 17) * 20) ;
}
return 0 ;
}

61
examples/blink8-drcn.c Normal file
View File

@@ -0,0 +1,61 @@
/*
* blink8-drcn.c:
* Simple sequence over the first 8 GPIO pins - LEDs
* Aimed at the Ladder board, but it's fairly generic.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <drcNet.h>
int main (void)
{
int i, led ;
printf ("Raspberry Pi - 8-LED Sequencer\n") ;
printf ("==============================\n") ;
printf ("\n") ;
printf ("Connect LEDs to the first 8 GPIO pins and watch ...\n") ;
int pinBase = 100 ;
// wiringPiSetup () ;
drcSetupNet (pinBase, 100, "192.168.254.21", "6124", "123456") ;
for (i = 0 ; i < 8 ; ++i)
pinMode (i + pinBase, OUTPUT) ;
for (;;)
{
for (led = 0 ; led < 8 ; ++led)
{
digitalWrite (led + pinBase, 1) ;
delay (10) ;
}
for (led = 0 ; led < 8 ; ++led)
{
digitalWrite (led + pinBase, 0) ;
delay (10) ;
}
}
}

57
examples/blink8.c Normal file
View File

@@ -0,0 +1,57 @@
/*
* blink8.c:
* Simple sequence over the first 8 GPIO pins - LEDs
* Aimed at the Gertboard, but it's fairly generic.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
int main (void)
{
int i, led ;
printf ("Raspberry Pi - 8-LED Sequencer\n") ;
printf ("==============================\n") ;
printf ("\n") ;
printf ("Connect LEDs to the first 8 GPIO pins and watch ...\n") ;
wiringPiSetup () ;
for (i = 0 ; i < 8 ; ++i)
pinMode (i, OUTPUT) ;
for (;;)
{
for (led = 0 ; led < 8 ; ++led)
{
digitalWrite (led, 1) ;
delay (100) ;
}
for (led = 0 ; led < 8 ; ++led)
{
digitalWrite (led, 0) ;
delay (100) ;
}
}
}

201
examples/clock.c Normal file
View File

@@ -0,0 +1,201 @@
/*
* clock.c:
* Demo of the 128x64 graphics based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based on the popular 12864H controller chip.
*
* This test program assumes the following:
* (Which is currently hard-wired into the driver)
*
* GPIO 0-7 is connected to display data pins 0-7.
* GPIO 10 is CS1
* GPIO 11 is CS2
* GPIO 12 is STROBE
* GPIO 10 is RS
*
* Copyright (c) 2012-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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <math.h>
#include <wiringPi.h>
#include <lcd128x64.h>
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
double clockRadius ;
double thickness, barLen ;
int maxX, maxY ;
double rads (double degs)
{
return degs * M_PI / 180.0 ;
}
void drawClockHands (void)
{
time_t t ;
struct tm *now ;
double angle, p, x0, y0, x1, y1 ;
int h24, h, m, s ;
char text [20] ;
time (&t) ;
now = localtime (&t) ;
h24 = now->tm_hour ;
m = now->tm_min ;
s = now->tm_sec ;
h = h24 ;
if (h > 12)
h -= 12 ;
// Hour hand
angle = h * 30 + m * 0.5 ;
x0 = sin (rads (angle)) * (clockRadius * 0.75) ;
y0 = cos (rads (angle)) * (clockRadius * 0.75) ;
for (p = -3.0 ; p <= 3.0 ; p += 0.2)
{
x1 = sin (rads (angle + p)) * (clockRadius * 0.7) ;
y1 = cos (rads (angle + p)) * (clockRadius * 0.7) ;
lcd128x64line (0, 0, x1, y1, 1) ;
lcd128x64lineTo (x0, y0, 1) ;
}
// Minute hand
angle = m * 6 ;
x0 = sin (rads (angle)) * (clockRadius * 0.9) ;
y0 = cos (rads (angle)) * (clockRadius * 0.9) ;
for (p = -1.0 ; p <= 1.0 ; p += 0.2)
{
x1 = sin (rads (angle + p)) * (clockRadius * 0.85) ;
y1 = cos (rads (angle + p)) * (clockRadius * 0.85) ;
lcd128x64line (0, 0, x1, y1, 1) ;
lcd128x64lineTo (x0, y0, 1) ;
}
// Second hand
angle = s * 6 ;
x0 = sin (rads (angle)) * (clockRadius * 0.2) ;
y0 = cos (rads (angle)) * (clockRadius * 0.2) ;
x1 = sin (rads (angle)) * (clockRadius * 0.95) ;
y1 = cos (rads (angle)) * (clockRadius * 0.95) ;
lcd128x64line (0 - x0, 0 - y0, x1, y1, 1) ;
lcd128x64circle (0, 0, clockRadius * 0.1, 0, 1) ;
lcd128x64circle (0, 0, clockRadius * 0.05, 1, 1) ;
// Text:
sprintf (text, "%02d:%02d:%02d", h24, m, s) ;
lcd128x64puts (32, 24, text, 0, 1) ;
sprintf (text, "%2d/%2d/%2d", now->tm_mday, now->tm_mon + 1, now->tm_year - 100) ;
lcd128x64puts (32, -23, text, 0, 1) ;
}
void drawClockFace (void)
{
int m ;
double d, px1, py1, px2, py2 ;
lcd128x64clear (0) ;
lcd128x64circle (0,0, clockRadius, 1, TRUE) ;
lcd128x64circle (0,0, clockRadius - thickness, 0, TRUE) ;
// The four big indicators for 12,15,30 and 45
lcd128x64rectangle (- 3, clockRadius - barLen, 3, clockRadius, 1, TRUE) ; // 12
lcd128x64rectangle (clockRadius - barLen, 3, clockRadius, -3, 1, TRUE) ; // 3
lcd128x64rectangle (- 3, -clockRadius + barLen, 3, -clockRadius, 1, TRUE) ; // 6
lcd128x64rectangle (-clockRadius + barLen, 3, -clockRadius, -3, 1, TRUE) ; // 9
// Smaller 5 and 1 minute ticks
for (m = 0 ; m < 60 ; ++m)
{
px1 = sin (rads (m * 6)) * clockRadius ;
py1 = cos (rads (m * 6)) * clockRadius ;
if ((m % 5) == 0)
d = barLen ;
else
d = barLen / 2.0 ;
px2 = sin (rads (m * 6)) * (clockRadius - d) ;
py2 = cos (rads (m * 6)) * (clockRadius - d) ;
lcd128x64line (px1, py1, px2, py2, 1) ;
}
}
void setup (void)
{
lcd128x64getScreenSize (&maxX, &maxY) ;
clockRadius = maxY / 2 - 1 ;
thickness = maxX / 48 ;
barLen = thickness * 4 ;
lcd128x64setOrigin (32, 32) ;
}
/*
***********************************************************************
* The main program
***********************************************************************
*/
int main (int argc, char *argv [])
{
time_t now ;
wiringPiSetup () ;
lcd128x64setup () ;
setup () ;
for (;;)
{
drawClockFace () ;
drawClockHands () ;
lcd128x64update () ;
now = time (NULL) ;
while (time (NULL) == now)
delay (10) ;
}
return 0 ;
}

102
examples/delayTest.c Normal file
View File

@@ -0,0 +1,102 @@
/*
* delayTest.c:
* Just a little test program I'm using to experiment with
* various timings and latency, etc.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#define CYCLES 1000
int main()
{
int x ;
struct timeval t1, t2, t3 ;
int t ;
int max, min ;
int del ;
int underRuns, overRuns, exactRuns, bogusRuns, total ;
int descheds ;
// Baseline test
gettimeofday (&t1, NULL) ;
gettimeofday (&t2, NULL) ;
t = t2.tv_usec - t1.tv_usec ;
printf ("Baseline test: %d\n", t);
for (del = 1 ; del < 200 ; ++del)
{
underRuns = overRuns = exactRuns = total = 0 ;
descheds = 0 ;
max = 0 ;
min = 999 ;
for (x = 0 ; x < CYCLES ; ++x)
{
for (;;) // Repeat this if we get a delay over 999uS
{ // -> High probability Linux has deschedulled us
gettimeofday (&t1, NULL) ;
usleep (del) ;
// delayMicroseconds (del) ;
gettimeofday (&t2, NULL) ;
timersub (&t2, &t1, &t3) ;
t = t3.tv_usec ;
if (t > 999)
{
++descheds ;
continue ;
}
else
break ;
}
if (t == del)
++exactRuns ;
else if (t < del)
++underRuns ;
else if (t > del)
++overRuns ;
if (t > max)
max = t ;
else if (t < min)
min = t ;
total += t ;
}
printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n",
del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ;
fflush (stdout) ;
usleep (1000) ;
}
return 0 ;
}

238
examples/ds1302.c Normal file
View File

@@ -0,0 +1,238 @@
/*
* ds1302.c:
* Real Time clock
*
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <wiringPi.h>
#include <ds1302.h>
// Register defines
#define RTC_SECS 0
#define RTC_MINS 1
#define RTC_HOURS 2
#define RTC_DATE 3
#define RTC_MONTH 4
#define RTC_DAY 5
#define RTC_YEAR 6
#define RTC_WP 7
#define RTC_TC 8
#define RTC_BM 31
static unsigned int masks [] = { 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x07, 0xFF } ;
/*
* bcdToD: dToBCD:
* BCD decode/encode
*********************************************************************************
*/
static int bcdToD (unsigned int byte, unsigned int mask)
{
unsigned int b1, b2 ;
byte &= mask ;
b1 = byte & 0x0F ;
b2 = ((byte >> 4) & 0x0F) * 10 ;
return b1 + b2 ;
}
static unsigned int dToBcd (unsigned int byte)
{
return ((byte / 10) << 4) + (byte % 10) ;
}
/*
* ramTest:
* Simple test of the 31 bytes of RAM inside the DS1302 chip
*********************************************************************************
*/
static int ramTestValues [] =
{ 0x00, 0xFF, 0xAA, 0x55, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0xF0, 0x0F, -1 } ;
static int ramTest (void)
{
int addr ;
int got ;
int i = 0 ;
int errors = 0 ;
int testVal ;
printf ("DS1302 RAM TEST\n") ;
testVal = ramTestValues [i] ;
while (testVal != -1)
{
for (addr = 0 ; addr < 31 ; ++addr)
ds1302ramWrite (addr, testVal) ;
for (addr = 0 ; addr < 31 ; ++addr)
if ((got = ds1302ramRead (addr)) != testVal)
{
printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n",
addr, testVal, got) ;
++errors ;
}
testVal = ramTestValues [++i] ;
}
for (addr = 0 ; addr < 31 ; ++addr)
ds1302ramWrite (addr, addr) ;
for (addr = 0 ; addr < 31 ; ++addr)
if ((got = ds1302ramRead (addr)) != addr)
{
printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n",
addr, addr, got) ;
++errors ;
}
if (errors == 0)
printf ("-- DS1302 RAM TEST: OK\n") ;
else
printf ("-- DS1302 RAM TEST FAILURE. %d errors.\n", errors) ;
return 0 ;
}
/*
* setLinuxClock:
* Set the Linux clock from the hardware
*********************************************************************************
*/
static int setLinuxClock (void)
{
char dateTime [20] ;
char command [64] ;
int clock [8] ;
printf ("Setting the Linux Clock from the DS1302... ") ; fflush (stdout) ;
ds1302clockRead (clock) ;
// [MMDDhhmm[[CC]YY][.ss]]
sprintf (dateTime, "%02d%02d%02d%02d%02d%02d.%02d",
bcdToD (clock [RTC_MONTH], masks [RTC_MONTH]),
bcdToD (clock [RTC_DATE], masks [RTC_DATE]),
bcdToD (clock [RTC_HOURS], masks [RTC_HOURS]),
bcdToD (clock [RTC_MINS], masks [RTC_MINS]),
20,
bcdToD (clock [RTC_YEAR], masks [RTC_YEAR]),
bcdToD (clock [RTC_SECS], masks [RTC_SECS])) ;
sprintf (command, "/bin/date %s", dateTime) ;
system (command) ;
return 0 ;
}
/*
* setDSclock:
* Set the DS1302 block from Linux time
*********************************************************************************
*/
static int setDSclock (void)
{
struct tm t ;
time_t now ;
int clock [8] ;
printf ("Setting the clock in the DS1302 from Linux time... ") ;
now = time (NULL) ;
gmtime_r (&now, &t) ;
clock [ 0] = dToBcd (t.tm_sec) ; // seconds
clock [ 1] = dToBcd (t.tm_min) ; // mins
clock [ 2] = dToBcd (t.tm_hour) ; // hours
clock [ 3] = dToBcd (t.tm_mday) ; // date
clock [ 4] = dToBcd (t.tm_mon + 1) ; // months 0-11 --> 1-12
clock [ 5] = dToBcd (t.tm_wday + 1) ; // weekdays (sun 0)
clock [ 6] = dToBcd (t.tm_year - 100) ; // years
clock [ 7] = 0 ; // W-Protect off
ds1302clockWrite (clock) ;
printf ("OK\n") ;
return 0 ;
}
int main (int argc, char *argv [])
{
int i ;
int clock [8] ;
wiringPiSetup () ;
ds1302setup (0, 1, 2) ;
if (argc == 2)
{
/**/ if (strcmp (argv [1], "-slc") == 0)
return setLinuxClock () ;
else if (strcmp (argv [1], "-sdsc") == 0)
return setDSclock () ;
else if (strcmp (argv [1], "-rtest") == 0)
return ramTest () ;
else
{
printf ("Usage: ds1302 [-slc | -sdsc | -rtest]\n") ;
return EXIT_FAILURE ;
}
}
for (i = 0 ;; ++i)
{
printf ("%5d: ", i) ;
ds1302clockRead (clock) ;
printf (" %2d:%02d:%02d",
bcdToD (clock [2], masks [2]), bcdToD (clock [1], masks [1]), bcdToD (clock [0], masks [0])) ;
printf (" %2d/%02d/%04d",
bcdToD (clock [3], masks [3]), bcdToD (clock [4], masks [4]), bcdToD (clock [6], masks [6]) + 2000) ;
printf ("\n") ;
delay (200) ;
}
return 0 ;
}

23
examples/header.h Normal file
View File

@@ -0,0 +1,23 @@
/*
* file.c:
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

118
examples/isr-osc.c Normal file
View File

@@ -0,0 +1,118 @@
/*
* isr-osc.c:
* Wait for Interrupt test program - ISR method - interrupt oscillator
*
* How to test:
*
* IMPORTANT: To run this test we connect 2 GPIO pins together, but
* before we do that YOU must make sure that they are both setup
* the right way. If they are set to outputs and one is high and one low,
* then you connect the wire, you'll create a short and that won't be good.
*
* Before making the connection, type:
* gpio mode 0 output
* gpio write 0 0
* gpio mode 1 input
* then you can connect them together.
*
* Run the program, then:
* gpio write 0 1
* gpio write 0 0
*
* at which point it will trigger an interrupt and the program will
* then do the up/down toggling for itself and run at full speed, and
* it will report the number of interrupts recieved every second.
*
* Copyright (c) 2013 Gordon Henderson. projects@drogon.net
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
// What GPIO input are we using?
// This is a wiringPi pin number
#define OUT_PIN 0
#define IN_PIN 1
// globalCounter:
// Global variable to count interrupts
// Should be declared volatile to make sure the compiler doesn't cache it.
static volatile int globalCounter = 0 ;
/*
* myInterrupt:
*********************************************************************************
*/
void myInterrupt (void)
{
digitalWrite (OUT_PIN, 1) ;
++globalCounter ;
digitalWrite (OUT_PIN, 0) ;
}
/*
*********************************************************************************
* main
*********************************************************************************
*/
int main (void)
{
int myCounter = 0 ;
int lastCounter = 0 ;
if (wiringPiSetup () < 0)
{
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
pinMode (OUT_PIN, OUTPUT) ;
pinMode (IN_PIN, INPUT) ;
if (wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
{
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}
for (;;)
{
printf ("Waiting ... ") ; fflush (stdout) ;
while (myCounter == globalCounter)
delay (1000) ;
printf (" Done. counter: %6d: %6d\n",
globalCounter, myCounter - lastCounter) ;
lastCounter = myCounter ;
myCounter = globalCounter ;
}
return 0 ;
}

110
examples/isr.c Normal file
View File

@@ -0,0 +1,110 @@
/*
* isr.c:
* Wait for Interrupt test program - ISR method
*
* How to test:
* Use the SoC's pull-up and pull down resistors that are avalable
* on input pins. So compile & run this program (via sudo), then
* in another terminal:
* gpio mode 0 up
* gpio mode 0 down
* at which point it should trigger an interrupt. Toggle the pin
* up/down to generate more interrupts to test.
*
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
// globalCounter:
// Global variable to count interrupts
// Should be declared volatile to make sure the compiler doesn't cache it.
static volatile int globalCounter [8] ;
/*
* myInterrupt:
*********************************************************************************
*/
void myInterrupt0 (void) { ++globalCounter [0] ; }
void myInterrupt1 (void) { ++globalCounter [1] ; }
void myInterrupt2 (void) { ++globalCounter [2] ; }
void myInterrupt3 (void) { ++globalCounter [3] ; }
void myInterrupt4 (void) { ++globalCounter [4] ; }
void myInterrupt5 (void) { ++globalCounter [5] ; }
void myInterrupt6 (void) { ++globalCounter [6] ; }
void myInterrupt7 (void) { ++globalCounter [7] ; }
/*
*********************************************************************************
* main
*********************************************************************************
*/
int main (void)
{
int gotOne, pin ;
int myCounter [8] ;
for (pin = 0 ; pin < 8 ; ++pin)
globalCounter [pin] = myCounter [pin] = 0 ;
wiringPiSetup () ;
wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ;
wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ;
wiringPiISR (2, INT_EDGE_FALLING, &myInterrupt2) ;
wiringPiISR (3, INT_EDGE_FALLING, &myInterrupt3) ;
wiringPiISR (4, INT_EDGE_FALLING, &myInterrupt4) ;
wiringPiISR (5, INT_EDGE_FALLING, &myInterrupt5) ;
wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ;
wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ;
for (;;)
{
gotOne = 0 ;
printf ("Waiting ... ") ; fflush (stdout) ;
for (;;)
{
for (pin = 0 ; pin < 8 ; ++pin)
{
if (globalCounter [pin] != myCounter [pin])
{
printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ;
myCounter [pin] = globalCounter [pin] ;
++gotOne ;
}
}
if (gotOne != 0)
break ;
}
}
return 0 ;
}

347
examples/lcd-adafruit.c Normal file
View File

@@ -0,0 +1,347 @@
/*
* lcd-adafruit.c:
* Text-based LCD driver test code
* This is designed to drive the Adafruit RGB LCD Plate
* with the additional 5 buttons for the Raspberry Pi
*
* Copyright (c) 2012-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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <wiringPi.h>
#include <mcp23017.h>
#include <lcd.h>
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
// Defines for the Adafruit Pi LCD interface board
#define AF_BASE 100
#define AF_RED (AF_BASE + 6)
#define AF_GREEN (AF_BASE + 7)
#define AF_BLUE (AF_BASE + 8)
#define AF_E (AF_BASE + 13)
#define AF_RW (AF_BASE + 14)
#define AF_RS (AF_BASE + 15)
#define AF_DB4 (AF_BASE + 12)
#define AF_DB5 (AF_BASE + 11)
#define AF_DB6 (AF_BASE + 10)
#define AF_DB7 (AF_BASE + 9)
#define AF_SELECT (AF_BASE + 0)
#define AF_RIGHT (AF_BASE + 1)
#define AF_DOWN (AF_BASE + 2)
#define AF_UP (AF_BASE + 3)
#define AF_LEFT (AF_BASE + 4)
// User-Defined character test
static unsigned char newChar [8] =
{
0b00100,
0b00100,
0b00000,
0b00100,
0b01110,
0b11011,
0b11011,
0b10001,
} ;
// Global lcd handle:
static int lcdHandle ;
/*
* usage:
*********************************************************************************
*/
int usage (const char *progName)
{
fprintf (stderr, "Usage: %s colour\n", progName) ;
return EXIT_FAILURE ;
}
/*
* scrollMessage:
*********************************************************************************
*/
static const char *message =
" "
"Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
" " ;
void scrollMessage (int line, int width)
{
char buf [32] ;
static int position = 0 ;
static int timer = 0 ;
if (millis () < timer)
return ;
timer = millis () + 200 ;
strncpy (buf, &message [position], width) ;
buf [width] = 0 ;
lcdPosition (lcdHandle, 0, line) ;
lcdPuts (lcdHandle, buf) ;
if (++position == (strlen (message) - width))
position = 0 ;
}
/*
* setBacklightColour:
* The colour outputs are inverted.
*********************************************************************************
*/
static void setBacklightColour (int colour)
{
colour &= 7 ;
digitalWrite (AF_RED, !(colour & 1)) ;
digitalWrite (AF_GREEN, !(colour & 2)) ;
digitalWrite (AF_BLUE, !(colour & 4)) ;
}
/*
* adafruitLCDSetup:
* Setup the Adafruit board by making sure the additional pins are
* set to the correct modes, etc.
*********************************************************************************
*/
static void adafruitLCDSetup (int colour)
{
int i ;
// Backlight LEDs
pinMode (AF_RED, OUTPUT) ;
pinMode (AF_GREEN, OUTPUT) ;
pinMode (AF_BLUE, OUTPUT) ;
setBacklightColour (colour) ;
// Input buttons
for (i = 0 ; i <= 4 ; ++i)
{
pinMode (AF_BASE + i, INPUT) ;
pullUpDnControl (AF_BASE + i, PUD_UP) ; // Enable pull-ups, switches close to 0v
}
// Control signals
pinMode (AF_RW, OUTPUT) ; digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode
// The other control pins are initialised with lcdInit ()
lcdHandle = lcdInit (2, 16, 4, AF_RS, AF_E, AF_DB4,AF_DB5,AF_DB6,AF_DB7, 0,0,0,0) ;
if (lcdHandle < 0)
{
fprintf (stderr, "lcdInit failed\n") ;
exit (EXIT_FAILURE) ;
}
}
/*
* waitForEnter:
* On the Adafruit display, wait for the select button
*********************************************************************************
*/
static void waitForEnter (void)
{
printf ("Press SELECT to continue: ") ; fflush (stdout) ;
while (digitalRead (AF_SELECT) == HIGH) // Wait for push
delay (1) ;
while (digitalRead (AF_SELECT) == LOW) // Wait for release
delay (1) ;
printf ("OK\n") ;
}
/*
* speedTest:
* Test the update speed of the display
*********************************************************************************
*/
static void speedTest (void)
{
unsigned int start, end, taken ;
int times ;
lcdClear (lcdHandle) ;
start = millis () ;
for (times = 0 ; times < 10 ; ++times)
{
lcdPuts (lcdHandle, "0123456789ABCDEF") ;
lcdPuts (lcdHandle, "0123456789ABCDEF") ;
}
end = millis () ;
taken = (end - start) / 10;
lcdClear (lcdHandle) ;
lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Speed: %dmS", taken) ;
lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "For full update") ;
waitForEnter () ;
lcdClear (lcdHandle) ;
lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Time: %dmS", taken / 32) ;
lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "Per character") ;
waitForEnter () ;
lcdClear (lcdHandle) ;
lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "%d cps...", 32000 / taken) ;
waitForEnter () ;
}
/*
* The works
*********************************************************************************
*/
int main (int argc, char *argv[])
{
int colour ;
int cols = 16 ;
int waitForRelease = FALSE ;
struct tm *t ;
time_t tim ;
char buf [32] ;
if (argc != 2)
return usage (argv [0]) ;
printf ("Raspberry Pi Adafruit LCD test\n") ;
printf ("==============================\n") ;
colour = atoi (argv [1]) ;
wiringPiSetupSys () ;
mcp23017Setup (AF_BASE, 0x20) ;
adafruitLCDSetup (colour) ;
lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
waitForEnter () ;
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "Adafruit RGB LCD") ;
waitForEnter () ;
lcdCharDef (lcdHandle, 2, newChar) ;
lcdClear (lcdHandle) ;
lcdPosition (lcdHandle, 0, 0) ;
lcdPuts (lcdHandle, "User Char: ") ;
lcdPutchar (lcdHandle, 2) ;
lcdCursor (lcdHandle, TRUE) ;
lcdCursorBlink (lcdHandle, TRUE) ;
waitForEnter () ;
lcdCursor (lcdHandle, FALSE) ;
lcdCursorBlink (lcdHandle, FALSE) ;
speedTest () ;
lcdClear (lcdHandle) ;
for (;;)
{
scrollMessage (0, cols) ;
tim = time (NULL) ;
t = localtime (&tim) ;
sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
lcdPuts (lcdHandle, buf) ;
// Check buttons to cycle colour
// If Up or Down are still pushed, then skip
if (waitForRelease)
{
if ((digitalRead (AF_UP) == LOW) || (digitalRead (AF_DOWN) == LOW))
continue ;
else
waitForRelease = FALSE ;
}
if (digitalRead (AF_UP) == LOW) // Pushed
{
colour = colour + 1 ;
if (colour == 8)
colour = 0 ;
setBacklightColour (colour) ;
waitForRelease = TRUE ;
}
if (digitalRead (AF_DOWN) == LOW) // Pushed
{
colour = colour - 1 ;
if (colour == -1)
colour = 7 ;
setBacklightColour (colour) ;
waitForRelease = TRUE ;
}
}
return 0 ;
}

286
examples/lcd.c Normal file
View File

@@ -0,0 +1,286 @@
/*
* lcd.c:
* Text-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based in the Hitachi HD44780U controller and compatables.
*
* This test program assumes the following:
*
* 8-bit displays:
* GPIO 0-7 is connected to display data pins 0-7.
* GPIO 11 is the RS pin.
* GPIO 10 is the Strobe/E pin.
*
* For 4-bit interface:
* GPIO 4-7 is connected to display data pins 4-7.
* GPIO 11 is the RS pin.
* GPIO 10 is the Strobe/E pin.
*
* Copyright (c) 2012-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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <wiringPi.h>
#include <lcd.h>
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
static unsigned char newChar [8] =
{
0b11111,
0b10001,
0b10001,
0b10101,
0b11111,
0b10001,
0b10001,
0b11111,
} ;
// Global lcd handle:
static int lcdHandle ;
/*
* usage:
*********************************************************************************
*/
int usage (const char *progName)
{
fprintf (stderr, "Usage: %s bits cols rows\n", progName) ;
return EXIT_FAILURE ;
}
/*
* scrollMessage:
*********************************************************************************
*/
static const char *message =
" "
"Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
" " ;
void scrollMessage (int line, int width)
{
char buf [32] ;
static int position = 0 ;
static int timer = 0 ;
if (millis () < timer)
return ;
timer = millis () + 200 ;
strncpy (buf, &message [position], width) ;
buf [width] = 0 ;
lcdPosition (lcdHandle, 0, line) ;
lcdPuts (lcdHandle, buf) ;
if (++position == (strlen (message) - width))
position = 0 ;
}
/*
* pingPong:
* Bounce a character - only on 4-line displays
*********************************************************************************
*/
static void pingPong (int lcd, int cols)
{
static int position = 0 ;
static int dir = 0 ;
if (dir == 0) // Setup
{
dir = 1 ;
lcdPosition (lcdHandle, 0, 3) ;
lcdPutchar (lcdHandle, '*') ;
return ;
}
lcdPosition (lcdHandle, position, 3) ;
lcdPutchar (lcdHandle, ' ') ;
position += dir ;
if (position == cols)
{
dir = -1 ;
--position ;
}
if (position < 0)
{
dir = 1 ;
++position ;
}
lcdPosition (lcdHandle, position, 3) ;
lcdPutchar (lcdHandle, '#') ;
}
/*
* waitForEnter:
*********************************************************************************
*/
static void waitForEnter (void)
{
printf ("Press ENTER to continue: ") ;
(void)fgetc (stdin) ;
}
/*
* The works
*********************************************************************************
*/
int main (int argc, char *argv[])
{
int i ;
int lcd ;
int bits, rows, cols ;
struct tm *t ;
time_t tim ;
char buf [32] ;
if (argc != 4)
return usage (argv [0]) ;
printf ("Raspberry Pi LCD test\n") ;
printf ("=====================\n") ;
bits = atoi (argv [1]) ;
cols = atoi (argv [2]) ;
rows = atoi (argv [3]) ;
if (!((rows == 1) || (rows == 2) || (rows == 4)))
{
fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ;
return EXIT_FAILURE ;
}
if (!((cols == 16) || (cols == 20)))
{
fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
return EXIT_FAILURE ;
}
wiringPiSetup () ;
if (bits == 4)
lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ;
else
lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ;
if (lcdHandle < 0)
{
fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
return -1 ;
}
lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
waitForEnter () ;
if (rows > 1)
{
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
if (rows == 4)
{
lcdPosition (lcdHandle, 0, 2) ;
for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
lcdPuts (lcdHandle, "=-") ;
lcdPuts (lcdHandle, "=3") ;
lcdPosition (lcdHandle, 0, 3) ;
for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
lcdPuts (lcdHandle, "-=") ;
lcdPuts (lcdHandle, "-4") ;
}
}
waitForEnter () ;
lcdCharDef (lcdHandle, 2, newChar) ;
lcdClear (lcdHandle) ;
lcdPosition (lcdHandle, 0, 0) ;
lcdPuts (lcdHandle, "User Char: ") ;
lcdPutchar (lcdHandle, 2) ;
lcdCursor (lcdHandle, TRUE) ;
lcdCursorBlink (lcdHandle, TRUE) ;
waitForEnter () ;
lcdCursor (lcdHandle, FALSE) ;
lcdCursorBlink (lcdHandle, FALSE) ;
lcdClear (lcdHandle) ;
for (;;)
{
scrollMessage (0, cols) ;
if (rows == 1)
continue ;
tim = time (NULL) ;
t = localtime (&tim) ;
sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
lcdPuts (lcdHandle, buf) ;
if (rows == 2)
continue ;
sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;
lcdPosition (lcdHandle, (cols - 10) / 2, 2) ;
lcdPuts (lcdHandle, buf) ;
pingPong (lcd, cols) ;
}
return 0 ;
}

68
examples/lowPower.c Normal file
View File

@@ -0,0 +1,68 @@
/*
* lowPower.c:
* Check the Pi's LOW-Power signal.
*
* This is a demonstration program that could be turned into some sort
* of logger via e.g. syslog - however it's also probably something
* that might be better handled by a future kernel - who knows.
*
* Copyright (c) 2014 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <time.h>
#include <wiringPi.h>
#define LOW_POWER 35
/*
* lowPower:
* This is an ISR that waits for the low-power signal going low and
* prints the result.
*********************************************************************************
*/
void lowPower (void)
{
time_t t ;
time (&t) ;
printf ("%s: LOW POWER DETECTED\n", ctime (&t)) ;
}
/*
*********************************************************************************
* main
*********************************************************************************
*/
int main (void)
{
wiringPiSetupGpio () ; // GPIO mode as it's an internal pin
wiringPiISR (LOW_POWER, INT_EDGE_FALLING, &lowPower) ;
for (;;)
delay (1000) ;
return 0 ;
}

60
examples/max31855.c Normal file
View File

@@ -0,0 +1,60 @@
/*
* max31855.c:
* SPI Thermocouple interface chip
*
* Copyright (c) 2015 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <wiringPi.h>
#include <max31855.h>
int main (int argc, char *argv [])
{
int i = 0 ;
wiringPiSetup () ;
max31855Setup (200, 0) ;
max31855Setup (400, 1) ;
for (;;)
{
if (i == 0)
{
printf ("+------+------+------+------++------+------+------+------+\n") ;
printf ("| Raw | Err | C | F || Raw | Err | C | F |\n") ;
printf ("+------+------+------+------++------+------+------+------+\n") ;
}
printf ("| %4d | %4d | %4d | %4d |", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ;
printf ("| %4d | %4d | %4d | %4d |\n", analogRead (400), analogRead (401), analogRead (402), analogRead (403)) ;
delay (500) ;
if (++i == 10)
i = 0 ;
}
}

67
examples/nes.c Normal file
View File

@@ -0,0 +1,67 @@
/*
* nes.c:
* Test program for an old NES controller connected to the Pi.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <piNes.h>
#define BLANK "| "
int main ()
{
int joystick ;
unsigned int buttons ;
if (wiringPiSetup () == -1)
{
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
return 1 ;
}
if ((joystick = setupNesJoystick (2, 1, 0)) == -1)
{
fprintf (stdout, "Unable to setup joystick\n") ;
return 1 ;
}
for (;;)
{
buttons = readNesJoystick (joystick) ;
if ((buttons & NES_UP) != 0) printf ("| UP " ) ; else printf (BLANK) ;
if ((buttons & NES_DOWN) != 0) printf ("| DOWN " ) ; else printf (BLANK) ;
if ((buttons & NES_LEFT) != 0) printf ("| LEFT " ) ; else printf (BLANK) ;
if ((buttons & NES_RIGHT) != 0) printf ("|RIGHT " ) ; else printf (BLANK) ;
if ((buttons & NES_SELECT) != 0) printf ("|SELECT" ) ; else printf (BLANK) ;
if ((buttons & NES_START) != 0) printf ("|START " ) ; else printf (BLANK) ;
if ((buttons & NES_A) != 0) printf ("| A " ) ; else printf (BLANK) ;
if ((buttons & NES_B) != 0) printf ("| B " ) ; else printf (BLANK) ;
printf ("|\n") ;
}
return 0 ;
}

82
examples/okLed.c Normal file
View File

@@ -0,0 +1,82 @@
/*
* okLed.c:
* Make the OK LED on the Pi Pulsate...
*
* Originally posted to the Raspberry Pi forums:
* http://www.raspberrypi.org/phpBB3/viewtopic.php?p=162581#p162581
*
* Compile this and store it somewhere, then kick it off at boot time
* e.g. by putting it in /etc/rc.local and running it in the
* background &
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <wiringPi.h>
#include <softPwm.h>
// The OK/Act LED is connected to BCM_GPIO pin 16
#define OK_LED 16
int main ()
{
int fd, i ;
wiringPiSetupGpio () ;
// Change the trigger on the OK/Act LED to "none"
if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0)
{
fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ;
return 1 ;
}
write (fd, "none\n", 5) ;
close (fd) ;
softPwmCreate (OK_LED, 0, 100) ;
for (;;)
{
for (i = 0 ; i <= 100 ; ++i)
{
softPwmWrite (OK_LED, i) ;
delay (10) ;
}
delay (50) ;
for (i = 100 ; i >= 0 ; --i)
{
softPwmWrite (OK_LED, i) ;
delay (10) ;
}
delay (10) ;
}
return 0 ;
}

100
examples/oled_demo.c Normal file
View File

@@ -0,0 +1,100 @@
/*
* Copyright (c) 2015, Vladimir Komendantskiy
* MIT License
*
* SSD1306 demo of block and font drawing.
*/
//
// fixed for OrangePiZero by HypHop
//
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include "oled.h"
#include "font.h"
int oled_demo(struct display_info *disp) {
int i;
char buf[100];
//putstrto(disp, 0, 0, "Spnd spd 2468 rpm");
// oled_putstrto(disp, 0, 9+1, "Spnd cur 0.46 A");
oled_putstrto(disp, 0, 9+1, "Welcome to");
disp->font = font1;
// oled_putstrto(disp, 0, 18+2, "Spnd tmp 53 C");
oled_putstrto(disp, 0, 18+2, "----OrangePi----");
disp->font = font2;
// oled_putstrto(disp, 0, 27+3, "DrvX tmp 64 C");
oled_putstrto(disp, 0, 27+3, "This is 0.96OLED");
oled_putstrto(disp, 0, 36+4, "");
oled_putstrto(disp, 0, 45+5, "");
disp->font = font1;
// oled_putstrto(disp, 0, 54, "Total cur 2.36 A");
oled_putstrto(disp, 0, 54, "*****************");
oled_send_buffer(disp);
disp->font = font3;
for (i=0; i<100; i++) {
sprintf(buf, "Spnd spd %d rpm", i);
oled_putstrto(disp, 0, 0, buf);
oled_putstrto(disp, 135-i, 36+4, "===");
oled_putstrto(disp, 100, 0+i/2, ".");
oled_send_buffer(disp);
}
//oled_putpixel(disp, 60, 45);
//oled_putstr(disp, 1, "hello");
return 0;
}
void show_error(int err, int add) {
//const gchar* errmsg;
//errmsg = g_strerror(errno);
printf("\nERROR: %i, %i\n\n", err, add);
//printf("\nERROR\n");
}
void show_usage(char *progname) {
printf("\nUsage:\n%s <I2C bus device node >\n", progname);
}
int main(int argc, char **argv) {
int e;
char filename[32];
struct display_info disp;
if (argc < 2) {
show_usage(argv[0]);
return -1;
}
memset(&disp, 0, sizeof(disp));
sprintf(filename, "%s", argv[1]);
disp.address = OLED_I2C_ADDR;
disp.font = font2;
e = oled_open(&disp, filename);
if (e < 0) {
show_error(1, e);
} else {
e = oled_init(&disp);
if (e < 0) {
show_error(2, e);
} else {
printf("---------start--------\n");
if (oled_demo(&disp) < 0)
show_error(3, 777);
printf("----------end---------\n");
}
}
return 0;
}

44
examples/pwm.c Normal file
View File

@@ -0,0 +1,44 @@
/*
* pwm.c:
* This tests the hardware PWM channel.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define PWM_PIN 29
int main (void)
{
printf ("OrangePi Pi wiringPi PWM test program\n") ;
if (wiringPiSetup () == -1)
exit (1) ;
pinMode (PWM_PIN, PWM_OUTPUT) ;
pwmWrite (PWM_PIN, 500) ;
return 0 ;
}

84
examples/q2w/Makefile Normal file
View File

@@ -0,0 +1,84 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://projects.drogon.net/wiring-pi
#
# Copyright (c) 2012-2013 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
LDFLAGS = -L/usr/local/lib
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm
###############################################################################
SRC = blink.c button.c blink-io.c volts.c bright.c
OBJ = $(SRC:.c=.o)
BINS = $(SRC:.c=)
all: $(BINS)
blink: blink.o
$Q echo [link]
$Q $(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
blink-io: blink-io.o
$Q echo [link]
$Q $(CC) -o $@ blink-io.o $(LDFLAGS) $(LDLIBS)
button: button.o
$Q echo [link]
$Q $(CC) -o $@ button.o $(LDFLAGS) $(LDLIBS)
volts: volts.o
$Q echo [link]
$Q $(CC) -o $@ volts.o $(LDFLAGS) $(LDLIBS)
bright: bright.o
$Q echo [link]
$Q $(CC) -o $@ bright.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)
depend:
makedepend -Y $(SRC)
# DO NOT DELETE

79
examples/q2w/binary.c Normal file
View File

@@ -0,0 +1,79 @@
/*
* binary.c:
* Using the Quick 2 wire 16-bit GPIO expansion board to output
* a binary counter.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <mcp23017.h>
#define Q2W_BASE 100
int main (void)
{
int i, bit ;
// Enable the on-goard GPIO
wiringPiSetup () ;
// Add in the mcp23017 on the q2w board
mcp23017Setup (Q2W_BASE, 0x20) ;
printf ("Raspberry Pi - quite2Wire MCP23017 Test\n") ;
// On-board button Input:
pinMode (0, INPUT) ;
// First 10 pins on q2w board as outputs:
for (i = 0 ; i < 10 ; ++i)
pinMode (Q2W_BASE + i, OUTPUT) ;
// Last pin as an input with the internal pull-up enabled
pinMode (Q2W_BASE + 15, INPUT) ;
pullUpDnControl (Q2W_BASE + 15, PUD_UP) ;
// Loop, outputting a binary number,
// Go faster with the button, or stop if the
// on-board button is pushed
for (;;)
{
for (i = 0 ; i < 1024 ; ++i)
{
for (bit = 0 ; bit < 10 ; ++bit)
digitalWrite (Q2W_BASE + bit, i & (1 << bit)) ;
while (digitalRead (0) == HIGH) // While pushed
delay (1) ;
if (digitalRead (Q2W_BASE + 15) == HIGH) // Not Pushed
delay (100) ;
}
}
return 0 ;
}

61
examples/q2w/blink-io.c Normal file
View File

@@ -0,0 +1,61 @@
/*
* blink-io.c:
* Simple "blink" test for the Quick2Wire 16-pin IO board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <mcp23017.h>
#define LED 1
#define Q2W_BASE 100
int main (void)
{
// Enable the on-goard GPIO
wiringPiSetup () ;
// Add in the mcp23017 on the q2w board
mcp23017Setup (Q2W_BASE, 0x20) ;
printf ("Raspberry Pi - Quick2Wire MCP23017 Blink Test\n") ;
// Blink the on-board LED as well as one on the mcp23017
pinMode (LED, OUTPUT) ;
pinMode (Q2W_BASE + 0, OUTPUT) ;
for (;;)
{
digitalWrite (LED, HIGH) ;
digitalWrite (Q2W_BASE + 0, HIGH) ;
delay (500) ;
digitalWrite (LED, LOW) ;
digitalWrite (Q2W_BASE + 0, LOW) ;
delay (500) ;
}
return 0 ;
}

50
examples/q2w/blink.c Normal file
View File

@@ -0,0 +1,50 @@
/*
* blink.c:
* Simple "blink" test for the Quick2Wire interface board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#define LED 1
int main (void)
{
// Enable the on-goard GPIO
wiringPiSetup () ;
printf ("Raspberry Pi - Quick2Wire Mainboard LED Blink Test\n") ;
pinMode (LED, OUTPUT) ;
for (;;)
{
digitalWrite (LED, HIGH) ;
delay (500) ;
digitalWrite (LED, LOW) ;
delay (500) ;
}
return 0 ;
}

37
examples/q2w/blink.sh Normal file
View File

@@ -0,0 +1,37 @@
#!/bin/sh -e
#
# blink.sh:
# Standard "blink" program in wiringPi. Blinks an LED connected
# to the LED on the Quick2Wire board
#
# Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
#######################################################################
# 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 <http://www.gnu.org/licenses/>.
#######################################################################
# LED Pin - wiringPi pin 1 is BCM_GPIO 18.
LED=1
gpio mode $LED out
while true; do
gpio write $LED 1
sleep 0.5
gpio write $LED 0
sleep 0.5
done

59
examples/q2w/bright.c Normal file
View File

@@ -0,0 +1,59 @@
/*
* bright.c:
* Vary the Q2W LED brightness with the analog card
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#define LED 1
#define Q2W_ABASE 120
int main (void)
{
int value ;
// Enable the on-goard GPIO
wiringPiSetup () ;
// Add in the pcf8591 on the q2w board
pcf8591Setup (Q2W_ABASE, 0x48) ;
printf ("Raspberry Pi - Quick2Wire Analog Test\n") ;
// Setup the LED
pinMode (LED, PWM_OUTPUT) ;
pwmWrite (LED, 0) ;
for (;;)
{
value = analogRead (Q2W_ABASE + 0) ;
pwmWrite (LED, value * 4) ;
delay (10) ;
}
return 0 ;
}

63
examples/q2w/button.c Normal file
View File

@@ -0,0 +1,63 @@
/*
* button.c:
* Simple button test for the Quick2Wire interface board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#define BUTTON 0
#define LED1 1
#define LED2 7
int main (void)
{
// Enable the on-goard GPIO
wiringPiSetup () ;
printf ("Raspberry Pi - Quick2Wire Mainboard Button & LED Test\n") ;
pinMode (BUTTON, INPUT) ;
pinMode (LED1, OUTPUT) ;
pinMode (LED2, OUTPUT) ;
digitalWrite (LED1, HIGH) ; // On-board LED on
digitalWrite (LED2, LOW) ; // 2nd LED off
for (;;)
{
if (digitalRead (BUTTON) == HIGH) // Swap LED states
{
digitalWrite (LED1, LOW) ;
digitalWrite (LED2, HIGH) ;
while (digitalRead (BUTTON) == HIGH)
delay (1) ;
digitalWrite (LED1, HIGH) ;
digitalWrite (LED2, LOW) ;
}
delay (1) ;
}
return 0 ;
}

62
examples/q2w/volts.c Normal file
View File

@@ -0,0 +1,62 @@
/*
* volts.c:
* Read in all 4 analogs on the Q2W analog board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#define LED 1
#define Q2W_ABASE 120
int main (void)
{
int value, pin ;
// Enable the on-goard GPIO
wiringPiSetup () ;
pinMode (LED, OUTPUT) ; // On-board LED
// Add in the pcf8591 on the q2w board
pcf8591Setup (Q2W_ABASE, 0x48) ;
printf ("Raspberry Pi - Quick2Wire Voltmeter\n") ;
for (;;)
{
for (pin = 0 ; pin < 4 ; ++pin)
{
value = analogRead (Q2W_ABASE + pin) ;
printf (" %5.2f", (double)value * 3.3 / 255.0) ;
}
printf ("\r") ; fflush (stdout) ;
delay (100) ;
digitalWrite (LED, !digitalRead (LED)) ; // Flicker the LED
}
return 0 ;
}

86
examples/rht03.c Normal file
View File

@@ -0,0 +1,86 @@
/*
* rht03.c:
* Driver for the MaxDetect series sensors
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <maxdetect.h>
#define RHT03_PIN 7
/*
***********************************************************************
* The main program
***********************************************************************
*/
int main (void)
{
int result, temp, rh ;
int minT, maxT, minRH, maxRH ;
int numGood, numBad ;
wiringPiSetup () ;
piHiPri (55) ;
minT = 1000 ;
maxT = -1000 ;
minRH = 1000 ;
maxRH = -1000 ;
numGood = numBad = 0 ;
for (;;)
{
delay (100) ;
result = readRHT03 (RHT03_PIN, &temp, &rh) ;
if (!result)
{
printf (".") ;
fflush (stdout) ;
++numBad ;
continue ;
}
++numGood ;
if (temp < minT) minT = temp ;
if (temp > maxT) maxT = temp ;
if (rh < minRH) minRH = rh ;
if (rh > maxRH) maxRH = rh ;
printf ("\r%6d, %6d: ", numGood, numBad) ;
printf ("Temp: %5.1f, RH: %5.1f%%", temp / 10.0, rh / 10.0) ;
printf (" Max/Min Temp: %5.1f:%5.1f", maxT / 10.0, minT / 10.0) ;
printf (" Max/Min RH: %5.1f:%5.1f", maxRH / 10.0, minRH / 10.0) ;
printf ("\n") ;
}
return 0 ;
}

View File

@@ -0,0 +1,79 @@
#
# Makefile:
# wiringPi - A "wiring" library for the Raspberry Pi
# https://projects.drogon.net/wiring-pi
#
# 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
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

View File

@@ -0,0 +1,230 @@
/*
* scphat.c:
* Little program to allow use of the Pimoroni Sctoll Phat
* from the command-line.
*
* Copyright (c) 2015-2016 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <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 ;
}

115
examples/scrollPhat/test.c Normal file
View File

@@ -0,0 +1,115 @@
/*
* test.c:
* Little test program forthe Pimoroni Scroll Phat.
*
* Copyright (c) 2015-2016 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <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 ;
}

48
examples/serialRead.c Normal file
View File

@@ -0,0 +1,48 @@
/*
* serial.c:
* Example program to read bytes from the Serial line
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
int main ()
{
int fd ;
if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
// Loop, getting and printing characters
for (;;)
{
putchar (serialGetchar (fd)) ;
fflush (stdout) ;
}
}

75
examples/serialTest.c Normal file
View File

@@ -0,0 +1,75 @@
/*
* serialTest.c:
* Very simple program to test the serial port. Expects
* the port to be looped back to itself
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main ()
{
int fd ;
int count ;
unsigned int nextTime ;
if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
nextTime = millis () + 300 ;
for (count = 0 ; count < 256 ; )
{
if (millis () > nextTime)
{
printf ("\nOut: %3d: ", count) ;
fflush (stdout) ;
serialPutchar (fd, count) ;
nextTime += 300 ;
++count ;
}
delay (3) ;
while (serialDataAvail (fd))
{
printf (" -> %3d", serialGetchar (fd)) ;
fflush (stdout) ;
}
}
printf ("\n") ;
return 0 ;
}

57
examples/servo.c Normal file
View File

@@ -0,0 +1,57 @@
/*
* servo.c:
* Test of the softServo code.
* Do not use this code - use the servoBlaster kernel module instead
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softServo.h>
int main ()
{
if (wiringPiSetup () == -1)
{
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
return 1 ;
}
softServoSetup (0, 1, 2, 3, 4, 5, 6, 7) ;
softServoWrite (0, 0) ;
/*
softServoWrite (1, 1000) ;
softServoWrite (2, 1100) ;
softServoWrite (3, 1200) ;
softServoWrite (4, 1300) ;
softServoWrite (5, 1400) ;
softServoWrite (6, 1500) ;
softServoWrite (7, 2200) ;
*/
for (;;)
delay (10) ;
}

89
examples/softPwm.c Normal file
View File

@@ -0,0 +1,89 @@
/*
* softPwm.c:
* Test of the software PWM driver. Needs 8 LEDs connected
* to the Pi - e.g. Ladder board.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softPwm.h>
#define RANGE 100
#define NUM_LEDS 8
int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7 } ;
int values [NUM_LEDS] = { 0, 25, 50, 75, 100, 75, 50, 25 } ;
int main ()
{
int i, j ;
char buf [80] ;
wiringPiSetup () ;
for (i = 0 ; i < NUM_LEDS ; ++i)
{
softPwmCreate (ledMap [i], 0, RANGE) ;
printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ;
}
fgets (buf, 80, stdin) ;
// Bring all up one by one:
for (i = 0 ; i < NUM_LEDS ; ++i)
for (j = 0 ; j <= 100 ; ++j)
{
softPwmWrite (ledMap [i], j) ;
delay (10) ;
}
fgets (buf, 80, stdin) ;
// All Down
for (i = 100 ; i > 0 ; --i)
{
for (j = 0 ; j < NUM_LEDS ; ++j)
softPwmWrite (ledMap [j], i) ;
delay (10) ;
}
fgets (buf, 80, stdin) ;
for (;;)
{
for (i = 0 ; i < NUM_LEDS ; ++i)
softPwmWrite (ledMap [i], values [i]) ;
delay (50) ;
i = values [0] ;
for (j = 0 ; j < NUM_LEDS - 1 ; ++j)
values [j] = values [j + 1] ;
values [NUM_LEDS - 1] = i ;
}
}

54
examples/softTone.c Normal file
View File

@@ -0,0 +1,54 @@
/*
* softTone.c:
* Test of the softTone module in wiringPi
* Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softTone.h>
#define PIN 3
int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ;
int main ()
{
int i ;
wiringPiSetup () ;
softToneCreate (PIN) ;
for (;;)
{
for (i = 0 ; i < 8 ; ++i)
{
printf ("%3d\n", i) ;
softToneWrite (PIN, scale [i]) ;
delay (500) ;
}
}
}

95
examples/speed.c Normal file
View File

@@ -0,0 +1,95 @@
/*
* speed.c:
* Simple program to measure the speed of the various GPIO
* access mechanisms.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define FAST_COUNT 10000000
#define SLOW_COUNT 1000000
#define PASSES 5
void speedTest (int pin, int maxCount)
{
int count, sum, perSec, i ;
unsigned int start, end ;
sum = 0 ;
for (i = 0 ; i < PASSES ; ++i)
{
start = millis () ;
for (count = 0 ; count < maxCount ; ++count)
digitalWrite (pin, 1) ;
end = millis () ;
printf (" %6d", end - start) ;
fflush (stdout) ;
sum += (end - start) ;
}
digitalWrite (pin, 0) ;
printf (". Av: %6dmS", sum / PASSES) ;
perSec = (int)(double)maxCount / (double)((double)sum / (double)PASSES) * 1000.0 ;
printf (": %7d/sec\n", perSec) ;
}
int main (void)
{
printf ("OrangePi Pi wiringPi GPIO speed test program\n") ;
printf ("=============================================\n") ;
// Start the standard way
printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
wiringPiSetup () ;
pinMode (0, OUTPUT) ;
speedTest (0, FAST_COUNT) ;
// GPIO
/* printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ;
wiringPiSetupGpio () ;
pinMode (17, OUTPUT) ;
speedTest (17, FAST_COUNT) ;*/
// Phys
printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ;
wiringPiSetupPhys () ;
pinMode (11, OUTPUT) ;
speedTest (11, FAST_COUNT) ;
// Switch to SYS mode:
system ("/usr/local/bin/gpio export 17 out") ;
printf ("\n/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
wiringPiSetupSys () ;
speedTest (17, SLOW_COUNT) ;
return 0 ;
}

118
examples/spiSpeed.c Normal file
View File

@@ -0,0 +1,118 @@
/*
* spiSpeed.c:
* Code to measure the SPI speed/latency.
* Copyright (c) 2014 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
//#include <fcntl.h>
//#include <sys/ioctl.h>
//#include <linux/spi/spidev.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define TRUE (1==1)
#define FALSE (!TRUE)
#define SPI_CHAN 0
#define NUM_TIMES 100
#define MAX_SIZE (1024*1024)
static int myFd ;
void spiSetup (int speed)
{
if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0)
{
fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ;
exit (EXIT_FAILURE) ;
}
}
int main (void)
{
int speed, times, size ;
unsigned int start, end ;
int spiFail ;
unsigned char *myData ;
double timePerTransaction, perfectTimePerTransaction, dataSpeed ;
if ((myData = malloc (MAX_SIZE)) == NULL)
{
fprintf (stderr, "Unable to allocate buffer: %s\n", strerror (errno)) ;
exit (EXIT_FAILURE) ;
}
wiringPiSetup () ;
for (speed = 1 ; speed <= 32 ; speed *= 2)
{
printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ;
printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
spiFail = FALSE ;
spiSetup (speed * 1000000) ;
for (size = 1 ; size <= MAX_SIZE ; size *= 2)
{
printf ("| %5d | %6d ", speed, size) ;
start = millis () ;
for (times = 0 ; times < NUM_TIMES ; ++times)
if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1)
{
printf ("SPI failure: %s\n", strerror (errno)) ;
spiFail = TRUE ;
break ;
}
end = millis () ;
if (spiFail)
break ;
timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ;
dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ;
perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ;
printf ("| %8.3f ", timePerTransaction * 1000.0) ;
printf ("| %8.1f ", 1.0 / timePerTransaction) ;
printf ("| %9.5f ", dataSpeed) ;
printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ;
printf ("|\n") ;
}
close (myFd) ;
printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
printf ("\n") ;
}
return 0 ;
}

480
examples/spidev_test.c Normal file
View File

@@ -0,0 +1,480 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* SPI testing utility (using spidev driver)
*
* Copyright (c) 2007 MontaVista Software, Inc.
* Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
*
* Cross-compile with cross-gcc -I/path/to/cross-kernel/include
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static void pabort(const char *s)
{
perror(s);
abort();
}
static const char *device = "/dev/spidev1.1";
static uint32_t mode;
static uint8_t bits = 8;
static char *input_file;
static char *output_file;
static uint32_t speed = 500000;
static uint16_t delay;
static int verbose;
static int transfer_size;
static int iterations;
static int interval = 5; /* interval in seconds for showing transfer rate */
uint8_t default_tx[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0x0D,
};
uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
char *input_tx;
static void hex_dump(const void *src, size_t length, size_t line_size,
char *prefix)
{
int i = 0;
const unsigned char *address = src;
const unsigned char *line = address;
unsigned char c;
printf("%s | ", prefix);
while (length-- > 0) {
printf("%02X ", *address++);
if (!(++i % line_size) || (length == 0 && i % line_size)) {
if (length == 0) {
while (i++ % line_size)
printf("__ ");
}
printf(" |");
while (line < address) {
c = *line++;
printf("%c", (c < 32 || c > 126) ? '.' : c);
}
printf("|\n");
if (length > 0)
printf("%s | ", prefix);
}
}
}
/*
* Unescape - process hexadecimal escape character
* converts shell input "\x23" -> 0x23
*/
static int unescape(char *_dst, char *_src, size_t len)
{
int ret = 0;
int match;
char *src = _src;
char *dst = _dst;
unsigned int ch;
while (*src) {
if (*src == '\\' && *(src+1) == 'x') {
match = sscanf(src + 2, "%2x", &ch);
if (!match)
pabort("malformed input string");
src += 4;
*dst++ = (unsigned char)ch;
} else {
*dst++ = *src++;
}
ret++;
}
return ret;
}
static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
{
int ret;
int out_fd;
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = len,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
if (mode & SPI_TX_QUAD)
tr.tx_nbits = 4;
else if (mode & SPI_TX_DUAL)
tr.tx_nbits = 2;
if (mode & SPI_RX_QUAD)
tr.rx_nbits = 4;
else if (mode & SPI_RX_DUAL)
tr.rx_nbits = 2;
if (!(mode & SPI_LOOP)) {
if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
tr.rx_buf = 0;
else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
tr.tx_buf = 0;
}
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
pabort("can't send spi message");
if (verbose)
hex_dump(tx, len, 32, "TX");
if (output_file) {
out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (out_fd < 0)
pabort("could not open output file");
ret = write(out_fd, rx, len);
if (ret != len)
pabort("not all bytes written to output file");
close(out_fd);
}
if (verbose)
hex_dump(rx, len, 32, "RX");
}
static void print_usage(const char *prog)
{
printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
puts(" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
" -d --delay delay (usec)\n"
" -b --bpw bits per word\n"
" -i --input input data from a file (e.g. \"test.bin\")\n"
" -o --output output data to a file (e.g. \"results.bin\")\n"
" -l --loop loopback\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
" -L --lsb least significant bit first\n"
" -C --cs-high chip select active high\n"
" -3 --3wire SI/SO signals shared\n"
" -v --verbose Verbose (show tx buffer)\n"
" -p Send data (e.g. \"1234\\xde\\xad\")\n"
" -N --no-cs no chip select\n"
" -R --ready slave pulls low to pause\n"
" -2 --dual dual transfer\n"
" -4 --quad quad transfer\n"
" -S --size transfer size\n"
" -I --iter iterations\n");
exit(1);
}
static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = {
{ "device", 1, 0, 'D' },
{ "speed", 1, 0, 's' },
{ "delay", 1, 0, 'd' },
{ "bpw", 1, 0, 'b' },
{ "input", 1, 0, 'i' },
{ "output", 1, 0, 'o' },
{ "loop", 0, 0, 'l' },
{ "cpha", 0, 0, 'H' },
{ "cpol", 0, 0, 'O' },
{ "lsb", 0, 0, 'L' },
{ "cs-high", 0, 0, 'C' },
{ "3wire", 0, 0, '3' },
{ "no-cs", 0, 0, 'N' },
{ "ready", 0, 0, 'R' },
{ "dual", 0, 0, '2' },
{ "verbose", 0, 0, 'v' },
{ "quad", 0, 0, '4' },
{ "size", 1, 0, 'S' },
{ "iter", 1, 0, 'I' },
{ NULL, 0, 0, 0 },
};
int c;
c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
lopts, NULL);
if (c == -1)
break;
switch (c) {
case 'D':
device = optarg;
break;
case 's':
speed = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 'b':
bits = atoi(optarg);
break;
case 'i':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
case 'l':
mode |= SPI_LOOP;
break;
case 'H':
mode |= SPI_CPHA;
break;
case 'O':
mode |= SPI_CPOL;
break;
case 'L':
mode |= SPI_LSB_FIRST;
break;
case 'C':
mode |= SPI_CS_HIGH;
break;
case '3':
mode |= SPI_3WIRE;
break;
case 'N':
mode |= SPI_NO_CS;
break;
case 'v':
verbose = 1;
break;
case 'R':
mode |= SPI_READY;
break;
case 'p':
input_tx = optarg;
break;
case '2':
mode |= SPI_TX_DUAL;
break;
case '4':
mode |= SPI_TX_QUAD;
break;
case 'S':
transfer_size = atoi(optarg);
break;
case 'I':
iterations = atoi(optarg);
break;
default:
print_usage(argv[0]);
break;
}
}
if (mode & SPI_LOOP) {
if (mode & SPI_TX_DUAL)
mode |= SPI_RX_DUAL;
if (mode & SPI_TX_QUAD)
mode |= SPI_RX_QUAD;
}
}
static void transfer_escaped_string(int fd, char *str)
{
size_t size = strlen(str);
uint8_t *tx;
uint8_t *rx;
tx = malloc(size);
if (!tx)
pabort("can't allocate tx buffer");
rx = malloc(size);
if (!rx)
pabort("can't allocate rx buffer");
size = unescape((char *)tx, str, size);
transfer(fd, tx, rx, size);
free(rx);
free(tx);
}
static void transfer_file(int fd, char *filename)
{
ssize_t bytes;
struct stat sb;
int tx_fd;
uint8_t *tx;
uint8_t *rx;
if (stat(filename, &sb) == -1)
pabort("can't stat input file");
tx_fd = open(filename, O_RDONLY);
if (tx_fd < 0)
pabort("can't open input file");
tx = malloc(sb.st_size);
if (!tx)
pabort("can't allocate tx buffer");
rx = malloc(sb.st_size);
if (!rx)
pabort("can't allocate rx buffer");
bytes = read(tx_fd, tx, sb.st_size);
if (bytes != sb.st_size)
pabort("failed to read input file");
transfer(fd, tx, rx, sb.st_size);
free(rx);
free(tx);
close(tx_fd);
}
static uint64_t _read_count;
static uint64_t _write_count;
static void show_transfer_rate(void)
{
static uint64_t prev_read_count, prev_write_count;
double rx_rate, tx_rate;
rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
prev_read_count = _read_count;
prev_write_count = _write_count;
}
static void transfer_buf(int fd, int len)
{
uint8_t *tx;
uint8_t *rx;
int i;
tx = malloc(len);
if (!tx)
pabort("can't allocate tx buffer");
for (i = 0; i < len; i++)
tx[i] = random();
rx = malloc(len);
if (!rx)
pabort("can't allocate rx buffer");
transfer(fd, tx, rx, len);
_write_count += len;
_read_count += len;
if (mode & SPI_LOOP) {
if (memcmp(tx, rx, len)) {
fprintf(stderr, "transfer error !\n");
hex_dump(tx, len, 32, "TX");
hex_dump(rx, len, 32, "RX");
exit(1);
}
}
free(rx);
free(tx);
}
int main(int argc, char *argv[])
{
int ret = 0;
int fd;
parse_opts(argc, argv);
fd = open(device, O_RDWR);
if (fd < 0)
pabort("can't open device");
/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
if (ret == -1)
pabort("can't get spi mode");
/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
printf("spi mode: 0x%x\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
if (input_tx && input_file)
pabort("only one of -p and --input may be selected");
if (input_tx)
transfer_escaped_string(fd, input_tx);
else if (input_file)
transfer_file(fd, input_file);
else if (transfer_size) {
struct timespec last_stat;
clock_gettime(CLOCK_MONOTONIC, &last_stat);
while (iterations-- > 0) {
struct timespec current;
transfer_buf(fd, transfer_size);
clock_gettime(CLOCK_MONOTONIC, &current);
if (current.tv_sec - last_stat.tv_sec > interval) {
show_transfer_rate();
last_stat = current;
}
}
printf("total: tx %.1fKB, rx %.1fKB\n",
_write_count/1024.0, _read_count/1024.0);
} else
transfer(fd, default_tx, default_rx, sizeof(default_tx));
close(fd);
return ret;
}

View File

@@ -0,0 +1,210 @@
/*
* SPI testing utility (using spidev driver)
*
* Copyright (c) 2007 MontaVista Software, Inc.
* Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* Cross-compile with cross-gcc -I/path/to/cross-kernel/include
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static void pabort(const char *s)
{
perror(s);
abort();
}
static const char *device = "/dev/spidev1.1";
static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;
static void transfer(int fd)
{
int ret;
uint8_t tx[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
0xF0, 0x0D,
};
uint8_t rx[ARRAY_SIZE(tx)] = {0, };
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
pabort("can't send spi message");
for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
if (!(ret % 6))
puts("");
printf("%.2X ", rx[ret]);
}
puts("");
}
static void print_usage(const char *prog)
{
printf("Usage: %s [-DsbdlHOLC3]\n", prog);
puts(" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
" -d --delay delay (usec)\n"
" -b --bpw bits per word \n"
" -l --loop loopback\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
" -L --lsb least significant bit first\n"
" -C --cs-high chip select active high\n"
" -3 --3wire SI/SO signals shared\n");
exit(1);
}
static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = {
{ "device", 1, 0, 'D' },
{ "speed", 1, 0, 's' },
{ "delay", 1, 0, 'd' },
{ "bpw", 1, 0, 'b' },
{ "loop", 0, 0, 'l' },
{ "cpha", 0, 0, 'H' },
{ "cpol", 0, 0, 'O' },
{ "lsb", 0, 0, 'L' },
{ "cs-high", 0, 0, 'C' },
{ "3wire", 0, 0, '3' },
{ "no-cs", 0, 0, 'N' },
{ "ready", 0, 0, 'R' },
{ NULL, 0, 0, 0 },
};
int c;
c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
if (c == -1)
break;
switch (c) {
case 'D':
device = optarg;
break;
case 's':
speed = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 'b':
bits = atoi(optarg);
break;
case 'l':
mode |= SPI_LOOP;
break;
case 'H':
mode |= SPI_CPHA;
break;
case 'O':
mode |= SPI_CPOL;
break;
case 'L':
mode |= SPI_LSB_FIRST;
break;
case 'C':
mode |= SPI_CS_HIGH;
break;
case '3':
mode |= SPI_3WIRE;
break;
case 'N':
mode |= SPI_NO_CS;
break;
case 'R':
mode |= SPI_READY;
break;
default:
print_usage(argv[0]);
break;
}
}
}
int main(int argc, char *argv[])
{
int ret = 0;
int fd;
parse_opts(argc, argv);
fd = open(device, O_RDWR);
if (fd < 0)
pabort("can't open device");
/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1)
pabort("can't get spi mode");
/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
printf("spi mode: %d\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
transfer(fd);
close(fd);
return ret;
}

139
examples/w25q64_test.c Normal file
View File

@@ -0,0 +1,139 @@
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "w25q64.h"
#define SPI_CHANNEL 0 // /dev/spidevX.0
//#define SPI_CHANNEL 1 // /dev/spidevX.0
#define SPI_PORT 0
//#define SPI_PORT 1
void dump(uint8_t *dt, uint32_t n) {
uint32_t sz;
char buf[64];
uint16_t clm = 0;
uint8_t data;
uint8_t sum;
uint8_t vsum[16];
uint8_t total =0;
uint32_t saddr =0;
uint32_t eaddr =n-1;
sz = eaddr -saddr;
printf("----------------------------------------------------------\n");
uint16_t i;
for (i=0;i<16;i++) vsum[i]=0;
uint32_t addr;
for (addr = saddr; addr <= eaddr; addr++) {
data = dt[addr];
if (clm == 0) {
sum =0;
printf("%05x: ",addr);
}
sum+=data;
vsum[addr % 16]+=data;
printf("%02x ",data);
clm++;
if (clm == 16) {
printf("|%02x \n",sum);
clm = 0;
}
}
printf("----------------------------------------------------------\n");
printf(" ");
for (i=0; i<16;i++) {
total+=vsum[i];
printf("%02x ",vsum[i]);
}
printf("|%02x \n\n",total);
}
void main() {
uint8_t buf[256];
uint8_t wdata[16];
uint8_t i;
uint16_t n;
// Start SPI channel 0 with 2MHz
//if (wiringPiSPISetup(SPI_CHANNEL, 2000000) < 0) {
if (wiringPiSPISetupMode(SPI_CHANNEL, SPI_PORT, 2000000, 0) < 0) {
printf("SPISetup failed:\n");
}
// Start Flush Memory
W25Q64_begin(SPI_CHANNEL);
// JEDEC ID Get
W25Q64_readManufacturer(buf);
printf("JEDEC ID : ");
for (i=0; i< 3; i++) {
printf("%x ",buf[i]);
}
printf("\n");
// Unique ID Get
W25Q64_readUniqieID(buf);
printf("Unique ID : ");
for (i=0; i< 7; i++) {
printf("%x ",buf[i]);
}
printf("\n");
// Read 256 byte data from Address=0
memset(buf,0,256);
n = W25Q64_read(0, buf, 256);
printf("Read Data: n=%d\n",n);
dump(buf,256);
// First read 256 byte data from Address=0
memset(buf,0,256);
n = W25Q64_fastread(0, buf, 256);
printf("Fast Read Data: n=%d\n",n);
dump(buf,256);
// Erase data by Sector
n = W25Q64_eraseSector(0,true);
printf("Erase Sector(0): n=%d\n",n);
memset(buf,0,256);
n = W25Q64_read (0, buf, 256);
dump(buf,256);
// Write data to Sector=0 Address=10
for (i=0; i < 26;i++) {
wdata[i]='A'+i; // A-Z
}
n = W25Q64_pageWrite(0, 10, wdata, 26);
printf("page_write(0,10,d,26): n=%d\n",n);
// Read 256 byte data from Address=0
memset(buf,0,256);
n = W25Q64_read(0, buf, 256);
printf("Read Data: n=%d\n",n);
dump(buf,256);
// Write data to Sector=0 Address=0
for (i=0; i < 10;i++) {
wdata[i]='0'+i; // 0-9
}
n = W25Q64_pageWrite(0, 0, wdata, 10);
printf("page_write(0,0,d,10): n=%d\n",n);
// First read 256 byte data from Address=0
memset(buf,0,256);
n = W25Q64_fastread(0,buf, 256);
printf("Fast Read Data: n=%d\n",n);
dump(buf,256);
// Get fron Status Register1
buf[0] = W25Q64_readStatusReg1();
printf("Status Register-1: %x\n",buf[0]);
// Get fron Status Register2
buf[0] = W25Q64_readStatusReg2();
printf("Status Register-2: %x\n",buf[0]);
}

115
examples/watchdog.c Normal file
View File

@@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <sys/signal.h>
#include <termios.h>
struct watchdog_info{
unsigned int options; //options the card/driver supprots 19
unsigned int firmware_version; //firmcard version of the card
unsigned char identity[32]; //identity of the board 21
};
#define WATCHDOG_IOCTL_BASE 'W'
#define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)
#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)
#define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int) //27
#define WDIOS_DISABLECARD 0x0001
#define WDIOS_ENABLECARD 0x0002
#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)
#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)
int Getch (void) //无回显的从屏幕输入字符,来达到喂狗的目的
{
int ch;
struct termios oldt, newt; //终端设备结构体
tcgetattr(STDIN_FILENO, &oldt); //获得终端属性
newt = oldt;
newt.c_lflag &= ~(ECHO|ICANON); //设置无回显属性
tcsetattr(STDIN_FILENO, TCSANOW, &newt); //设置新的终端属性
ch = getchar(); //从键盘输入一个数据
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); //恢复终端设备初始设置
return ch;
}
//suspend some seconds
int zsleep(int millisecond)
{
unsigned long usec;
usec=1000*millisecond;
usleep(usec); //usleep(1)睡眠一微秒10E-6),这里也就是0.1s
}
int Init()
{
int fd;
//open device file
fd = open("/dev/watchdog",O_RDWR); //打开看门狗设备
if(fd < 0)
{
printf("device open fail\n");
return -1;
}
printf("open success\n");
return fd;
}
int main(int argc,char **argv)
{
int fd,ch;
int i,j;
char c;
struct watchdog_info wi;
if(argc != 2){
printf("Usage : ./watchdog 10\n");
return -1;
}
fd=Init(); //打开终端看门狗设备
ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
//读板卡信息,但不常用
ioctl(fd,WDIOC_GETSUPPORT,&wi);
printf("options is %d,identity is %s\n",wi.options,wi.identity);
//读看门狗溢出时间
printf("put_usr return,if 0,success:%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i));
printf("The old reset time is: %d\n", i);
//关闭
i=WDIOS_DISABLECARD;//WDIOC_SETOPTIONS=0X0001
printf("return ENOTTY,if -1,success:%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));
//打开
i=WDIOS_ENABLECARD;//WDIOS_ENABLECARD 0x0002
printf("return ENOTTY,if -1,success:%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));
i=atoi(argv[1]);
printf("put_user return,if 0,success:%d\n",ioctl(fd,WDIOC_SETTIMEOUT,&i));
//读新的设置时间
printf("put_usr return,if 0,success:%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i));
while(1)
{
zsleep(100);
if((c=Getch())!=27){
//输入如果不是ESC就喂狗否则不喂狗到时间后系统重启
printf("keep alive \n");
ioctl(fd,WDIOC_KEEPALIVE,NULL);
//write(fd,NULL,1); //同样是喂狗
}
}
close(fd); //关闭设备
return 0;
}

161
examples/wfi.c Normal file
View File

@@ -0,0 +1,161 @@
/*
* wfi.c:
* Wait for Interrupt test program
*
* This program demonstrates the use of the waitForInterrupt()
* function in wiringPi. It listens to a button input on
* BCM_GPIO pin 17 (wiringPi pin 0)
*
* The biggest issue with this method is that it really only works
* well in Sys mode.
*
* Jan 2013: This way of doing things is sort of deprecated now, see
* the wiringPiISR() function instead and the isr.c test program here.
*
* Copyright (c) 2012-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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
// A 'key' which we can lock and unlock - values are 0 through 3
// This is interpreted internally as a pthread_mutex by wiringPi
// which is hiding some of that to make life simple.
#define COUNT_KEY 0
// What BCM_GPIO input are we using?
#define BUTTON_PIN 17
// Debounce time in mS
#define DEBOUNCE_TIME 100
// globalCounter:
// Global variable to count interrupts
// Should be declared volatile to make sure the compiler doesn't cache it.
static volatile int globalCounter = 0 ;
/*
* waitForIt:
* This is a thread created using the wiringPi simplified threading
* mechanism. It will wait on an interrupt on the button and increment
* a counter.
*********************************************************************************
*/
PI_THREAD (waitForIt)
{
int state = 0 ;
int debounceTime = 0 ;
(void)piHiPri (10) ; // Set this thread to be high priority
for (;;)
{
if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
{
// Bouncing?
if (millis () < debounceTime)
{
debounceTime = millis () + DEBOUNCE_TIME ;
continue ;
}
// We have a valid one
state ^= 1 ;
piLock (COUNT_KEY) ;
++globalCounter ;
piUnlock (COUNT_KEY) ;
// Wait for key to be released
while (digitalRead (BUTTON_PIN) == LOW)
delay (1) ;
debounceTime = millis () + DEBOUNCE_TIME ;
}
}
}
/*
* setup:
* Demo a crude but effective way to initialise the hardware
*********************************************************************************
*/
void setup (void)
{
// Use the gpio program to initialise the hardware
// (This is the crude, but effective)
system ("gpio edge 17 falling") ;
// Setup wiringPi
wiringPiSetupSys () ;
// Fire off our interrupt handler
piThreadCreate (waitForIt) ;
}
/*
* main
*********************************************************************************
*/
int main (void)
{
int lastCounter = 0 ;
int myCounter = 0 ;
setup () ;
for (;;)
{
printf ("Waiting ... ") ; fflush (stdout) ;
while (myCounter == lastCounter)
{
piLock (COUNT_KEY) ;
myCounter = globalCounter ;
piUnlock (COUNT_KEY) ;
delay (500) ;
}
printf (" Done. myCounter: %5d\n", myCounter) ;
lastCounter = myCounter ;
}
return 0 ;
}