mirror of
git://soft.sys114.com/WiringPi2-Python
synced 2026-02-04 15:30:35 +09:00
Merged
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ build/
|
||||
wiringpi2.egg-info/
|
||||
dist/
|
||||
__pycache__
|
||||
*.pyc
|
||||
|
||||
8
Makefile
Normal file
8
Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
all: bindings
|
||||
python setup.py build
|
||||
|
||||
bindings:
|
||||
swig2.0 -python wiringpi.i
|
||||
|
||||
install:
|
||||
sudo python setup.py install
|
||||
6
README
6
README
@@ -31,11 +31,11 @@ Class-based Usage:
|
||||
|
||||
Usage:
|
||||
import wiringpi2
|
||||
wiringpi2.wiringPiSetup // For sequential pin numbering, one of these MUST be called before using IO functions
|
||||
wiringpi2.wiringPiSetup() // For sequential pin numbering, one of these MUST be called before using IO functions
|
||||
OR
|
||||
wiringpi2.wiringPiSetupSys // For /sys/class/gpio with GPIO pin numbering
|
||||
wiringpi2.wiringPiSetupSys() // For /sys/class/gpio with GPIO pin numbering
|
||||
OR
|
||||
wiringpi2.wiringPiSetupGpio // For GPIO pin numbering
|
||||
wiringpi2.wiringPiSetupGpio() // For GPIO pin numbering
|
||||
|
||||
Setting up IO expanders (This example was tested on a quick2wire board with one digital IO expansion board connected via I2C):
|
||||
wiringpi2.mcp23017Setup(65,0x20)
|
||||
|
||||
27
README.md
27
README.md
@@ -10,9 +10,15 @@ Build with gcc version 4.6.3 (Debian 4.6.3-14+rpi1)
|
||||
Built against Python 2.7.2, Python 3.2.3
|
||||
|
||||
**Prerequisites:**
|
||||
You must have python-dev and python-setuptools installed
|
||||
You **must** have python-dev and python-setuptools installed
|
||||
If you manually rebuild the bindings with swig -python wiringpi.i
|
||||
|
||||
YOU MUST FIRST INSTALL WIRINGPI2!!
|
||||
|
||||
git clone git://git.drogon.net/wiringPi
|
||||
cd wiringPi
|
||||
sudo ./build
|
||||
|
||||
**Get/setup repo:**
|
||||
git clone https://github.com/Gadgetoid/WiringPi2-Python.git
|
||||
cd WiringPi2-Python
|
||||
@@ -29,11 +35,12 @@ Description incoming!
|
||||
**Usage:**
|
||||
|
||||
import wiringpi2
|
||||
wiringpi2.wiringPiSetup() // For sequential pin numbering, one of these MUST be called before using IO functions
|
||||
|
||||
wiringpi2.wiringPiSetup() # For sequential pin numbering, one of these MUST be called before using IO functions
|
||||
OR
|
||||
wiringpi2.wiringPiSetupSys() // For /sys/class/gpio with GPIO pin numbering
|
||||
wiringpi2.wiringPiSetupSys() # For /sys/class/gpio with GPIO pin numbering
|
||||
OR
|
||||
wiringpi2.wiringPiSetupGpio() // For GPIO pin numbering
|
||||
wiringpi2.wiringPiSetupGpio() # For GPIO pin numbering
|
||||
|
||||
Setting up IO expanders (This example was tested on a quick2wire board with one digital IO expansion board connected via I2C):
|
||||
|
||||
@@ -43,9 +50,9 @@ Setting up IO expanders (This example was tested on a quick2wire board with one
|
||||
|
||||
**General IO:**
|
||||
|
||||
wiringpi2.pinMode(6,1) // Set pin 6 to 1 ( OUTPUT )
|
||||
wiringpi2.digitalWrite(6,1) // Write 1 ( HIGH ) to pin 6
|
||||
wiringpi2.digitalRead(6) // Read pin 6
|
||||
wiringpi2.pinMode(6,1) # Set pin 6 to 1 ( OUTPUT )
|
||||
wiringpi2.digitalWrite(6,1) # Write 1 ( HIGH ) to pin 6
|
||||
wiringpi2.digitalRead(6) # Read pin 6
|
||||
|
||||
**Setting up a peripheral:**
|
||||
WiringPi2 supports expanding your range of available "pins" by setting up a port expander. The implementation details of
|
||||
@@ -63,13 +70,13 @@ Hook a speaker up to your Pi and generate music with softTone. Also useful for g
|
||||
|
||||
**Bit shifting:**
|
||||
|
||||
wiringpi2.shiftOut(1,2,0,123) // Shift out 123 (b1110110, byte 0-255) to data pin 1, clock pin 2
|
||||
wiringpi2.shiftOut(1,2,0,123) # Shift out 123 (b1110110, byte 0-255) to data pin 1, clock pin 2
|
||||
|
||||
**Serial:**
|
||||
|
||||
serial = wiringpi2.serialOpen('/dev/ttyAMA0',9600) // Requires device/baud and returns an ID
|
||||
serial = wiringpi2.serialOpen('/dev/ttyAMA0',9600) # Requires device/baud and returns an ID
|
||||
wiringpi2.serialPuts(serial,"hello")
|
||||
wiringpi2.serialClose(serial) // Pass in ID
|
||||
wiringpi2.serialClose(serial) # Pass in ID
|
||||
|
||||
**Full details at:**
|
||||
http://www.wiringpi.com
|
||||
|
||||
@@ -30,18 +30,6 @@ To un-install wiringPi:
|
||||
|
||||
./build uninstall
|
||||
|
||||
|
||||
I2C:
|
||||
|
||||
If your system has the correct i2c-dev libraries and headers installed,
|
||||
then the I2C helpers will be compiled into wiringPi. If you want to
|
||||
use the I2C helpers and don't have them installed, then under Raspbian,
|
||||
issue the command:
|
||||
|
||||
sudo apt-get install libi2c-dev
|
||||
|
||||
Consult the documentation for your system if you are not running Raspbian.
|
||||
|
||||
Gordon Henderson
|
||||
|
||||
projects@drogon.net
|
||||
|
||||
@@ -25,3 +25,9 @@ CHARLES Thibaut:
|
||||
|
||||
Xian Stannard
|
||||
Fixing some typos in the man page!
|
||||
|
||||
Andre Crone
|
||||
Suggested the __WIRING_PI.H__ round wiringPi.h
|
||||
|
||||
Rik Teerling
|
||||
Pointing out some silly mistooks in the I2C code...
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
WiringPi: An implementation of most of the Arduino Wiring
|
||||
functions for the Raspberry Pi,
|
||||
along with many more features and libraries to support
|
||||
hardware, etc. on the Raspberry Pi
|
||||
|
||||
Full details at:
|
||||
https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
|
||||
@@ -16,7 +16,7 @@ accepted to Github....
|
||||
|
||||
Please see
|
||||
|
||||
https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
http://wiringpi.com/
|
||||
|
||||
for the official documentation, etc. and the best way to submit bug reports, etc.
|
||||
is by sending an email to projects@drogon.net
|
||||
|
||||
103
WiringPi/build
Executable file
103
WiringPi/build
Executable file
@@ -0,0 +1,103 @@
|
||||
#!/bin/sh
|
||||
|
||||
check_make_ok() {
|
||||
if [ $? != 0 ]; then
|
||||
echo ""
|
||||
echo "Make Failed..."
|
||||
echo "Please check the messages and fix any problems. If you're still stuck,"
|
||||
echo "then please email all the output and as many details as you can to"
|
||||
echo " projects@drogon.net"
|
||||
echo ""
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [ x$1 = "xclean" ]; then
|
||||
cd wiringPi
|
||||
echo -n "wiringPi: " ; make clean
|
||||
cd ../devLib
|
||||
echo -n "DevLib: " ; make clean
|
||||
cd ../gpio
|
||||
echo -n "gpio: " ; make clean
|
||||
cd ../examples
|
||||
echo -n "Examples: " ; make clean
|
||||
cd Gertboard
|
||||
echo -n "Gertboard: " ; make clean
|
||||
cd ../PiFace
|
||||
echo -n "PiFace: " ; make clean
|
||||
cd ../q2w
|
||||
echo -n "Quick2Wire: " ; make clean
|
||||
cd ../PiGlow
|
||||
echo -n "PiGlow: " ; make clean
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ x$1 = "xuninstall" ]; then
|
||||
cd wiringPi
|
||||
echo -n "wiringPi: " ; sudo make uninstall
|
||||
cd ../devLib
|
||||
echo -n "DevLib: " ; sudo make uninstall
|
||||
cd ../gpio
|
||||
echo -n "gpio: " ; sudo make uninstall
|
||||
exit
|
||||
fi
|
||||
|
||||
|
||||
echo "wiringPi Build script"
|
||||
echo "====================="
|
||||
echo
|
||||
|
||||
echo
|
||||
echo "WiringPi Library"
|
||||
cd wiringPi
|
||||
sudo make uninstall
|
||||
if [ x$1 = "xstatic" ]; then
|
||||
make static
|
||||
check_make_ok
|
||||
sudo make install-static
|
||||
else
|
||||
make
|
||||
check_make_ok
|
||||
sudo make install
|
||||
fi
|
||||
check_make_ok
|
||||
|
||||
echo
|
||||
echo "WiringPi Devices Library"
|
||||
cd ../devLib
|
||||
sudo make uninstall
|
||||
if [ x$1 = "xstatic" ]; then
|
||||
make static
|
||||
check_make_ok
|
||||
sudo make install-static
|
||||
else
|
||||
make
|
||||
check_make_ok
|
||||
sudo make install
|
||||
fi
|
||||
check_make_ok
|
||||
|
||||
echo
|
||||
echo "GPIO Utility"
|
||||
cd ../gpio
|
||||
make
|
||||
check_make_ok
|
||||
sudo make install
|
||||
check_make_ok
|
||||
|
||||
# echo
|
||||
# echo "Examples"
|
||||
# cd ../examples
|
||||
# make
|
||||
# cd ..
|
||||
|
||||
echo
|
||||
echo All Done.
|
||||
echo ""
|
||||
echo "NOTE: To compile programs with wiringPi, you need to add:"
|
||||
echo " -lwiringPi"
|
||||
echo " to your compile line(s) To use the Gertboard, MaxDetect, etc."
|
||||
echo " code (the devLib), you need to also add:"
|
||||
echo " -lwiringPiDev"
|
||||
echo " to your compile line(s)."
|
||||
echo ""
|
||||
134
WiringPi/devLib/Makefile
Normal file
134
WiringPi/devLib/Makefile
Normal file
@@ -0,0 +1,134 @@
|
||||
# Makefile:
|
||||
# wiringPi device - Wiring Compatable library 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/>.
|
||||
#################################################################################
|
||||
|
||||
DYN_VERS_MAJ=2
|
||||
DYN_VERS_MIN=0
|
||||
|
||||
VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN)
|
||||
DESTDIR=/usr
|
||||
PREFIX=/local
|
||||
|
||||
STATIC=libwiringPiDev.a
|
||||
DYNAMIC=libwiringPiDev.so.$(VERSION)
|
||||
|
||||
#DEBUG = -g -O0
|
||||
DEBUG = -O2
|
||||
CC = gcc
|
||||
INCLUDE = -I.
|
||||
CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC
|
||||
|
||||
LIBS =
|
||||
|
||||
###############################################################################
|
||||
|
||||
SRC = ds1302.c maxdetect.c piNes.c \
|
||||
gertboard.c piFace.c \
|
||||
lcd128x64.c lcd.c \
|
||||
piGlow.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
all: $(DYNAMIC)
|
||||
|
||||
static: $(STATIC)
|
||||
|
||||
$(STATIC): $(OBJ)
|
||||
@echo "[Link (Static)]"
|
||||
@ar rcs $(STATIC) $(OBJ)
|
||||
@ranlib $(STATIC)
|
||||
# @size $(STATIC)
|
||||
|
||||
$(DYNAMIC): $(OBJ)
|
||||
@echo "[Link (Dynamic)]"
|
||||
@$(CC) -shared -Wl,-soname,libwiringPiDev.so -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ)
|
||||
|
||||
.c.o:
|
||||
@echo [Compile] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONEY: clean
|
||||
clean:
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPiDev.*
|
||||
|
||||
.PHONEY: tags
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
|
||||
.PHONEY: install-headers
|
||||
install-headers:
|
||||
@echo "[Install Headers]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 ds1302.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 maxdetect.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 lcd128x64.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piGlow.h $(DESTDIR)$(PREFIX)/include
|
||||
|
||||
.PHONEY: install
|
||||
install: $(DYNAMIC) install-headers
|
||||
@echo "[Install Dynamic Lib]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION)
|
||||
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so
|
||||
@ldconfig
|
||||
|
||||
.PHONEY: install-static
|
||||
install-static: $(STATIC) install-headers
|
||||
@echo "[Install Static Lib]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib
|
||||
|
||||
.PHONEY: uninstall
|
||||
uninstall:
|
||||
@echo "[UnInstall]"
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/maxdetect.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/piNes.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/piFace.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/lcd128x64.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/lcd.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/piGlow.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.*
|
||||
@ldconfig
|
||||
|
||||
|
||||
.PHONEY: depend
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
ds1302.o: ds1302.h
|
||||
maxdetect.o: maxdetect.h
|
||||
piNes.o: piNes.h
|
||||
gertboard.o: gertboard.h
|
||||
piFace.o: piFace.h
|
||||
lcd128x64.o: font.h lcd128x64.h
|
||||
lcd.o: lcd.h
|
||||
piGlow.o: piGlow.h
|
||||
@@ -27,7 +27,8 @@
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include "ds1302.h"
|
||||
|
||||
// Register defines
|
||||
@@ -55,7 +56,7 @@ static int dPin, cPin, sPin ;
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int dsShiftIn (void)
|
||||
static unsigned int dsShiftIn (void)
|
||||
{
|
||||
uint8_t value = 0 ;
|
||||
int i ;
|
||||
@@ -80,7 +81,7 @@ unsigned int dsShiftIn (void)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void dsShiftOut (unsigned int data)
|
||||
static void dsShiftOut (unsigned int data)
|
||||
{
|
||||
int i ;
|
||||
|
||||
@@ -101,7 +102,7 @@ void dsShiftOut (unsigned int data)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static unsigned int ds1302regRead (int reg)
|
||||
static unsigned int ds1302regRead (const int reg)
|
||||
{
|
||||
unsigned int data ;
|
||||
|
||||
@@ -113,7 +114,7 @@ static unsigned int ds1302regRead (int reg)
|
||||
return data ;
|
||||
}
|
||||
|
||||
static void ds1302regWrite (int reg, unsigned int data)
|
||||
static void ds1302regWrite (const int reg, const unsigned int data)
|
||||
{
|
||||
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
|
||||
dsShiftOut (reg) ;
|
||||
@@ -128,7 +129,7 @@ static void ds1302regWrite (int reg, unsigned int data)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int ds1302rtcRead (int reg)
|
||||
unsigned int ds1302rtcRead (const int reg)
|
||||
{
|
||||
return ds1302regRead (0x81 | ((reg & 0x1F) << 1)) ;
|
||||
}
|
||||
@@ -145,12 +146,12 @@ void ds1302rtcWrite (int reg, unsigned int data)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int ds1302ramRead (int addr)
|
||||
unsigned int ds1302ramRead (const int addr)
|
||||
{
|
||||
return ds1302regRead (0xC1 | ((addr & 0x1F) << 1)) ;
|
||||
}
|
||||
|
||||
void ds1302ramWrite (int addr, unsigned int data)
|
||||
void ds1302ramWrite (const int addr, const unsigned int data)
|
||||
{
|
||||
ds1302regWrite ( 0xC0 | ((addr & 0x1F) << 1), data) ;
|
||||
}
|
||||
@@ -182,7 +183,7 @@ void ds1302clockRead (int clockData [8])
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302clockWrite (int clockData [8])
|
||||
void ds1302clockWrite (const int clockData [8])
|
||||
{
|
||||
int i ;
|
||||
unsigned int regVal = 0x80 | ((RTC_BM & 0x1F) << 1) ;
|
||||
@@ -204,7 +205,7 @@ void ds1302clockWrite (int clockData [8])
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302trickleCharge (int diodes, int resistors)
|
||||
void ds1302trickleCharge (const int diodes, const int resistors)
|
||||
{
|
||||
if (diodes + resistors == 0)
|
||||
ds1302rtcWrite (RTC_TC, 0x5C) ; // Disabled
|
||||
@@ -221,7 +222,7 @@ void ds1302trickleCharge (int diodes, int resistors)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302setup (int clockPin, int dataPin, int csPin)
|
||||
void ds1302setup (const int clockPin, const int dataPin, const int csPin)
|
||||
{
|
||||
dPin = dataPin ;
|
||||
cPin = clockPin ;
|
||||
@@ -26,18 +26,18 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern unsigned int ds1302rtcRead (int reg) ;
|
||||
extern void ds1302rtcWrite (int reg, unsigned int data) ;
|
||||
extern unsigned int ds1302rtcRead (const int reg) ;
|
||||
extern void ds1302rtcWrite (const int reg, const unsigned int data) ;
|
||||
|
||||
extern unsigned int ds1302ramRead (int addr) ;
|
||||
extern void ds1302ramWrite (int addr, unsigned int data) ;
|
||||
extern unsigned int ds1302ramRead (const int addr) ;
|
||||
extern void ds1302ramWrite (const int addr, const unsigned int data) ;
|
||||
|
||||
extern void ds1302clockRead (int clockData [8]) ;
|
||||
extern void ds1302clockWrite (int clockData [8]) ;
|
||||
extern void ds1302clockWrite (const int clockData [8]) ;
|
||||
|
||||
extern void ds1302trickleCharge (int diodes, int resistors) ;
|
||||
extern void ds1302trickleCharge (const int diodes, const int resistors) ;
|
||||
|
||||
extern void ds1302setup (int clockPin, int dataPin, int csPin) ;
|
||||
extern void ds1302setup (const int clockPin, const int dataPin, const int csPin) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
2577
WiringPi/devLib/font.h
Normal file
2577
WiringPi/devLib/font.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -38,8 +38,8 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/spi/spidev.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiSPI.h"
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "gertboard.h"
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void gertboardAnalogWrite (int chan, int value)
|
||||
void gertboardAnalogWrite (const int chan, const int value)
|
||||
{
|
||||
uint8_t spiData [2] ;
|
||||
uint8_t chanBits, dataBits ;
|
||||
@@ -85,7 +85,7 @@ void gertboardAnalogWrite (int chan, int value)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gertboardAnalogRead (int chan)
|
||||
int gertboardAnalogRead (const int chan)
|
||||
{
|
||||
uint8_t spiData [2] ;
|
||||
|
||||
@@ -128,16 +128,14 @@ int gertboardSPISetup (void)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gbWiringPiAnalogRead (struct wiringPiNodeStruct *node, int chan)
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, const int chan)
|
||||
{
|
||||
chan -= node->pinBase ;
|
||||
return gertboardAnalogRead (chan) ;
|
||||
return gertboardAnalogRead (chan - node->pinBase) ;
|
||||
}
|
||||
|
||||
void gbWiringPiAnalogWrite (struct wiringPiNodeStruct *node, int chan, int value)
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, const int chan, const int value)
|
||||
{
|
||||
chan -= node->pinBase ;
|
||||
gertboardAnalogWrite (chan, value) ;
|
||||
gertboardAnalogWrite (chan - node->pinBase, value) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -150,7 +148,7 @@ void gbWiringPiAnalogWrite (struct wiringPiNodeStruct *node, int chan, int value
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gertboardAnalogSetup (int pinBase)
|
||||
int gertboardAnalogSetup (const int pinBase)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
int x ;
|
||||
@@ -159,8 +157,8 @@ int gertboardAnalogSetup (int pinBase)
|
||||
return x;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
node->analogRead = gbWiringPiAnalogRead ;
|
||||
node->analogWrite = gbWiringPiAnalogWrite ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
@@ -30,10 +30,15 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void gertboardAnalogWrite (int chan, int value) ;
|
||||
extern int gertboardAnalogRead (int chan) ;
|
||||
// Old routines
|
||||
|
||||
extern void gertboardAnalogWrite (const int chan, const int value) ;
|
||||
extern int gertboardAnalogRead (const int chan) ;
|
||||
extern int gertboardSPISetup (void) ;
|
||||
extern int gertboardAnalogSetup (int pinBase) ;
|
||||
|
||||
// New
|
||||
|
||||
extern int gertboardAnalogSetup (const int pinBase) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
@@ -26,29 +26,40 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include "lcd.h"
|
||||
|
||||
// Commands
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
// HD44780U Commands
|
||||
|
||||
#define LCD_CLEAR 0x01
|
||||
#define LCD_HOME 0x02
|
||||
#define LCD_ENTRY 0x04
|
||||
#define LCD_ON_OFF 0x08
|
||||
#define LCD_CTRL 0x08
|
||||
#define LCD_CDSHIFT 0x10
|
||||
#define LCD_FUNC 0x20
|
||||
#define LCD_CGRAM 0x40
|
||||
#define LCD_DGRAM 0x80
|
||||
|
||||
#define LCD_ENTRY_SH 0x01
|
||||
#define LCD_ENTRY_ID 0x02
|
||||
// Bits in the entry register
|
||||
|
||||
#define LCD_ON_OFF_B 0x01
|
||||
#define LCD_ON_OFF_C 0x02
|
||||
#define LCD_ON_OFF_D 0x04
|
||||
#define LCD_ENTRY_SH 0x01
|
||||
#define LCD_ENTRY_ID 0x02
|
||||
|
||||
// Bits in the control register
|
||||
|
||||
#define LCD_BLINK_CTRL 0x01
|
||||
#define LCD_CURSOR_CTRL 0x02
|
||||
#define LCD_DISPLAY_CTRL 0x04
|
||||
|
||||
// Bits in the function register
|
||||
|
||||
#define LCD_FUNC_F 0x04
|
||||
#define LCD_FUNC_N 0x08
|
||||
@@ -58,13 +69,20 @@
|
||||
|
||||
struct lcdDataStruct
|
||||
{
|
||||
uint8_t bits, rows, cols ;
|
||||
uint8_t rsPin, strbPin ;
|
||||
uint8_t dataPins [8] ;
|
||||
int bits, rows, cols ;
|
||||
int rsPin, strbPin ;
|
||||
int dataPins [8] ;
|
||||
int cx, cy ;
|
||||
} ;
|
||||
|
||||
struct lcdDataStruct *lcds [MAX_LCDS] ;
|
||||
|
||||
static int lcdControl ;
|
||||
|
||||
// Row offsets
|
||||
|
||||
static const int rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
|
||||
|
||||
|
||||
/*
|
||||
* strobe:
|
||||
@@ -73,7 +91,7 @@ struct lcdDataStruct *lcds [MAX_LCDS] ;
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void strobe (struct lcdDataStruct *lcd)
|
||||
static void strobe (const struct lcdDataStruct *lcd)
|
||||
{
|
||||
|
||||
// Note timing changes for new version of delayMicroseconds ()
|
||||
@@ -89,13 +107,14 @@ static void strobe (struct lcdDataStruct *lcd)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
|
||||
static void sendDataCmd (const struct lcdDataStruct *lcd, unsigned char data)
|
||||
{
|
||||
uint8_t i, d4 ;
|
||||
register unsigned char myData = data ;
|
||||
unsigned char i, d4 ;
|
||||
|
||||
if (lcd->bits == 4)
|
||||
{
|
||||
d4 = (data >> 4) & 0x0F;
|
||||
d4 = (myData >> 4) & 0x0F;
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
|
||||
@@ -103,7 +122,7 @@ static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
|
||||
}
|
||||
strobe (lcd) ;
|
||||
|
||||
d4 = data & 0x0F ;
|
||||
d4 = myData & 0x0F ;
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
|
||||
@@ -114,8 +133,8 @@ static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
|
||||
{
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (data & 1)) ;
|
||||
data >>= 1 ;
|
||||
digitalWrite (lcd->dataPins [i], (myData & 1)) ;
|
||||
myData >>= 1 ;
|
||||
}
|
||||
}
|
||||
strobe (lcd) ;
|
||||
@@ -128,22 +147,24 @@ static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void putCommand (struct lcdDataStruct *lcd, uint8_t command)
|
||||
static void putCommand (const struct lcdDataStruct *lcd, unsigned char command)
|
||||
{
|
||||
digitalWrite (lcd->rsPin, 0) ;
|
||||
sendDataCmd (lcd, command) ;
|
||||
delay (2) ;
|
||||
}
|
||||
|
||||
static void put4Command (struct lcdDataStruct *lcd, uint8_t command)
|
||||
static void put4Command (const struct lcdDataStruct *lcd, unsigned char command)
|
||||
{
|
||||
uint8_t i ;
|
||||
register unsigned char myCommand = command ;
|
||||
register unsigned char i ;
|
||||
|
||||
digitalWrite (lcd->rsPin, 0) ;
|
||||
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (command & 1)) ;
|
||||
command >>= 1 ;
|
||||
digitalWrite (lcd->dataPins [i], (myCommand & 1)) ;
|
||||
myCommand >>= 1 ;
|
||||
}
|
||||
strobe (lcd) ;
|
||||
}
|
||||
@@ -151,7 +172,7 @@ static void put4Command (struct lcdDataStruct *lcd, uint8_t command)
|
||||
|
||||
/*
|
||||
*********************************************************************************
|
||||
* User Code below here
|
||||
* User Callable code below here
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
@@ -161,16 +182,66 @@ static void put4Command (struct lcdDataStruct *lcd, uint8_t command)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdHome (int fd)
|
||||
void lcdHome (const int fd)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
putCommand (lcd, LCD_HOME) ;
|
||||
lcd->cx = lcd->cy = 0 ;
|
||||
delay (5) ;
|
||||
}
|
||||
|
||||
void lcdClear (int fd)
|
||||
void lcdClear (const int fd)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
putCommand (lcd, LCD_CLEAR) ;
|
||||
putCommand (lcd, LCD_HOME) ;
|
||||
lcd->cx = lcd->cy = 0 ;
|
||||
delay (5) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdDisplay: lcdCursor: lcdCursorBlink:
|
||||
* Turn the display, cursor, cursor blinking on/off
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdDisplay (const int fd, int state)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
if (state)
|
||||
lcdControl |= LCD_DISPLAY_CTRL ;
|
||||
else
|
||||
lcdControl &= ~LCD_DISPLAY_CTRL ;
|
||||
|
||||
putCommand (lcd, LCD_CTRL | lcdControl) ;
|
||||
}
|
||||
|
||||
void lcdCursor (const int fd, int state)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
if (state)
|
||||
lcdControl |= LCD_CURSOR_CTRL ;
|
||||
else
|
||||
lcdControl &= ~LCD_CURSOR_CTRL ;
|
||||
|
||||
putCommand (lcd, LCD_CTRL | lcdControl) ;
|
||||
}
|
||||
|
||||
void lcdCursorBlink (const int fd, int state)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
if (state)
|
||||
lcdControl |= LCD_BLINK_CTRL ;
|
||||
else
|
||||
lcdControl &= ~LCD_BLINK_CTRL ;
|
||||
|
||||
putCommand (lcd, LCD_CTRL | lcdControl) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -180,40 +251,77 @@ void lcdClear (int fd)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdSendCommand (int fd, uint8_t command)
|
||||
void lcdSendCommand (const int fd, unsigned char command)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
putCommand (lcd, command) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdPosition:
|
||||
* Update the position of the cursor on the display
|
||||
* Update the position of the cursor on the display.
|
||||
* Ignore invalid locations.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
|
||||
void lcdPosition (int fd, int x, int y)
|
||||
void lcdPosition (const int fd, int x, int y)
|
||||
{
|
||||
static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
if ((x > lcd->cols) || (x < 0))
|
||||
return ;
|
||||
if ((y > lcd->rows) || (y < 0))
|
||||
return ;
|
||||
|
||||
putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
|
||||
|
||||
lcd->cx = x ;
|
||||
lcd->cy = y ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdCharDef:
|
||||
* Defines a new character in the CGRAM
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdCharDef (const int fd, int index, unsigned char data [8])
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
int i ;
|
||||
|
||||
putCommand (lcd, LCD_CGRAM | ((index & 7) << 3)) ;
|
||||
|
||||
digitalWrite (lcd->rsPin, 1) ;
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
sendDataCmd (lcd, data [i]) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdPutchar:
|
||||
* Send a data byte to be displayed on the display
|
||||
* Send a data byte to be displayed on the display. We implement a very
|
||||
* simple terminal here - with line wrapping, but no scrolling. Yet.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdPutchar (int fd, uint8_t data)
|
||||
void lcdPutchar (const int fd, unsigned char data)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
digitalWrite (lcd->rsPin, 1) ;
|
||||
sendDataCmd (lcd, data) ;
|
||||
sendDataCmd (lcd, data) ;
|
||||
|
||||
if (++lcd->cx == lcd->cols)
|
||||
{
|
||||
lcd->cx = 0 ;
|
||||
if (++lcd->cy == lcd->rows)
|
||||
lcd->cy = 0 ;
|
||||
|
||||
putCommand (lcd, lcd->cx + (LCD_DGRAM | rowOff [lcd->cy])) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -223,7 +331,7 @@ void lcdPutchar (int fd, uint8_t data)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdPuts (int fd, char *string)
|
||||
void lcdPuts (const int fd, const char *string)
|
||||
{
|
||||
while (*string)
|
||||
lcdPutchar (fd, *string++) ;
|
||||
@@ -236,7 +344,7 @@ void lcdPuts (int fd, char *string)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdPrintf (int fd, char *message, ...)
|
||||
void lcdPrintf (const int fd, const char *message, ...)
|
||||
{
|
||||
va_list argp ;
|
||||
char buffer [1024] ;
|
||||
@@ -256,12 +364,14 @@ void lcdPrintf (int fd, char *message, ...)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
|
||||
int lcdInit (const int rows, const int cols, const int bits,
|
||||
const int rs, const int strb,
|
||||
const int d0, const int d1, const int d2, const int d3, const int d4,
|
||||
const int d5, const int d6, const int d7)
|
||||
{
|
||||
static int initialised = 0 ;
|
||||
|
||||
uint8_t func ;
|
||||
unsigned char func ;
|
||||
int i ;
|
||||
int lcdFd = -1 ;
|
||||
struct lcdDataStruct *lcd ;
|
||||
@@ -298,7 +408,7 @@ int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
if (lcdFd == -1)
|
||||
return -1 ;
|
||||
|
||||
lcd = malloc (sizeof (struct lcdDataStruct)) ;
|
||||
lcd = (struct lcdDataStruct *)malloc (sizeof (struct lcdDataStruct)) ;
|
||||
if (lcd == NULL)
|
||||
return -1 ;
|
||||
|
||||
@@ -307,6 +417,8 @@ int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
lcd->bits = 8 ; // For now - we'll set it properly later.
|
||||
lcd->rows = rows ;
|
||||
lcd->cols = cols ;
|
||||
lcd->cx = 0 ;
|
||||
lcd->cy = 0 ;
|
||||
|
||||
lcd->dataPins [0] = d0 ;
|
||||
lcd->dataPins [1] = d1 ;
|
||||
@@ -371,10 +483,13 @@ int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
|
||||
// Rest of the initialisation sequence
|
||||
|
||||
putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ;
|
||||
putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ;
|
||||
putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ;
|
||||
putCommand (lcd, LCD_CLEAR) ; delay (5) ;
|
||||
lcdDisplay (lcdFd, TRUE) ;
|
||||
lcdCursor (lcdFd, FALSE) ;
|
||||
lcdCursorBlink (lcdFd, FALSE) ;
|
||||
lcdClear (lcdFd) ;
|
||||
|
||||
putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ;
|
||||
putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ;
|
||||
|
||||
return lcdFd ;
|
||||
}
|
||||
52
WiringPi/devLib/lcd.h
Normal file
52
WiringPi/devLib/lcd.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* lcd.h:
|
||||
* Text-based LCD driver.
|
||||
* This is designed to drive the parallel interface LCD drivers
|
||||
* based in the Hitachi HD44780U controller and compatables.
|
||||
*
|
||||
* Copyright (c) 2012 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#define MAX_LCDS 8
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void lcdHome (const int fd) ;
|
||||
extern void lcdClear (const int fd) ;
|
||||
extern void lcdDisplay (const int fd, int state) ;
|
||||
extern void lcdCursor (const int fd, int state) ;
|
||||
extern void lcdCursorBlink (const int fd, int state) ;
|
||||
extern void lcdSendCommand (const int fd, unsigned char command) ;
|
||||
extern void lcdPosition (const int fd, int x, int y) ;
|
||||
extern void lcdCharDef (const int fd, int index, unsigned char data [8]) ;
|
||||
extern void lcdPutchar (const int fd, unsigned char data) ;
|
||||
extern void lcdPuts (const int fd, const char *string) ;
|
||||
extern void lcdPrintf (const int fd, const char *message, ...) ;
|
||||
|
||||
extern int lcdInit (const int rows, const int cols, const int bits,
|
||||
const int rs, const int strb,
|
||||
const int d0, const int d1, const int d2, const int d3, const int d4,
|
||||
const int d5, const int d6, const int d7) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
673
WiringPi/devLib/lcd128x64.c
Normal file
673
WiringPi/devLib/lcd128x64.c
Normal file
@@ -0,0 +1,673 @@
|
||||
/*
|
||||
* lcd128x64.c:
|
||||
* Graphics-based LCD driver.
|
||||
* This is designed to drive the parallel interface LCD drivers
|
||||
* based on the generic 12864H chips
|
||||
*
|
||||
* There are many variations on these chips, however they all mostly
|
||||
* seem to be similar.
|
||||
* This implementation has the Pins from the Pi hard-wired into it,
|
||||
* in particular wiringPi pins 0-7 so that we can use
|
||||
* digitalWriteByete() to speed things up somewhat.
|
||||
*
|
||||
* 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 <wiringPi.h>
|
||||
|
||||
#include "font.h"
|
||||
#include "lcd128x64.h"
|
||||
|
||||
// Size
|
||||
|
||||
#define LCD_WIDTH 128
|
||||
#define LCD_HEIGHT 64
|
||||
|
||||
// Hardware Pins
|
||||
// Note pins 0-7 are the 8-bit data port
|
||||
|
||||
#define CS1 10
|
||||
#define CS2 11
|
||||
#define STROBE 12
|
||||
#define RS 13
|
||||
|
||||
// Software copy of the framebuffer
|
||||
// it's 8-bit deep although the display itself is only 1-bit deep.
|
||||
|
||||
static unsigned char frameBuffer [LCD_WIDTH * LCD_HEIGHT] ;
|
||||
|
||||
static int maxX, maxY ;
|
||||
static int lastX, lastY ;
|
||||
static int xOrigin, yOrigin ;
|
||||
static int lcdOrientation = 0 ;
|
||||
|
||||
/*
|
||||
* strobe:
|
||||
* Toggle the strobe (Really the "E") pin to the device.
|
||||
* According to the docs, data is latched on the falling edge.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void strobe (void)
|
||||
{
|
||||
digitalWrite (STROBE, 1) ; delayMicroseconds (1) ;
|
||||
digitalWrite (STROBE, 0) ; delayMicroseconds (5) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* sentData:
|
||||
* Send an data or command byte to the display.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void sendData (const int data, const int chip)
|
||||
{
|
||||
digitalWrite (chip, 0) ;
|
||||
digitalWriteByte (data) ;
|
||||
strobe () ;
|
||||
digitalWrite (chip, 1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* sendCommand:
|
||||
* Send a command byte to the display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void sendCommand (const int command, const int chip)
|
||||
{
|
||||
digitalWrite (RS, 0) ;
|
||||
sendData (command, chip) ;
|
||||
digitalWrite (RS, 1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* setCol: SetLine:
|
||||
* Set the column and line addresses
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void setCol (int col, const int chip)
|
||||
{ sendCommand (0x40 | (col & 0x3F), chip) ; }
|
||||
|
||||
static void setLine (int line, const int chip)
|
||||
{ sendCommand (0xB8 | (line & 0x07), chip) ; }
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64update:
|
||||
* Copy our software version to the real display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64update (void)
|
||||
{
|
||||
int line, x, y, fbLoc ;
|
||||
unsigned char byte ;
|
||||
|
||||
// Left side
|
||||
|
||||
for (line = 0 ; line < 8 ; ++line)
|
||||
{
|
||||
setCol (0, CS1) ;
|
||||
setLine (line, CS1) ;
|
||||
|
||||
for (x = 63 ; x >= 0 ; --x)
|
||||
{
|
||||
byte = 0 ;
|
||||
for (y = 0 ; y < 8 ; ++y)
|
||||
{
|
||||
fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ;
|
||||
if (frameBuffer [fbLoc] != 0)
|
||||
byte |= (1 << y) ;
|
||||
}
|
||||
sendData (byte, CS1) ;
|
||||
}
|
||||
}
|
||||
|
||||
// Right side
|
||||
|
||||
for (line = 0 ; line < 8 ; ++line)
|
||||
{
|
||||
setCol (0, CS2) ;
|
||||
setLine (line, CS2) ;
|
||||
|
||||
for (x = 127 ; x >= 64 ; --x)
|
||||
{
|
||||
byte = 0 ;
|
||||
for (y = 0 ; y < 8 ; ++y)
|
||||
{
|
||||
fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ;
|
||||
if (frameBuffer [fbLoc] != 0)
|
||||
byte |= (1 << y) ;
|
||||
}
|
||||
sendData (byte, CS2) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64setOrigin:
|
||||
* Set the display offset origin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64setOrigin (int x, int y)
|
||||
{
|
||||
xOrigin = x ;
|
||||
yOrigin = y ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64setOrientation:
|
||||
* Set the display orientation:
|
||||
* 0: Normal, the display is portrait mode, 0,0 is top left
|
||||
* 1: Landscape
|
||||
* 2: Portrait, flipped
|
||||
* 3: Landscape, flipped
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64setOrientation (int orientation)
|
||||
{
|
||||
lcdOrientation = orientation & 3 ;
|
||||
|
||||
lcd128x64setOrigin (0,0) ;
|
||||
|
||||
switch (lcdOrientation)
|
||||
{
|
||||
case 0:
|
||||
maxX = LCD_WIDTH ;
|
||||
maxY = LCD_HEIGHT ;
|
||||
break ;
|
||||
|
||||
case 1:
|
||||
maxX = LCD_HEIGHT ;
|
||||
maxY = LCD_WIDTH ;
|
||||
break ;
|
||||
|
||||
case 2:
|
||||
maxX = LCD_WIDTH ;
|
||||
maxY = LCD_HEIGHT ;
|
||||
break ;
|
||||
|
||||
case 3:
|
||||
maxX = LCD_HEIGHT ;
|
||||
maxY = LCD_WIDTH ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64orientCoordinates:
|
||||
* Adjust the coordinates given to the display orientation
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64orientCoordinates (int *x, int *y)
|
||||
{
|
||||
register int tmp ;
|
||||
|
||||
*x += xOrigin ;
|
||||
*y += yOrigin ;
|
||||
*y = maxY - *y - 1 ;
|
||||
|
||||
switch (lcdOrientation)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
|
||||
case 1:
|
||||
tmp = maxY - *y - 1 ;
|
||||
*y = *x ;
|
||||
*x = tmp ;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
*x = maxX - *x - 1 ;
|
||||
*y = maxY - *y - 1 ;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
*x = maxX - *x - 1 ;
|
||||
tmp = *y ;
|
||||
*y = *x ;
|
||||
*x = tmp ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64getScreenSize:
|
||||
* Return the max X & Y screen sizes. Needs to be called again, if you
|
||||
* change screen orientation.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64getScreenSize (int *x, int *y)
|
||||
{
|
||||
*x = maxX ;
|
||||
*y = maxY ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************
|
||||
* Standard Graphical Functions
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64point:
|
||||
* Plot a pixel.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64point (int x, int y, int colour)
|
||||
{
|
||||
lastX = x ;
|
||||
lastY = y ;
|
||||
|
||||
lcd128x64orientCoordinates (&x, &y) ;
|
||||
|
||||
if ((x < 0) || (x >= LCD_WIDTH) || (y < 0) || (y >= LCD_HEIGHT))
|
||||
return ;
|
||||
|
||||
frameBuffer [x + y * LCD_WIDTH] = colour ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64line: lcd128x64lineTo:
|
||||
* Classic Bressenham Line code
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64line (int x0, int y0, int x1, int y1, int colour)
|
||||
{
|
||||
int dx, dy ;
|
||||
int sx, sy ;
|
||||
int err, e2 ;
|
||||
|
||||
lastX = x1 ;
|
||||
lastY = y1 ;
|
||||
|
||||
dx = abs (x1 - x0) ;
|
||||
dy = abs (y1 - y0) ;
|
||||
|
||||
sx = (x0 < x1) ? 1 : -1 ;
|
||||
sy = (y0 < y1) ? 1 : -1 ;
|
||||
|
||||
err = dx - dy ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
lcd128x64point (x0, y0, colour) ;
|
||||
|
||||
if ((x0 == x1) && (y0 == y1))
|
||||
break ;
|
||||
|
||||
e2 = 2 * err ;
|
||||
|
||||
if (e2 > -dy)
|
||||
{
|
||||
err -= dy ;
|
||||
x0 += sx ;
|
||||
}
|
||||
|
||||
if (e2 < dx)
|
||||
{
|
||||
err += dx ;
|
||||
y0 += sy ;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void lcd128x64lineTo (int x, int y, int colour)
|
||||
{
|
||||
lcd128x64line (lastX, lastY, x, y, colour) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64rectangle:
|
||||
* A rectangle is a spoilt days fishing
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled)
|
||||
{
|
||||
register int x ;
|
||||
|
||||
if (filled)
|
||||
{
|
||||
/**/ if (x1 == x2)
|
||||
lcd128x64line (x1, y1, x2, y2, colour) ;
|
||||
else if (x1 < x2)
|
||||
for (x = x1 ; x <= x2 ; ++x)
|
||||
lcd128x64line (x, y1, x, y2, colour) ;
|
||||
else
|
||||
for (x = x2 ; x <= x1 ; ++x)
|
||||
lcd128x64line (x, y1, x, y2, colour) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd128x64line (x1, y1, x2, y1, colour) ;
|
||||
lcd128x64lineTo (x2, y2, colour) ;
|
||||
lcd128x64lineTo (x1, y2, colour) ;
|
||||
lcd128x64lineTo (x1, y1, colour) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64circle:
|
||||
* This is the midpoint circle algorithm.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64circle (int x, int y, int r, int colour, int filled)
|
||||
{
|
||||
int ddF_x = 1 ;
|
||||
int ddF_y = -2 * r ;
|
||||
|
||||
int f = 1 - r ;
|
||||
int x1 = 0 ;
|
||||
int y1 = r ;
|
||||
|
||||
if (filled)
|
||||
{
|
||||
lcd128x64line (x, y + r, x, y - r, colour) ;
|
||||
lcd128x64line (x + r, y, x - r, y, colour) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd128x64point (x, y + r, colour) ;
|
||||
lcd128x64point (x, y - r, colour) ;
|
||||
lcd128x64point (x + r, y, colour) ;
|
||||
lcd128x64point (x - r, y, colour) ;
|
||||
}
|
||||
|
||||
while (x1 < y1)
|
||||
{
|
||||
if (f >= 0)
|
||||
{
|
||||
y1-- ;
|
||||
ddF_y += 2 ;
|
||||
f += ddF_y ;
|
||||
}
|
||||
x1++ ;
|
||||
ddF_x += 2 ;
|
||||
f += ddF_x ;
|
||||
if (filled)
|
||||
{
|
||||
lcd128x64line (x + x1, y + y1, x - x1, y + y1, colour) ;
|
||||
lcd128x64line (x + x1, y - y1, x - x1, y - y1, colour) ;
|
||||
lcd128x64line (x + y1, y + x1, x - y1, y + x1, colour) ;
|
||||
lcd128x64line (x + y1, y - x1, x - y1, y - x1, colour) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd128x64point (x + x1, y + y1, colour) ; lcd128x64point (x - x1, y + y1, colour) ;
|
||||
lcd128x64point (x + x1, y - y1, colour) ; lcd128x64point (x - x1, y - y1, colour) ;
|
||||
lcd128x64point (x + y1, y + x1, colour) ; lcd128x64point (x - y1, y + x1, colour) ;
|
||||
lcd128x64point (x + y1, y - x1, colour) ; lcd128x64point (x - y1, y - x1, colour) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64ellipse:
|
||||
* Fast ellipse drawing algorithm by
|
||||
* John Kennedy
|
||||
* Mathematics Department
|
||||
* Santa Monica College
|
||||
* 1900 Pico Blvd.
|
||||
* Santa Monica, CA 90405
|
||||
* jrkennedy6@gmail.com
|
||||
* -Confirned in email this algorithm is in the public domain -GH-
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void plot4ellipsePoints (int cx, int cy, int x, int y, int colour, int filled)
|
||||
{
|
||||
if (filled)
|
||||
{
|
||||
lcd128x64line (cx + x, cy + y, cx - x, cy + y, colour) ;
|
||||
lcd128x64line (cx - x, cy - y, cx + x, cy - y, colour) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd128x64point (cx + x, cy + y, colour) ;
|
||||
lcd128x64point (cx - x, cy + y, colour) ;
|
||||
lcd128x64point (cx - x, cy - y, colour) ;
|
||||
lcd128x64point (cx + x, cy - y, colour) ;
|
||||
}
|
||||
}
|
||||
|
||||
void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled)
|
||||
{
|
||||
int x, y ;
|
||||
int xChange, yChange, ellipseError ;
|
||||
int twoAsquare, twoBsquare ;
|
||||
int stoppingX, stoppingY ;
|
||||
|
||||
twoAsquare = 2 * xRadius * xRadius ;
|
||||
twoBsquare = 2 * yRadius * yRadius ;
|
||||
|
||||
x = xRadius ;
|
||||
y = 0 ;
|
||||
|
||||
xChange = yRadius * yRadius * (1 - 2 * xRadius) ;
|
||||
yChange = xRadius * xRadius ;
|
||||
|
||||
ellipseError = 0 ;
|
||||
stoppingX = twoBsquare * xRadius ;
|
||||
stoppingY = 0 ;
|
||||
|
||||
while (stoppingX >= stoppingY) // 1st set of points
|
||||
{
|
||||
plot4ellipsePoints (cx, cy, x, y, colour, filled) ;
|
||||
++y ;
|
||||
stoppingY += twoAsquare ;
|
||||
ellipseError += yChange ;
|
||||
yChange += twoAsquare ;
|
||||
|
||||
if ((2 * ellipseError + xChange) > 0 )
|
||||
{
|
||||
--x ;
|
||||
stoppingX -= twoBsquare ;
|
||||
ellipseError += xChange ;
|
||||
xChange += twoBsquare ;
|
||||
}
|
||||
}
|
||||
|
||||
x = 0 ;
|
||||
y = yRadius ;
|
||||
|
||||
xChange = yRadius * yRadius ;
|
||||
yChange = xRadius * xRadius * (1 - 2 * yRadius) ;
|
||||
|
||||
ellipseError = 0 ;
|
||||
stoppingX = 0 ;
|
||||
stoppingY = twoAsquare * yRadius ;
|
||||
|
||||
while (stoppingX <= stoppingY) //2nd set of points
|
||||
{
|
||||
plot4ellipsePoints (cx, cy, x, y, colour, filled) ;
|
||||
++x ;
|
||||
stoppingX += twoBsquare ;
|
||||
ellipseError += xChange ;
|
||||
xChange += twoBsquare ;
|
||||
|
||||
if ((2 * ellipseError + yChange) > 0 )
|
||||
{
|
||||
--y ;
|
||||
stoppingY -= twoAsquare ;
|
||||
ellipseError += yChange ;
|
||||
yChange += twoAsquare ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64putchar:
|
||||
* Print a single character to the screen
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol)
|
||||
{
|
||||
int y1, y2 ;
|
||||
|
||||
unsigned char line ;
|
||||
unsigned char *fontPtr ;
|
||||
|
||||
// Can't print if we're offscreen
|
||||
|
||||
//if ((x < 0) || (x >= (maxX - fontWidth)) || (y < 0) || (y >= (maxY - fontHeight)))
|
||||
// return ;
|
||||
|
||||
fontPtr = font + c * fontHeight ;
|
||||
|
||||
for (y1 = fontHeight - 1 ; y1 >= 0 ; --y1)
|
||||
{
|
||||
y2 = y + y1 ;
|
||||
line = *fontPtr++ ;
|
||||
lcd128x64point (x + 0, y2, (line & 0x80) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 1, y2, (line & 0x40) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 2, y2, (line & 0x20) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 3, y2, (line & 0x10) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 4, y2, (line & 0x08) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 5, y2, (line & 0x04) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 6, y2, (line & 0x02) == 0 ? bgCol : fgCol) ;
|
||||
lcd128x64point (x + 7, y2, (line & 0x01) == 0 ? bgCol : fgCol) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64puts:
|
||||
* Send a string to the display. Obeys \n and \r formatting
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol)
|
||||
{
|
||||
int c, mx, my ;
|
||||
|
||||
mx = x ; my = y ;
|
||||
|
||||
while (*str)
|
||||
{
|
||||
c = *str++ ;
|
||||
|
||||
if (c == '\r')
|
||||
{
|
||||
mx = x ;
|
||||
continue ;
|
||||
}
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
mx = x ;
|
||||
my -= fontHeight ;
|
||||
continue ;
|
||||
}
|
||||
|
||||
lcd128x64putchar (mx, my, c, bgCol, fgCol) ;
|
||||
|
||||
mx += fontWidth ;
|
||||
if (mx >= (maxX - fontWidth))
|
||||
{
|
||||
mx = 0 ;
|
||||
my -= fontHeight ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64clear:
|
||||
* Clear the display to the given colour.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcd128x64clear (int colour)
|
||||
{
|
||||
register int i ;
|
||||
register unsigned char *ptr = frameBuffer ;
|
||||
|
||||
for (i = 0 ; i < (maxX * maxY) ; ++i)
|
||||
*ptr++ = colour ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* lcd128x64setup:
|
||||
* Initialise the display and GPIO.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int lcd128x64setup (void)
|
||||
{
|
||||
int i ;
|
||||
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
pinMode (i, OUTPUT) ;
|
||||
|
||||
digitalWrite (CS1, 1) ;
|
||||
digitalWrite (CS2, 1) ;
|
||||
digitalWrite (STROBE, 0) ;
|
||||
digitalWrite (RS, 1) ;
|
||||
|
||||
pinMode (CS1, OUTPUT) ;
|
||||
pinMode (CS2, OUTPUT) ;
|
||||
pinMode (STROBE, OUTPUT) ;
|
||||
pinMode (RS, OUTPUT) ;
|
||||
|
||||
sendCommand (0x3F, CS1) ; // Display ON
|
||||
sendCommand (0xC0, CS1) ; // Set display start line to 0
|
||||
|
||||
sendCommand (0x3F, CS2) ; // Display ON
|
||||
sendCommand (0xC0, CS2) ; // Set display start line to 0
|
||||
|
||||
lcd128x64clear (0) ;
|
||||
lcd128x64setOrientation (0) ;
|
||||
lcd128x64update () ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
39
WiringPi/devLib/lcd128x64.h
Normal file
39
WiringPi/devLib/lcd128x64.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* lcd128x64.h:
|
||||
*
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
extern void lcd128x64setOrigin (int x, int y) ;
|
||||
extern void lcd128x64setOrientation (int orientation) ;
|
||||
extern void lcd128x64orientCoordinates (int *x, int *y) ;
|
||||
extern void lcd128x64getScreenSize (int *x, int *y) ;
|
||||
extern void lcd128x64point (int x, int y, int colour) ;
|
||||
extern void lcd128x64line (int x0, int y0, int x1, int y1, int colour) ;
|
||||
extern void lcd128x64lineTo (int x, int y, int colour) ;
|
||||
extern void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled) ;
|
||||
extern void lcd128x64circle (int x, int y, int r, int colour, int filled) ;
|
||||
extern void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled) ;
|
||||
extern void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol) ;
|
||||
extern void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol) ;
|
||||
extern void lcd128x64update (void) ;
|
||||
extern void lcd128x64clear (int colour) ;
|
||||
|
||||
extern int lcd128x64setup (void) ;
|
||||
165
WiringPi/devLib/maxdetect.c
Executable file
165
WiringPi/devLib/maxdetect.c
Executable file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* maxdetect.c:
|
||||
* Driver for the MaxDetect series sensors
|
||||
*
|
||||
* 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 <unistd.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include "maxdetect.h"
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* maxDetectLowHighWait:
|
||||
* Wait for a transition from high to low on the bus
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void maxDetectLowHighWait (const int pin)
|
||||
{
|
||||
unsigned int timeOut = millis () + 2000 ;
|
||||
|
||||
while (digitalRead (pin) == HIGH)
|
||||
if (millis () > timeOut)
|
||||
return ;
|
||||
|
||||
while (digitalRead (pin) == LOW)
|
||||
if (millis () > timeOut)
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* maxDetectClockByte:
|
||||
* Read in a single byte from the MaxDetect bus
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static unsigned int maxDetectClockByte (const int pin)
|
||||
{
|
||||
unsigned int byte = 0 ;
|
||||
int bit ;
|
||||
|
||||
for (bit = 0 ; bit < 8 ; ++bit)
|
||||
{
|
||||
maxDetectLowHighWait (pin) ;
|
||||
|
||||
// bit starting now - we need to time it.
|
||||
|
||||
delayMicroseconds (30) ;
|
||||
byte <<= 1 ;
|
||||
if (digitalRead (pin) == HIGH) // It's a 1
|
||||
byte |= 1 ;
|
||||
}
|
||||
|
||||
return byte ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* maxDetectRead:
|
||||
* Read in and return the 4 data bytes from the MaxDetect sensor.
|
||||
* Return TRUE/FALSE depending on the checksum validity
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int maxDetectRead (const int pin, unsigned char buffer [4])
|
||||
{
|
||||
int i ;
|
||||
unsigned int checksum ;
|
||||
unsigned char localBuf [5] ;
|
||||
|
||||
// Wake up the RHT03 by pulling the data line low, then high
|
||||
// Low for 10mS, high for 40uS.
|
||||
|
||||
pinMode (pin, OUTPUT) ;
|
||||
digitalWrite (pin, 0) ; delay (10) ;
|
||||
digitalWrite (pin, 1) ; delayMicroseconds (40) ;
|
||||
pinMode (pin, INPUT) ;
|
||||
|
||||
// Now wait for sensor to pull pin low
|
||||
|
||||
maxDetectLowHighWait (pin) ;
|
||||
|
||||
// and read in 5 bytes (40 bits)
|
||||
|
||||
for (i = 0 ; i < 5 ; ++i)
|
||||
localBuf [i] = maxDetectClockByte (pin) ;
|
||||
|
||||
checksum = 0 ;
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
buffer [i] = localBuf [i] ;
|
||||
checksum += localBuf [i] ;
|
||||
}
|
||||
checksum &= 0xFF ;
|
||||
|
||||
return checksum == localBuf [4] ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* readRHT03:
|
||||
* Read the Temperature & Humidity from an RHT03 sensor
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int readRHT03 (const int pin, int *temp, int *rh)
|
||||
{
|
||||
static unsigned int nextTime = 0 ;
|
||||
static int lastTemp = 0 ;
|
||||
static int lastRh = 0 ;
|
||||
static int lastResult = TRUE ;
|
||||
|
||||
unsigned char buffer [4] ;
|
||||
|
||||
// Don't read more than once a second
|
||||
|
||||
if (millis () < nextTime)
|
||||
{
|
||||
*temp = lastTemp ;
|
||||
*rh = lastRh ;
|
||||
return lastResult ;
|
||||
}
|
||||
|
||||
lastResult = maxDetectRead (pin, buffer) ;
|
||||
|
||||
if (lastResult)
|
||||
{
|
||||
*temp = lastTemp = (buffer [2] * 256 + buffer [3]) ;
|
||||
*rh = lastRh = (buffer [0] * 256 + buffer [1]) ;
|
||||
nextTime = millis () + 2000 ;
|
||||
return TRUE ;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE ;
|
||||
}
|
||||
}
|
||||
24
WiringPi/wiringPi/lcd.h → WiringPi/devLib/maxdetect.h
Normal file → Executable file
24
WiringPi/wiringPi/lcd.h → WiringPi/devLib/maxdetect.h
Normal file → Executable file
@@ -1,10 +1,8 @@
|
||||
/*
|
||||
* lcd.h:
|
||||
* Text-based LCD driver.
|
||||
* This is designed to drive the parallel interface LCD drivers
|
||||
* based in the Hitachi HD44780U controller and compatables.
|
||||
* maxdetect.h:
|
||||
* Driver for the MaxDetect series sensors
|
||||
*
|
||||
* Copyright (c) 2012 Gordon Henderson.
|
||||
* Copyright (c) 2013 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
@@ -24,22 +22,18 @@
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#define MAX_LCDS 8
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void lcdHome (int fd) ;
|
||||
extern void lcdClear (int fd) ;
|
||||
extern void lcdSendCommand (int fd, uint8_t command) ;
|
||||
extern void lcdPosition (int fd, int x, int y) ;
|
||||
extern void lcdPutchar (int fd, uint8_t data) ;
|
||||
extern void lcdPuts (int fd, char *string) ;
|
||||
extern void lcdPrintf (int fd, char *message, ...) ;
|
||||
// Main generic function
|
||||
|
||||
extern int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ;
|
||||
int maxDetectRead (const int pin, unsigned char buffer [4]) ;
|
||||
|
||||
// Individual sensors
|
||||
|
||||
int readRHT03 (const int pin, int *temp, int *rh) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
112
WiringPi/devLib/piFace.c
Normal file
112
WiringPi/devLib/piFace.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* piFace.:
|
||||
* This file to interface with the PiFace peripheral device which
|
||||
* has an MCP23S17 GPIO device connected via the SPI bus.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://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 <stdint.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <mcp23s17.h>
|
||||
|
||||
#include "piFace.h"
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
* Perform the digitalWrite function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
digitalWrite (pin + 16, value) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
* Perform the digitalRead function on the PiFace board
|
||||
* With a slight twist - if we read from base + 8, then we
|
||||
* read from the output latch...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
if ((pin - node->pinBase) >= 8)
|
||||
return digitalRead (pin + 8) ;
|
||||
else
|
||||
return digitalRead (pin + 16 + 8) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
* Perform the pullUpDnControl function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
|
||||
{
|
||||
pullUpDnControl (pin + 16 + 8, pud) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* piFaceSetup
|
||||
* We're going to create an instance of the mcp23s17 here, then
|
||||
* provide our own read/write routines on-top of it...
|
||||
* The supplied PiFace code (in Pithon) treats it as an 8-bit device
|
||||
* where you write the output ports and read the input port using the
|
||||
* same pin numbers, however I have had a request to be able to read
|
||||
* the output port, so reading 8..15 will read the output latch.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int piFaceSetup (const int pinBase)
|
||||
{
|
||||
int i ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
// Create an mcp23s17 instance:
|
||||
|
||||
mcp23s17Setup (pinBase + 16, 0, 0) ;
|
||||
|
||||
// Set the direction bits
|
||||
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
pinMode (pinBase + 16 + i, OUTPUT) ; // Port A is the outputs
|
||||
pinMode (pinBase + 16 + 8 + i, INPUT) ; // Port B inputs.
|
||||
}
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int piFaceSetup (int pinBase) ;
|
||||
extern int piFaceSetup (const int pinBase) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
@@ -29,15 +29,16 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiSPI.h"
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "../wiringPi/mcp23x0817.h"
|
||||
|
||||
#include "piFace.h"
|
||||
|
||||
#define PIFACE_SPEED 4000000
|
||||
#define PIFACE_DEVNO 0
|
||||
|
||||
#include "mcp23x0817.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -77,12 +78,12 @@ static uint8_t readByte (uint8_t reg)
|
||||
|
||||
|
||||
/*
|
||||
* digitalWrite:
|
||||
* myDigitalWrite:
|
||||
* Perform the digitalWrite function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void digitalWritePiFace (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
uint8_t mask, old ;
|
||||
|
||||
@@ -100,17 +101,16 @@ void digitalWritePiFace (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
|
||||
|
||||
/*
|
||||
* digitalReadPiFace:
|
||||
* myDigitalRead:
|
||||
* Perform the digitalRead function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int digitalReadPiFace (struct wiringPiNodeStruct *node, int pin)
|
||||
int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
uint8_t mask, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
mask = 1 << (pin & 7) ;
|
||||
mask = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
if (pin < 8)
|
||||
reg = MCP23x17_GPIOB ; // Input regsiter
|
||||
@@ -125,17 +125,16 @@ int digitalReadPiFace (struct wiringPiNodeStruct *node, int pin)
|
||||
|
||||
|
||||
/*
|
||||
* pullUpDnControlPiFace:
|
||||
* myPullUpDnControl:
|
||||
* Perform the pullUpDnControl function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void pullUpDnControlPiFace (struct wiringPiNodeStruct *node, int pin, int pud)
|
||||
void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
|
||||
{
|
||||
uint8_t mask, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
mask = 1 << pin ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
old = readByte (MCP23x17_GPPUB) ;
|
||||
|
||||
if (pud == 0)
|
||||
@@ -156,7 +155,7 @@ void pullUpDnControlPiFace (struct wiringPiNodeStruct *node, int pin, int pud)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int piFaceSetup (int pinBase)
|
||||
int piFaceSetup (const int pinBase)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
@@ -171,9 +170,9 @@ int piFaceSetup (int pinBase)
|
||||
writeByte (MCP23x17_IODIRB, 0xFF) ; // Port B -> Inputs
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
node->digitalRead = digitalReadPiFace ;
|
||||
node->digitalWrite = digitalWritePiFace ;
|
||||
node->pullUpDnControl = pullUpDnControlPiFace ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
118
WiringPi/devLib/piGlow.c
Normal file
118
WiringPi/devLib/piGlow.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* piGlow.c:
|
||||
* Easy access to the Pimoroni PiGlow board.
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <sn3218.h>
|
||||
|
||||
#include "piGlow.h"
|
||||
|
||||
#define PIGLOW_BASE 577
|
||||
|
||||
static int leg0 [6] = { 6, 7, 8, 5, 4, 9 } ;
|
||||
static int leg1 [6] = { 17, 16, 15, 13, 11, 10 } ;
|
||||
static int leg2 [6] = { 0, 1, 2, 3, 14, 12 } ;
|
||||
|
||||
|
||||
/*
|
||||
* piGlow1:
|
||||
* Light up an individual LED
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void piGlow1 (const int leg, const int ring, const int intensity)
|
||||
{
|
||||
int *legLeds ;
|
||||
|
||||
if ((leg < 0) || (leg > 2)) return ;
|
||||
if ((ring < 0) || (ring > 5)) return ;
|
||||
|
||||
/**/ if (leg == 0)
|
||||
legLeds = leg0 ;
|
||||
else if (leg == 1)
|
||||
legLeds = leg1 ;
|
||||
else
|
||||
legLeds = leg2 ;
|
||||
|
||||
analogWrite (PIGLOW_BASE + legLeds [ring], intensity) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* piGlowLeg:
|
||||
* Light up all 6 LEDs on a leg
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void piGlowLeg (const int leg, const int intensity)
|
||||
{
|
||||
int i ;
|
||||
int *legLeds ;
|
||||
|
||||
if ((leg < 0) || (leg > 2))
|
||||
return ;
|
||||
|
||||
/**/ if (leg == 0)
|
||||
legLeds = leg0 ;
|
||||
else if (leg == 1)
|
||||
legLeds = leg1 ;
|
||||
else
|
||||
legLeds = leg2 ;
|
||||
|
||||
for (i = 0 ; i < 6 ; ++i)
|
||||
analogWrite (PIGLOW_BASE + legLeds [i], intensity) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* piGlowRing:
|
||||
* Light up 3 LEDs in a ring. Ring 0 is the outermost, 5 the innermost
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void piGlowRing (const int ring, const int intensity)
|
||||
{
|
||||
if ((ring < 0) || (ring > 5))
|
||||
return ;
|
||||
|
||||
analogWrite (PIGLOW_BASE + leg0 [ring], intensity) ;
|
||||
analogWrite (PIGLOW_BASE + leg1 [ring], intensity) ;
|
||||
analogWrite (PIGLOW_BASE + leg2 [ring], intensity) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* piGlowSetup:
|
||||
* Initialise the board & remember the pins we're using
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void piGlowSetup (int clear)
|
||||
{
|
||||
sn3218Setup (PIGLOW_BASE) ;
|
||||
|
||||
if (clear)
|
||||
{
|
||||
piGlowLeg (0, 0) ;
|
||||
piGlowLeg (1, 0) ;
|
||||
piGlowLeg (2, 0) ;
|
||||
}
|
||||
}
|
||||
45
WiringPi/devLib/piGlow.h
Normal file
45
WiringPi/devLib/piGlow.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* piglow.h:
|
||||
* Easy access to the Pimoroni PiGlow board.
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
#define PIGLOW_RED 0
|
||||
#define PIGLOW_YELLOW 1
|
||||
#define PIGLOW_ORANGE 2
|
||||
#define PIGLOW_GREEN 3
|
||||
#define PIGLOW_BLUE 4
|
||||
#define PIGLOW_WHITE 5
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void piGlow1 (const int leg, const int ring, const int intensity) ;
|
||||
extern void piGlowLeg (const int leg, const int intensity) ;
|
||||
extern void piGlowRing (const int ring, const int intensity) ;
|
||||
extern void piGlowSetup (int clear) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -22,7 +22,7 @@
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include "piNes.h"
|
||||
|
||||
221
WiringPi/examples/Gertboard/7segments.c
Normal file
221
WiringPi/examples/Gertboard/7segments.c
Normal 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 ;
|
||||
}
|
||||
74
WiringPi/examples/Gertboard/Makefile
Normal file
74
WiringPi/examples/Gertboard/Makefile
Normal file
@@ -0,0 +1,74 @@
|
||||
#
|
||||
# Makefile:
|
||||
# Gertboard - Examples using wiringPi
|
||||
#
|
||||
# Copyright (c) 2013 Gordon Henderson
|
||||
#################################################################################
|
||||
|
||||
#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
|
||||
@echo [link]
|
||||
@$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
buttons: buttons.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
7segments: 7segments.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ 7segments.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
voltmeter: voltmeter.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ voltmeter.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
temperature: temperature.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ temperature.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
vumeter: vumeter.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ vumeter.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
record: record.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ record.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
@echo [Clean]
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
83
WiringPi/examples/Gertboard/buttons.c
Normal file
83
WiringPi/examples/Gertboard/buttons.c
Normal 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) ;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
* 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
|
||||
* copy this value to D/A port 1 and use a 'scope on both D/A ports
|
||||
* to check all's well.
|
||||
* plot the input value on the terminal as a sort of vertical scrolling
|
||||
* oscilloscipe.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
@@ -30,11 +30,13 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define B_SIZE 200
|
||||
#undef DO_TIMING
|
||||
// Gertboard D to A is an 8-bit unit.
|
||||
|
||||
#define B_SIZE 256
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <gertboard.h>
|
||||
@@ -42,23 +44,28 @@
|
||||
int main (void)
|
||||
{
|
||||
double angle ;
|
||||
int i ;
|
||||
uint32_t x1 ;
|
||||
int i, inputValue ;
|
||||
int buffer [B_SIZE] ;
|
||||
int cols ;
|
||||
struct winsize w ;
|
||||
|
||||
#ifdef DO_TIMING
|
||||
unsigned int now, then ;
|
||||
#endif
|
||||
|
||||
printf ("Raspberry Pi Gertboard SPI test program\n") ;
|
||||
printf ("=======================================\n") ;
|
||||
|
||||
if (wiringPiSetupSys () < 0)
|
||||
return -1 ;
|
||||
ioctl (fileno (stdin), TIOCGWINSZ, &w);
|
||||
cols = w.ws_col - 2 ;
|
||||
|
||||
if (gertboardSPISetup () < 0)
|
||||
return 1 ;
|
||||
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
||||
// (or want) to run as root
|
||||
|
||||
// Generate a Sine Wave
|
||||
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)
|
||||
{
|
||||
@@ -66,28 +73,23 @@ int main (void)
|
||||
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 (;;)
|
||||
{
|
||||
#ifdef DO_TIMING
|
||||
then = millis () ;
|
||||
#endif
|
||||
|
||||
for (i = 0 ; i < B_SIZE ; ++i)
|
||||
{
|
||||
gertboardAnalogWrite (0, buffer [i]) ;
|
||||
analogWrite (100, buffer [i]) ;
|
||||
|
||||
#ifndef DO_TIMING
|
||||
x1 = gertboardAnalogRead (0) ;
|
||||
gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A
|
||||
#endif
|
||||
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) ;
|
||||
}
|
||||
|
||||
#ifdef DO_TIMING
|
||||
now = millis () ;
|
||||
printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ;
|
||||
printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
60
WiringPi/examples/Gertboard/record.c
Normal file
60
WiringPi/examples/Gertboard/record.c
Normal 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 ;
|
||||
}
|
||||
|
||||
78
WiringPi/examples/Gertboard/temperature.c
Normal file
78
WiringPi/examples/Gertboard/temperature.c
Normal 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 ;
|
||||
}
|
||||
|
||||
73
WiringPi/examples/Gertboard/voltmeter.c
Normal file
73
WiringPi/examples/Gertboard/voltmeter.c
Normal 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 ;
|
||||
}
|
||||
|
||||
152
WiringPi/examples/Gertboard/vumeter.c
Normal file
152
WiringPi/examples/Gertboard/vumeter.c
Normal 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 ;
|
||||
}
|
||||
@@ -30,15 +30,20 @@ INCLUDE = -I/usr/local/include
|
||||
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe
|
||||
|
||||
LDFLAGS = -L/usr/local/lib
|
||||
LDLIBS = -lwiringPi -lpthread -lm
|
||||
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm
|
||||
|
||||
# Should not alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = blink.c test1.c test2.c speed.c lcd.c wfi.c isr.c isr-osc.c \
|
||||
piface.c gertboard.c nes.c \
|
||||
pwm.c tone.c servo.c \
|
||||
delayTest.c serialRead.c serialTest.c okLed.c ds1302.c
|
||||
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 \
|
||||
rht03.c piglow.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
@@ -55,13 +60,17 @@ blink: blink.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
test1: test1.o
|
||||
blink8: blink8.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ test1.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
test2: test2.o
|
||||
@$(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
blink12drcs: blink12drcs.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ test2.o $(LDFLAGS) $(LDLIBS)
|
||||
@$(CC) -o $@ blink12drcs.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
blink12: blink12.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
speed: speed.o
|
||||
@echo [link]
|
||||
@@ -71,6 +80,14 @@ lcd: lcd.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
lcd-adafruit: lcd-adafruit.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ lcd-adafruit.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
clock: clock.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
wfi: wfi.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS)
|
||||
@@ -83,22 +100,26 @@ isr-osc: isr-osc.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
piface: piface.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
gertboard: gertboard.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
nes: nes.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
rht03: rht03.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ rht03.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
pwm: pwm.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
softPwm: softPwm.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ softPwm.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
softTone: softTone.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ softTone.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
delayTest: delayTest.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS)
|
||||
@@ -119,21 +140,22 @@ tone: tone.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
servo: servo.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ servo.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
ds1302: ds1302.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
piglow: piglow.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) *~ core tags $(BINS)
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
|
||||
85
WiringPi/examples/PiFace/Makefile
Normal file
85
WiringPi/examples/PiFace/Makefile
Normal file
@@ -0,0 +1,85 @@
|
||||
#
|
||||
# Makefile:
|
||||
# wiringPi - Wiring Compatable library for the Raspberry Pi
|
||||
# https://projects.drogon.net/wiring-pi
|
||||
#
|
||||
# Copyright (c) 2012 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/>.
|
||||
#################################################################################
|
||||
|
||||
|
||||
#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
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
buttons: buttons.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
reaction: reaction.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ reaction.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
ladder: ladder.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ ladder.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
metro: metro.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ metro.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
motor: motor.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ motor.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
59
WiringPi/examples/PiFace/blink.c
Normal file
59
WiringPi/examples/PiFace/blink.c
Normal 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 ;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* piFace.c:
|
||||
* buttons.c:
|
||||
* Simple test for the PiFace interface board.
|
||||
*
|
||||
* Read the buttons and output the same to the LEDs
|
||||
@@ -24,23 +24,33 @@
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <piFace.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.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)
|
||||
@@ -48,26 +58,45 @@ void scanButton (int button)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* start here
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int pin, button ;
|
||||
|
||||
printf ("Raspberry Pi wiringPiFace test program\n") ;
|
||||
printf ("======================================\n") ;
|
||||
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") ;
|
||||
|
||||
if (piFaceSetup (200) == -1)
|
||||
exit (1) ;
|
||||
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
||||
// (or want) to run as root
|
||||
|
||||
// Enable internal pull-ups
|
||||
wiringPiSetupSys () ;
|
||||
|
||||
for (pin = PIFACE_BASE ; pin < (PIFACE_BASE + 8) ; ++pin)
|
||||
pullUpDnControl (pin, PUD_UP) ;
|
||||
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 (1) ;
|
||||
delay (5) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
337
WiringPi/examples/PiFace/ladder.c
Executable file
337
WiringPi/examples/PiFace/ladder.c
Executable 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
WiringPi/examples/PiFace/metro.c
Normal file
111
WiringPi/examples/PiFace/metro.c
Normal 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
WiringPi/examples/PiFace/motor.c
Normal file
120
WiringPi/examples/PiFace/motor.c
Normal 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
WiringPi/examples/PiFace/reaction.c
Normal file
194
WiringPi/examples/PiFace/reaction.c
Normal 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 ;
|
||||
}
|
||||
79
WiringPi/examples/PiGlow/Makefile
Normal file
79
WiringPi/examples/PiGlow/Makefile
Normal file
@@ -0,0 +1,79 @@
|
||||
#
|
||||
# Makefile:
|
||||
# wiringPi - Wiring Compatable library for the Raspberry Pi
|
||||
# https://projects.drogon.net/wiring-pi
|
||||
#
|
||||
# Copyright (c) 2012-2013 Gordon Henderson
|
||||
#################################################################################
|
||||
# This file is part of wiringPi:
|
||||
# Wiring Compatable library for the Raspberry Pi
|
||||
#
|
||||
# wiringPi is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# wiringPi is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
#################################################################################
|
||||
|
||||
|
||||
#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
|
||||
@echo [link]
|
||||
@$(CC) -o $@ piGlow0.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
piGlow1: piGlow1.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
piglow: piglow.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
install: piglow
|
||||
@echo Installing piglow into /usr/local/bin
|
||||
@cp -a piglow /usr/local/bin/piglow
|
||||
@chmod 755 /usr/local/bin/piglow
|
||||
@echo Done. Remember to load the I2C drivers!
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
51
WiringPi/examples/PiGlow/piGlow0.c
Normal file
51
WiringPi/examples/PiGlow/piGlow0.c
Normal 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
WiringPi/examples/PiGlow/piGlow1.c
Normal file
258
WiringPi/examples/PiGlow/piGlow1.c
Normal 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
WiringPi/examples/PiGlow/piglow.c
Normal file
176
WiringPi/examples/PiGlow/piglow.c
Normal 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 ;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,16 +34,14 @@ int main (void)
|
||||
{
|
||||
printf ("Raspberry Pi blink\n") ;
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
return 1 ;
|
||||
|
||||
wiringPiSetup () ;
|
||||
pinMode (LED, OUTPUT) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
digitalWrite (LED, 1) ; // On
|
||||
digitalWrite (LED, HIGH) ; // On
|
||||
delay (500) ; // mS
|
||||
digitalWrite (LED, 0) ; // Off
|
||||
digitalWrite (LED, LOW) ; // Off
|
||||
delay (500) ;
|
||||
}
|
||||
return 0 ;
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
# LED Pin - wiringPi pin 0 is BCM_GPIO 17.
|
||||
|
||||
LED=0
|
||||
PIN=0
|
||||
|
||||
gpio mode $PIN out
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
/*
|
||||
* test1.c:
|
||||
* Simple test program to test the wiringPi functions
|
||||
* This is a sequencer to make a patter appear on 8 LEDs
|
||||
* connected to the GPIO pins.
|
||||
* 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>
|
||||
***********************************************************************
|
||||
@@ -24,17 +23,13 @@
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
// Simple sequencer data
|
||||
// Triplets of LED, On/Off and delay
|
||||
|
||||
uint8_t data [] =
|
||||
int data [] =
|
||||
{
|
||||
0, 1, 1,
|
||||
1, 1, 1,
|
||||
@@ -44,15 +39,23 @@ uint8_t data [] =
|
||||
3, 0, 0, 5, 1, 1,
|
||||
4, 0, 0, 6, 1, 1,
|
||||
5, 0, 0, 7, 1, 1,
|
||||
6, 0, 1,
|
||||
7, 0, 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
|
||||
|
||||
7, 1, 1,
|
||||
6, 1, 1,
|
||||
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,
|
||||
@@ -64,7 +67,7 @@ uint8_t data [] =
|
||||
|
||||
0, 0, 1, // Extra delay
|
||||
|
||||
9, 9, 9, // End marker
|
||||
0, 9, 0, // End marker
|
||||
|
||||
} ;
|
||||
|
||||
@@ -75,16 +78,17 @@ int main (void)
|
||||
int dataPtr ;
|
||||
int l, s, d ;
|
||||
|
||||
printf ("Raspberry Pi wiringPi test program\n") ;
|
||||
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") ;
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
exit (1) ;
|
||||
wiringPiSetup () ;
|
||||
|
||||
for (pin = 0 ; pin < 8 ; ++pin)
|
||||
for (pin = 0 ; pin < 14 ; ++pin)
|
||||
pinMode (pin, OUTPUT) ;
|
||||
|
||||
pinMode (8, INPUT) ; // Pin 8 SDA0 - Has on-board 2k2 pull-up resistor
|
||||
|
||||
dataPtr = 0 ;
|
||||
|
||||
for (;;)
|
||||
@@ -93,18 +97,14 @@ int main (void)
|
||||
s = data [dataPtr++] ; // State
|
||||
d = data [dataPtr++] ; // Duration (10ths)
|
||||
|
||||
if ((l + s + d) == 27)
|
||||
if (s == 9) // 9 -> End Marker
|
||||
{
|
||||
dataPtr = 0 ;
|
||||
continue ;
|
||||
}
|
||||
|
||||
digitalWrite (l, s) ;
|
||||
|
||||
if (digitalRead (8) == 0) // Pressed as our switch shorts to ground
|
||||
delay (d * 10) ; // Faster!
|
||||
else
|
||||
delay (d * 100) ;
|
||||
delay (d * 100) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
125
WiringPi/examples/blink12drcs.c
Normal file
125
WiringPi/examples/blink12drcs.c
Normal 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
WiringPi/examples/blink6drcs.c
Normal file
115
WiringPi/examples/blink6drcs.c
Normal 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 ;
|
||||
}
|
||||
57
WiringPi/examples/blink8.c
Normal file
57
WiringPi/examples/blink8.c
Normal 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
WiringPi/examples/clock.c
Normal file
201
WiringPi/examples/clock.c
Normal 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 ;
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 4.7 KiB |
@@ -38,16 +38,11 @@
|
||||
#include <wiringPi.h>
|
||||
|
||||
|
||||
// What GPIO input are we using?
|
||||
// This is a wiringPi pin number
|
||||
|
||||
#define BUTTON_PIN 0
|
||||
|
||||
// globalCounter:
|
||||
// Global variable to count interrupts
|
||||
// Should be declared volatile to make sure the compiler doesn't cache it.
|
||||
|
||||
static volatile int globalCounter = 0 ;
|
||||
static volatile int globalCounter [8] ;
|
||||
|
||||
|
||||
/*
|
||||
@@ -55,10 +50,14 @@ static volatile int globalCounter = 0 ;
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void myInterrupt (void)
|
||||
{
|
||||
++globalCounter ;
|
||||
}
|
||||
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] ; }
|
||||
|
||||
|
||||
/*
|
||||
@@ -69,30 +68,42 @@ void myInterrupt (void)
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int myCounter = 0 ;
|
||||
int gotOne, pin ;
|
||||
int myCounter [8] ;
|
||||
|
||||
if (wiringPiSetup () < 0)
|
||||
{
|
||||
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
|
||||
return 1 ;
|
||||
}
|
||||
for (pin = 0 ; pin < 8 ; ++pin)
|
||||
globalCounter [pin] = myCounter [pin] = 0 ;
|
||||
|
||||
if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
|
||||
{
|
||||
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
|
||||
return 1 ;
|
||||
}
|
||||
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) ;
|
||||
|
||||
while (myCounter == globalCounter)
|
||||
delay (100) ;
|
||||
|
||||
printf (" Done. counter: %5d\n", globalCounter) ;
|
||||
myCounter = globalCounter ;
|
||||
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
WiringPi/examples/lcd-adafruit.c
Normal file
347
WiringPi/examples/lcd-adafruit.c
Normal 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 ;
|
||||
}
|
||||
@@ -4,7 +4,19 @@
|
||||
* This is designed to drive the parallel interface LCD drivers
|
||||
* based in the Hitachi HD44780U controller and compatables.
|
||||
*
|
||||
* Copyright (c) 2012 Gordon Henderson.
|
||||
* 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/
|
||||
@@ -35,94 +47,239 @@
|
||||
#include <wiringPi.h>
|
||||
#include <lcd.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int i, j ;
|
||||
int fd1, fd2 ;
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
char message1 [256] ;
|
||||
char message2 [256] ;
|
||||
char buf1 [30] ;
|
||||
char buf2 [30] ;
|
||||
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 ;
|
||||
|
||||
printf ("Raspberry Pi LCD test program\n") ;
|
||||
char buf [32] ;
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
exit (1) ;
|
||||
if (argc != 4)
|
||||
return usage (argv [0]) ;
|
||||
|
||||
fd1 = lcdInit (4, 20, 4, 8, 9, 4,5,6,7,0,0,0,0) ;
|
||||
fd2 = lcdInit (2, 16, 4, 8, 10, 4,5,6,7,0,0,0,0) ;
|
||||
printf ("Raspberry Pi LCD test\n") ;
|
||||
printf ("=====================\n") ;
|
||||
|
||||
//fd1 = lcdInit (4, 20, 8, 8, 9, 0,1,2,3,4,5,6,7) ;
|
||||
//fd2 = lcdInit (2, 16, 8, 8, 10, 0,1,2,3,4,5,6,7) ;
|
||||
bits = atoi (argv [1]) ;
|
||||
cols = atoi (argv [2]) ;
|
||||
rows = atoi (argv [3]) ;
|
||||
|
||||
if (fd1 == -1)
|
||||
if (!((rows == 1) || (rows == 2) || (rows == 4)))
|
||||
{
|
||||
printf ("lcdInit 1 failed\n") ;
|
||||
return 1 ;
|
||||
fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ;
|
||||
return EXIT_FAILURE ;
|
||||
}
|
||||
|
||||
if (fd2 == -1)
|
||||
if (!((cols == 16) || (cols == 20)))
|
||||
{
|
||||
printf ("lcdInit 2 failed\n") ;
|
||||
return 1 ;
|
||||
fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
|
||||
return EXIT_FAILURE ;
|
||||
}
|
||||
|
||||
sleep (1) ;
|
||||
wiringPiSetup () ;
|
||||
|
||||
lcdPosition (fd1, 0, 0) ; lcdPuts (fd1, " Gordon Henderson") ;
|
||||
lcdPosition (fd1, 0, 1) ; lcdPuts (fd1, " --------------") ;
|
||||
/*
|
||||
lcdPosition (fd1, 0, 2) ; lcdPuts (fd1, " 00:00:00") ;
|
||||
lcdPosition (fd1, 0, 3) ; lcdPuts (fd1, " DD:MM:YY") ;
|
||||
*/
|
||||
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) ;
|
||||
|
||||
lcdPosition (fd2, 0, 0) ; lcdPuts (fd2, "Gordon Henderson") ;
|
||||
lcdPosition (fd2, 0, 1) ; lcdPuts (fd2, "----------------") ;
|
||||
if (lcdHandle < 0)
|
||||
{
|
||||
fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
sleep (2) ;
|
||||
lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;
|
||||
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
|
||||
|
||||
sprintf (message1, "%s", " http://projects.drogon.net/ ") ;
|
||||
sprintf (message2, "%s", " This is a long message to go into the smaller display just for a demonstration of what we can do. ") ;
|
||||
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 (;;)
|
||||
{
|
||||
i = 0 ;
|
||||
j = 0 ;
|
||||
for (;;)
|
||||
{
|
||||
strncpy (buf1, &message1 [i], 20) ;
|
||||
buf1 [20] = 0 ;
|
||||
lcdPosition (fd1, 0, 1) ;
|
||||
lcdPuts (fd1, buf1) ;
|
||||
++i ;
|
||||
if (i == strlen (message1) - 20)
|
||||
i = 0 ;
|
||||
scrollMessage (0, cols) ;
|
||||
|
||||
if (rows == 1)
|
||||
continue ;
|
||||
|
||||
strncpy (buf2, &message2 [j], 16) ;
|
||||
buf2 [16] = 0 ;
|
||||
lcdPosition (fd2, 0, 1) ;
|
||||
lcdPuts (fd2, buf2) ;
|
||||
++j ;
|
||||
if (j == strlen (message2) - 16)
|
||||
j = 0 ;
|
||||
tim = time (NULL) ;
|
||||
t = localtime (&tim) ;
|
||||
|
||||
tim = time (NULL) ;
|
||||
t = localtime (&tim) ;
|
||||
sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
|
||||
|
||||
sprintf (buf1, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
|
||||
lcdPosition (fd1, 5, 2) ;
|
||||
lcdPuts (fd1, buf1) ;
|
||||
lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
|
||||
lcdPuts (lcdHandle, buf) ;
|
||||
|
||||
sprintf (buf1, "%02d/%02d/%02d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;
|
||||
lcdPosition (fd1, 4, 3) ;
|
||||
lcdPuts (fd1, buf1) ;
|
||||
if (rows == 2)
|
||||
continue ;
|
||||
|
||||
delay (250) ;
|
||||
}
|
||||
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 ;
|
||||
|
||||
@@ -39,27 +39,26 @@
|
||||
#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) ;
|
||||
|
||||
if (wiringPiSetupGpio () < 0)
|
||||
{
|
||||
fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ;
|
||||
return 1 ;
|
||||
}
|
||||
|
||||
softPwmCreate (OK_LED, 0, 100) ;
|
||||
|
||||
for (;;)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/*
|
||||
* pwm.c:
|
||||
* Test of the software PWM driver. Needs 12 LEDs connected
|
||||
* to the Pi.
|
||||
* This tests the hardware PWM channel.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
@@ -23,71 +22,37 @@
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <softPwm.h>
|
||||
|
||||
#define RANGE 100
|
||||
#define NUM_LEDS 12
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ;
|
||||
|
||||
int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ;
|
||||
|
||||
int main ()
|
||||
int main (void)
|
||||
{
|
||||
int i, j ;
|
||||
char buf [80] ;
|
||||
int bright ;
|
||||
|
||||
printf ("Raspberry Pi wiringPi PWM test program\n") ;
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
{
|
||||
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
|
||||
return 1 ;
|
||||
}
|
||||
exit (1) ;
|
||||
|
||||
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) ;
|
||||
|
||||
// Down fast
|
||||
|
||||
for (i = 100 ; i > 0 ; --i)
|
||||
{
|
||||
for (j = 0 ; j < NUM_LEDS ; ++j)
|
||||
softPwmWrite (ledMap [j], i) ;
|
||||
delay (10) ;
|
||||
}
|
||||
|
||||
fgets (buf, 80, stdin) ;
|
||||
pinMode (1, PWM_OUTPUT) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
for (i = 0 ; i < NUM_LEDS ; ++i)
|
||||
softPwmWrite (ledMap [i], values [i]) ;
|
||||
for (bright = 0 ; bright < 1024 ; ++bright)
|
||||
{
|
||||
pwmWrite (1, bright) ;
|
||||
delay (1) ;
|
||||
}
|
||||
|
||||
delay (50) ;
|
||||
|
||||
i = values [0] ;
|
||||
for (j = 0 ; j < NUM_LEDS - 1 ; ++j)
|
||||
values [j] = values [j + 1] ;
|
||||
values [NUM_LEDS - 1] = i ;
|
||||
for (bright = 1023 ; bright >= 0 ; --bright)
|
||||
{
|
||||
pwmWrite (1, bright) ;
|
||||
delay (1) ;
|
||||
}
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
81
WiringPi/examples/q2w/Makefile
Normal file
81
WiringPi/examples/q2w/Makefile
Normal file
@@ -0,0 +1,81 @@
|
||||
#
|
||||
# Makefile:
|
||||
# wiringPi - Wiring Compatable library for the Raspberry Pi
|
||||
# https://projects.drogon.net/wiring-pi
|
||||
#
|
||||
# Copyright (c) 2012-2013 Gordon Henderson
|
||||
#################################################################################
|
||||
# This file is part of wiringPi:
|
||||
# Wiring Compatable library for the Raspberry Pi
|
||||
#
|
||||
# wiringPi is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# wiringPi is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
#################################################################################
|
||||
|
||||
|
||||
#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
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
blink-io: blink-io.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink-io.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
button: button.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ button.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
volts: volts.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ volts.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
bright: bright.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ bright.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
79
WiringPi/examples/q2w/binary.c
Normal file
79
WiringPi/examples/q2w/binary.c
Normal 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
WiringPi/examples/q2w/blink-io.c
Normal file
61
WiringPi/examples/q2w/blink-io.c
Normal 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 ;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* test2.c:
|
||||
* This tests the hardware PWM channel.
|
||||
* blink.c:
|
||||
* Simple "blink" test for the Quick2Wire interface board.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
@@ -22,36 +22,28 @@
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#define LED 1
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int bright ;
|
||||
|
||||
printf ("Raspberry Pi wiringPi PWM test program\n") ;
|
||||
// Enable the on-goard GPIO
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
exit (1) ;
|
||||
wiringPiSetup () ;
|
||||
|
||||
pinMode (1, PWM_OUTPUT) ;
|
||||
printf ("Raspberry Pi - Quick2Wire Mainboard LED Blink Test\n") ;
|
||||
|
||||
pinMode (LED, OUTPUT) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
for (bright = 0 ; bright < 1024 ; ++bright)
|
||||
{
|
||||
pwmWrite (1, bright) ;
|
||||
delay (1) ;
|
||||
}
|
||||
|
||||
for (bright = 1023 ; bright >= 0 ; --bright)
|
||||
{
|
||||
pwmWrite (1, bright) ;
|
||||
delay (1) ;
|
||||
}
|
||||
digitalWrite (LED, HIGH) ;
|
||||
delay (500) ;
|
||||
digitalWrite (LED, LOW) ;
|
||||
delay (500) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
37
WiringPi/examples/q2w/blink.sh
Executable file
37
WiringPi/examples/q2w/blink.sh
Executable file
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# 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
WiringPi/examples/q2w/bright.c
Normal file
59
WiringPi/examples/q2w/bright.c
Normal 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
WiringPi/examples/q2w/button.c
Normal file
63
WiringPi/examples/q2w/button.c
Normal 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
WiringPi/examples/q2w/volts.c
Normal file
62
WiringPi/examples/q2w/volts.c
Normal 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 ;
|
||||
}
|
||||
69
WiringPi/examples/rht03.c
Normal file
69
WiringPi/examples/rht03.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 0
|
||||
|
||||
/*
|
||||
***********************************************************************
|
||||
* The main program
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int temp, rh ;
|
||||
int newTemp, newRh ;
|
||||
|
||||
temp = rh = newTemp = newRh = 0 ;
|
||||
|
||||
wiringPiSetup () ;
|
||||
piHiPri (55) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
delay (100) ;
|
||||
|
||||
if (!readRHT03 (RHT03_PIN, &newTemp, &newRh))
|
||||
continue ;
|
||||
|
||||
if ((temp != newTemp) || (rh != newRh))
|
||||
{
|
||||
temp = newTemp ;
|
||||
rh = newRh ;
|
||||
if ((temp & 0x8000) != 0) // Negative
|
||||
{
|
||||
temp &= 0x7FFF ;
|
||||
temp = -temp ;
|
||||
}
|
||||
printf ("Temp: %5.1f, RH: %5.1f%%\n", temp / 10.0, rh / 10.0) ;
|
||||
}
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
89
WiringPi/examples/softPwm.c
Normal file
89
WiringPi/examples/softPwm.c
Normal 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 ;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* tone.c:
|
||||
* softTone.c:
|
||||
* Test of the softTone module in wiringPi
|
||||
* Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v
|
||||
*
|
||||
@@ -38,11 +38,7 @@ int main ()
|
||||
{
|
||||
int i ;
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
{
|
||||
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
|
||||
return 1 ;
|
||||
}
|
||||
wiringPiSetup () ;
|
||||
|
||||
softToneCreate (PIN) ;
|
||||
|
||||
@@ -55,5 +51,4 @@ int main ()
|
||||
delay (500) ;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,93 +31,65 @@
|
||||
|
||||
#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)
|
||||
{
|
||||
int i ;
|
||||
uint32_t start, end, count, sum, perSec ;
|
||||
|
||||
printf ("Raspberry Pi wiringPi speed test program\n") ;
|
||||
printf ("Raspberry Pi wiringPi GPIO speed test program\n") ;
|
||||
printf ("=============================================\n") ;
|
||||
|
||||
// Start the standard way
|
||||
|
||||
if (wiringPiSetup () == -1)
|
||||
exit (1) ;
|
||||
|
||||
printf ("Native wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
|
||||
|
||||
printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
|
||||
wiringPiSetup () ;
|
||||
pinMode (0, OUTPUT) ;
|
||||
speedTest (0, FAST_COUNT) ;
|
||||
|
||||
sum = 0 ;
|
||||
for (i = 0 ; i < 3 ; ++i)
|
||||
{
|
||||
printf (" Pass: %d: ", i) ;
|
||||
fflush (stdout) ;
|
||||
|
||||
start = millis () ;
|
||||
for (count = 0 ; count < FAST_COUNT ; ++count)
|
||||
digitalWrite (0, 1) ;
|
||||
end = millis () ;
|
||||
printf (" %8dmS\n", end - start) ;
|
||||
sum += (end - start) ;
|
||||
}
|
||||
digitalWrite (0, 0) ;
|
||||
printf (" Average: %8dmS", sum / 3) ;
|
||||
perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
|
||||
printf (": %6d/sec\n", perSec) ;
|
||||
|
||||
|
||||
printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ;
|
||||
|
||||
if (wiringPiSetupGpio () == -1)
|
||||
exit (1) ;
|
||||
// GPIO
|
||||
|
||||
printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ;
|
||||
wiringPiSetupGpio () ;
|
||||
pinMode (17, OUTPUT) ;
|
||||
speedTest (17, FAST_COUNT) ;
|
||||
|
||||
sum = 0 ;
|
||||
for (i = 0 ; i < 3 ; ++i)
|
||||
{
|
||||
printf (" Pass: %d: ", i) ;
|
||||
fflush (stdout) ;
|
||||
|
||||
start = millis () ;
|
||||
for (count = 0 ; count < 10000000 ; ++count)
|
||||
digitalWrite (17, 1) ;
|
||||
end = millis () ;
|
||||
printf (" %8dmS\n", end - start) ;
|
||||
sum += (end - start) ;
|
||||
}
|
||||
digitalWrite (17, 0) ;
|
||||
printf (" Average: %8dmS", sum / 3) ;
|
||||
perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
|
||||
printf (": %6d/sec\n", perSec) ;
|
||||
// Phys
|
||||
|
||||
printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ;
|
||||
wiringPiSetupPhys () ;
|
||||
pinMode (11, OUTPUT) ;
|
||||
speedTest (11, FAST_COUNT) ;
|
||||
|
||||
// Switch to SYS mode:
|
||||
|
||||
if (wiringPiSetupSys () == -1)
|
||||
exit (1) ;
|
||||
|
||||
printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
|
||||
|
||||
sum = 0 ;
|
||||
for (i = 0 ; i < 3 ; ++i)
|
||||
{
|
||||
printf (" Pass: %d: ", i) ;
|
||||
fflush (stdout) ;
|
||||
|
||||
start = millis () ;
|
||||
for (count = 0 ; count < SLOW_COUNT ; ++count)
|
||||
digitalWrite (17, 1) ;
|
||||
end = millis () ;
|
||||
printf (" %8dmS\n", end - start) ;
|
||||
sum += (end - start) ;
|
||||
}
|
||||
digitalWrite (17, 0) ;
|
||||
printf (" Average: %8dmS", sum / 3) ;
|
||||
perSec = (int)(double)SLOW_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
|
||||
printf (": %6d/sec\n", perSec) ;
|
||||
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 ;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#
|
||||
# Makefile:
|
||||
# wiringPi - Wiring Compatable library for the Raspberry Pi
|
||||
# The gpio command:
|
||||
# A swiss-army knige of GPIO shenanigans.
|
||||
# https://projects.drogon.net/wiring-pi
|
||||
#
|
||||
# Copyright (c) 2012 Gordon Henderson
|
||||
# Copyright (c) 2012-2013 Gordon Henderson
|
||||
#################################################################################
|
||||
# This file is part of wiringPi:
|
||||
# Wiring Compatable library for the Raspberry Pi
|
||||
@@ -22,59 +23,65 @@
|
||||
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
#################################################################################
|
||||
|
||||
DESTDIR=/usr
|
||||
PREFIX=/local
|
||||
|
||||
#DEBUG = -g -O0
|
||||
DEBUG = -O2
|
||||
CC = gcc
|
||||
INCLUDE = -I/usr/local/include
|
||||
INCLUDE = -I$(DESTDIR)$(PREFIX)/include
|
||||
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe
|
||||
|
||||
LDFLAGS = -L/usr/local/lib
|
||||
LIBS = -lwiringPi -lpthread -lm
|
||||
LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib
|
||||
LIBS = -lwiringPi -lwiringPiDev -lpthread -lm
|
||||
|
||||
# May not need to alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = gpio.c
|
||||
SRC = gpio.c extensions.c readall.c pins.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
all: gpio
|
||||
|
||||
gpio: gpio.o
|
||||
gpio: $(OBJ)
|
||||
@echo [Link]
|
||||
@$(CC) -o $@ gpio.o $(LDFLAGS) $(LIBS)
|
||||
@$(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS)
|
||||
|
||||
.c.o:
|
||||
@echo [Compile] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONEY: clean
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(OBJ) gpio *~ core tags *.bak
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) gpio *~ core tags *.bak
|
||||
|
||||
.PHONEY: tags
|
||||
.PHONY: tags
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
.PHONEY: install
|
||||
.PHONY: install
|
||||
install:
|
||||
@echo "[Install]"
|
||||
@cp gpio /usr/local/bin
|
||||
@chown root.root /usr/local/bin/gpio
|
||||
@chmod 4755 /usr/local/bin/gpio
|
||||
@mkdir -p /usr/local/man/man1
|
||||
@cp gpio.1 /usr/local/man/man1
|
||||
@cp gpio $(DESTDIR)$(PREFIX)/bin
|
||||
@chown root.root $(DESTDIR)$(PREFIX)/bin/gpio
|
||||
@chmod 4755 $(DESTDIR)$(PREFIX)/bin/gpio
|
||||
@mkdir -p $(DESTDIR)$(PREFIX)/man/man1
|
||||
@cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1
|
||||
|
||||
.PHONEY: uninstall
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
@echo "[UnInstall]"
|
||||
@rm -f /usr/local/bin/gpio
|
||||
@rm -f /usr/local/man/man1/gpio.1
|
||||
@rm -f $(DESTDIR)$(PREFIX)/bin/gpio
|
||||
@rm -f $(DESTDIR)$(PREFIX)/man/man1/gpio.1
|
||||
|
||||
.PHONEY: depend
|
||||
.PHONY: depend
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
gpio.o: extensions.h
|
||||
extensions.o: extensions.h
|
||||
|
||||
700
WiringPi/gpio/extensions.c
Normal file
700
WiringPi/gpio/extensions.c
Normal file
@@ -0,0 +1,700 @@
|
||||
/*
|
||||
* extensions.c:
|
||||
* Part of the GPIO program to test, peek, poke and otherwise
|
||||
* noodle with the GPIO hardware on 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 <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include <mcp23008.h>
|
||||
#include <mcp23016.h>
|
||||
#include <mcp23017.h>
|
||||
#include <mcp23s08.h>
|
||||
#include <mcp23s17.h>
|
||||
#include <sr595.h>
|
||||
#include <pcf8591.h>
|
||||
#include <pcf8574.h>
|
||||
#include <max31855.h>
|
||||
#include <max5322.h>
|
||||
#include <mcp3002.h>
|
||||
#include <mcp3004.h>
|
||||
#include <mcp4802.h>
|
||||
#include <mcp3422.h>
|
||||
#include <sn3218.h>
|
||||
#include <drcSerial.h>
|
||||
|
||||
#include "extensions.h"
|
||||
|
||||
extern int wiringPiDebug ;
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
// Local structure to hold details
|
||||
|
||||
struct extensionFunctionStruct
|
||||
{
|
||||
const char *name ;
|
||||
int (*function)(char *progName, int pinBase, char *params) ;
|
||||
} ;
|
||||
|
||||
|
||||
/*
|
||||
* extractInt:
|
||||
* Check & return an integer at the given location (prefixed by a :)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static char *extractInt (char *progName, char *p, int *num)
|
||||
{
|
||||
if (*p != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected\n", progName) ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
++p ;
|
||||
|
||||
if (!isdigit (*p))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected\n", progName) ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
*num = strtol (p, NULL, 0) ;
|
||||
while (isdigit (*p))
|
||||
++p ;
|
||||
|
||||
return p ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* extractStr:
|
||||
* Check & return a string at the given location (prefixed by a :)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static char *extractStr (char *progName, char *p, char **str)
|
||||
{
|
||||
char *q, *r ;
|
||||
|
||||
if (*p != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected\n", progName) ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
++p ;
|
||||
|
||||
if (!isprint (*p))
|
||||
{
|
||||
fprintf (stderr, "%s: character expected\n", progName) ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
q = p ;
|
||||
while ((*q != 0) && (*q != ':'))
|
||||
++q ;
|
||||
|
||||
*str = r = calloc (q - p + 2, 1) ; // Zeros it
|
||||
|
||||
while (p != q)
|
||||
*r++ = *p++ ;
|
||||
|
||||
return p ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp23008:
|
||||
* MCP23008 - 8-bit I2C GPIO expansion chip
|
||||
* mcp23002:base:i2cAddr
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp23008 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
|
||||
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23008Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp23016:
|
||||
* MCP230016- 16-bit I2C GPIO expansion chip
|
||||
* mcp23016:base:i2cAddr
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp23016 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
|
||||
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23016Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp23017:
|
||||
* MCP230017- 16-bit I2C GPIO expansion chip
|
||||
* mcp23017:base:i2cAddr
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp23017 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
|
||||
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23017Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp23s08:
|
||||
* MCP23s08 - 8-bit SPI GPIO expansion chip
|
||||
* mcp23s08:base:spi:port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp23s08 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi, port ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractInt (progName, params, &port)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((port < 0) || (port > 7))
|
||||
{
|
||||
fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23s08Setup (pinBase, spi, port) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp23s17:
|
||||
* MCP23s17 - 16-bit SPI GPIO expansion chip
|
||||
* mcp23s17:base:spi:port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp23s17 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi, port ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractInt (progName, params, &port)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((port < 0) || (port > 7))
|
||||
{
|
||||
fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23s17Setup (pinBase, spi, port) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionSr595:
|
||||
* Shift Register 74x595
|
||||
* sr595:base:pins:data:clock:latch
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionSr595 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int pins, data, clock, latch ;
|
||||
|
||||
// Extract pins
|
||||
|
||||
if ((params = extractInt (progName, params, &pins)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((pins < 8) || (pins > 32))
|
||||
{
|
||||
fprintf (stderr, "%s: pin count (%d) out of range - 8-32 expected.\n", progName, pins) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractInt (progName, params, &data)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((params = extractInt (progName, params, &clock)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((params = extractInt (progName, params, &latch)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
sr595Setup (pinBase, pins, data, clock, latch) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionPcf8574:
|
||||
* Digital IO (Crude!)
|
||||
* pcf8574:base:i2cAddr
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionPcf8574 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
|
||||
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
pcf8574Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionPcf8591:
|
||||
* Analog IO
|
||||
* pcf8591:base:i2cAddr
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
|
||||
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
pcf8591Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMax31855:
|
||||
* Analog IO
|
||||
* max31855:base:spiChan
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMax31855 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
max31855Setup (pinBase, spi) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp3002:
|
||||
* Analog IO
|
||||
* mcp3002:base:spiChan
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp3002 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp3002Setup (pinBase, spi) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp3004:
|
||||
* Analog IO
|
||||
* mcp3004:base:spiChan
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp3004 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp3004Setup (pinBase, spi) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMax5322:
|
||||
* Analog O
|
||||
* max5322:base:spiChan
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMax5322 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
max5322Setup (pinBase, spi) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp4802:
|
||||
* Analog IO
|
||||
* mcp4802:base:spiChan
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp4802 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp4802Setup (pinBase, spi) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionSn3218:
|
||||
* Analog Output (LED Driver)
|
||||
* sn3218:base
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionSn3218 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
sn3218Setup (pinBase) ;
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp3422:
|
||||
* Analog IO
|
||||
* mcp3422:base:i2cAddr
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c, sampleRate, gain ;
|
||||
|
||||
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractInt (progName, params, &sampleRate)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((sampleRate < 0) || (sampleRate > 3))
|
||||
{
|
||||
fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractInt (progName, params, &gain)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((gain < 0) || (gain > 3))
|
||||
{
|
||||
fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp3422Setup (pinBase, i2c, sampleRate, gain) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
/*
|
||||
* doExtensionDrcS:
|
||||
* Interface to a DRC Serial system
|
||||
* drcs:base:pins:serialPort:baud
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionDrcS (char *progName, int pinBase, char *params)
|
||||
{
|
||||
char *port ;
|
||||
int pins, baud ;
|
||||
|
||||
if ((params = extractInt (progName, params, &pins)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((pins < 1) || (pins > 100))
|
||||
{
|
||||
fprintf (stderr, "%s: pins (%d) out of range (2-100)\n", progName, pins) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractStr (progName, params, &port)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if (strlen (port) == 0)
|
||||
{
|
||||
fprintf (stderr, "%s: serial port device name required\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
if ((params = extractInt (progName, params, &baud)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((baud < 1) || (baud > 4000000))
|
||||
{
|
||||
fprintf (stderr, "%s: baud rate (%d) out of range\n", progName, baud) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
drcSetupSerial (pinBase, pins, port, baud) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Function list
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
struct extensionFunctionStruct extensionFunctions [] =
|
||||
{
|
||||
{ "mcp23008", &doExtensionMcp23008 },
|
||||
{ "mcp23016", &doExtensionMcp23016 },
|
||||
{ "mcp23017", &doExtensionMcp23017 },
|
||||
{ "mcp23s08", &doExtensionMcp23s08 },
|
||||
{ "mcp23s17", &doExtensionMcp23s17 },
|
||||
{ "sr595", &doExtensionSr595 },
|
||||
{ "pcf8574", &doExtensionPcf8574 },
|
||||
{ "pcf8591", &doExtensionPcf8591 },
|
||||
{ "mcp3002", &doExtensionMcp3002 },
|
||||
{ "mcp3004", &doExtensionMcp3004 },
|
||||
{ "mcp4802", &doExtensionMcp4802 },
|
||||
{ "mcp3422", &doExtensionMcp3422 },
|
||||
{ "max31855", &doExtensionMax31855 },
|
||||
{ "max5322", &doExtensionMax5322 },
|
||||
{ "sn3218", &doExtensionSn3218 },
|
||||
{ "drcs", &doExtensionDrcS },
|
||||
{ NULL, NULL },
|
||||
} ;
|
||||
|
||||
|
||||
/*
|
||||
* doExtension:
|
||||
* Load in a wiringPi extension
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int doExtension (char *progName, char *extensionData)
|
||||
{
|
||||
char *p ;
|
||||
char *extension = extensionData ;
|
||||
struct extensionFunctionStruct *extensionFn ;
|
||||
int pinBase = 0 ;
|
||||
|
||||
// Get the extension extension name by finding the first colon
|
||||
|
||||
p = extension ;
|
||||
while (*p != ':')
|
||||
{
|
||||
if (!*p) // ran out of characters
|
||||
{
|
||||
fprintf (stderr, "%s: extension name not terminated by a colon\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
++p ;
|
||||
}
|
||||
|
||||
*p++ = 0 ;
|
||||
|
||||
if (!isdigit (*p))
|
||||
{
|
||||
fprintf (stderr, "%s: pinBase number expected after extension name\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
while (isdigit (*p))
|
||||
{
|
||||
if (pinBase > 1000000000)
|
||||
{
|
||||
fprintf (stderr, "%s: pinBase too large\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
pinBase = pinBase * 10 + (*p - '0') ;
|
||||
++p ;
|
||||
}
|
||||
|
||||
if (pinBase < 64)
|
||||
{
|
||||
fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
// Search for extensions:
|
||||
|
||||
for (extensionFn = extensionFunctions ; extensionFn->name != NULL ; ++extensionFn)
|
||||
{
|
||||
if (strcmp (extensionFn->name, extension) == 0)
|
||||
return extensionFn->function (progName, pinBase, p) ;
|
||||
}
|
||||
|
||||
fprintf (stderr, "%s: extension %s not found\n", progName, extension) ;
|
||||
return FALSE ;
|
||||
}
|
||||
26
WiringPi/gpio/extensions.h
Normal file
26
WiringPi/gpio/extensions.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* extensions.h:
|
||||
* Part of the GPIO program to test, peek, poke and otherwise
|
||||
* noodle with the GPIO hardware on 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
extern int doExtension (char *progName, char *extensionData) ;
|
||||
@@ -1,7 +1,7 @@
|
||||
.TH "GPIO" "21st October 2012" "Command-Line access to Raspberry Pi and PiFace GPIO"
|
||||
.TH "GPIO" "March 2013" "Command-Line access to Raspberry Pi's GPIO"
|
||||
|
||||
.SH NAME
|
||||
gpio \- Command-line access to Raspberry Pi and PiFace GPIO
|
||||
gpio \- Command-line access to Raspberry Pi's GPIO
|
||||
|
||||
.SH SYNOPSIS
|
||||
.B gpio
|
||||
@@ -9,11 +9,15 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO
|
||||
.PP
|
||||
.B gpio
|
||||
.B [ \-g | \-1 ]
|
||||
.B read/write/aread/awrite/wb/pwm/clock/mode ...
|
||||
.B mode/read/write/aread/awrite/wb/pwm/clock ...
|
||||
.PP
|
||||
.B gpio
|
||||
.B [ \-x extension:params ]
|
||||
.B mode/read/write/aread/awrite/pwm/pwmTone ...
|
||||
.PP
|
||||
.B gpio
|
||||
.B [ \-p ]
|
||||
.B read/write/wb
|
||||
.B read/write/toggle/wb
|
||||
.B ...
|
||||
.PP
|
||||
.B gpio
|
||||
@@ -27,10 +31,18 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO
|
||||
.B ...
|
||||
.PP
|
||||
.B gpio
|
||||
.B wfi
|
||||
.B ...
|
||||
.PP
|
||||
.B gpio
|
||||
.B drive
|
||||
group value
|
||||
.PP
|
||||
.B gpio
|
||||
.B usbp
|
||||
high | low
|
||||
.PP
|
||||
.B gpio
|
||||
.B pwm-bal/pwm-ms
|
||||
.PP
|
||||
.B gpio
|
||||
@@ -83,6 +95,13 @@ Use the physical pin numbers rather than wiringPi pin numbers.
|
||||
use pins on the Revision 2 P5 connector this way, and as with \-g the
|
||||
BCM_GPIO pin numbers are always used with the export and edge commands.
|
||||
|
||||
.TP
|
||||
.B \-x extension
|
||||
This causes the named extension to be initialised. Extensions
|
||||
comprise of a name (e.g. mcp23017) followed by a colon, then the
|
||||
pin-base, then more optional parameters depending on the extension type.
|
||||
See the web page on http://wiringpi.com/the-gpio-utility/
|
||||
|
||||
.TP
|
||||
.B \-p
|
||||
Use the PiFace interface board and its corresponding pin numbers. The PiFace
|
||||
@@ -99,6 +118,28 @@ respective logic levels.
|
||||
Write the given value (0 or 1) to the pin. You need to set the pin
|
||||
to output mode first.
|
||||
|
||||
.TP
|
||||
.B aread <pin>
|
||||
Read the analog value of the given pin. This needs to be uses in
|
||||
conjunction with a -x flag to add in an extension that handles analog
|
||||
inputs. respective logic levels.
|
||||
|
||||
e.g. gpio -x mcp3002:200:0 aread 200
|
||||
|
||||
will read the first analog input on an mcp3002 SPI ADC chip.
|
||||
|
||||
.TP
|
||||
.B awrite <pin> <value>
|
||||
Write the analog value to the given pin. This needs to be used in
|
||||
conjunction with a -x flag to add in an extension that handles analog
|
||||
inputs. respective logic levels.
|
||||
|
||||
e.g. gpio -x mcp4802:200:0 awrite 200 128
|
||||
|
||||
will write the value 128 to the first DAC port on an mcp4802 chip on
|
||||
the Pi's SPI bus 0.
|
||||
|
||||
|
||||
.TP
|
||||
.B wb <value>
|
||||
Write the given byte to the 8 main GPIO pins. You can prefix it with 0x
|
||||
@@ -111,10 +152,19 @@ Output a table of all GPIO pins values. The values represent the actual values r
|
||||
if the pin is in input mode, or the last value written if the pin is in output
|
||||
mode.
|
||||
|
||||
The readall command is usable with an extension module (via the -x parameter),
|
||||
but it's unable to determine pin modes or states, so will perform both a
|
||||
digital and analog read on each pin in-turn.
|
||||
|
||||
.TP
|
||||
.B reset
|
||||
Resets the GPIO - As much as it's possible to do. All pins are set to input
|
||||
mode and all the internal pull-up/down resistors are disconnected (tristate mode).
|
||||
Resets the GPIO - As much as it's possible to do. All pins are set to
|
||||
input mode and all the internal pull-up/down resistors are disconnected
|
||||
(tristate mode).
|
||||
|
||||
The reset command is usable with an extension module (via the -x parameter),
|
||||
but it's limited to turning the pin into input mode (if applicable) and
|
||||
removing any pull up/down resistor.
|
||||
|
||||
.TP
|
||||
.B pwm <pin> <value>
|
||||
@@ -171,6 +221,12 @@ requiring root/sudo.
|
||||
.B unexport
|
||||
Un-Export a GPIO pin in the /sys/class/gpio directory.
|
||||
|
||||
.TP
|
||||
.B wfi <pin> <mode>
|
||||
This set the given pin to the supplied interrupt mode: rising, falling
|
||||
or both then waits for the interrupt to happen. It's a non-busy wait,
|
||||
so does not consume and CPU while it's waiting.
|
||||
|
||||
.TP
|
||||
.B drive
|
||||
group value
|
||||
@@ -179,6 +235,13 @@ Change the pad driver value for the given pad group to the supplied drive
|
||||
value. Group is 0, 1 or 2 and value is 0-7. Do not use unless you are
|
||||
absolutely sure you know what you're doing.
|
||||
|
||||
.TP
|
||||
.B usbp
|
||||
high | low
|
||||
|
||||
Change the USB current limiter to high (1.2 amps) or low (the default, 600mA)
|
||||
This is only applicable to the model B+
|
||||
|
||||
.TP
|
||||
.B pwm-bal/pwm-ms
|
||||
Change the PWM mode to balanced (the default) or mark:space ratio (traditional)
|
||||
@@ -191,8 +254,8 @@ Change the PWM range register. The default is 1024.
|
||||
.B load i2c [baudrate]
|
||||
This loads the i2c or drivers into the kernel and changes the permissions
|
||||
on the associated /dev/ entries so that the current user has access to
|
||||
them. Optionally it will set the I2C baudrate to that supplied (or as
|
||||
close as the Pi can manage) The default speed is 100Kb/sec.
|
||||
them. Optionally it will set the I2C baudrate to that supplied in Kb/sec
|
||||
(or as close as the Pi can manage) The default speed is 100Kb/sec.
|
||||
|
||||
.TP
|
||||
.B load spi [buffer size in KB]
|
||||
@@ -221,26 +284,26 @@ The board jumpers need to be in-place to do this operation.
|
||||
|
||||
.PP
|
||||
.TS
|
||||
r r r l.
|
||||
WiringPi GPIO-r1 GPIO-r2 Function
|
||||
c c c c l.
|
||||
WiringPi GPIO-r1 GPIO-r2 P1-Phys Function
|
||||
_
|
||||
0 17 17
|
||||
1 18 18 (PWM)
|
||||
2 21 27
|
||||
3 22 22
|
||||
4 23 23
|
||||
5 24 24
|
||||
6 25 25
|
||||
7 4 4
|
||||
8 0 2 I2C: SDA0
|
||||
9 1 3 I2C: SCL0
|
||||
10 8 8 SPI: CE0
|
||||
11 7 7 SPI: CE1
|
||||
12 10 10 SPI: MOSI
|
||||
13 9 9 SPI: MISO
|
||||
14 11 11 SPI: SCLK
|
||||
15 14 14 TxD
|
||||
16 15 16 RxD
|
||||
0 17 17 11
|
||||
1 18 18 12 (PWM)
|
||||
2 21 27 13
|
||||
3 22 22 15
|
||||
4 23 23 16
|
||||
5 24 24 18
|
||||
6 25 25 22
|
||||
7 4 4 7
|
||||
8 0 2 3 I2C: SDA0
|
||||
9 1 3 5 I2C: SCL0
|
||||
10 8 8 24 SPI: CE0
|
||||
11 7 7 26 SPI: CE1
|
||||
12 10 10 19 SPI: MOSI
|
||||
13 9 9 21 SPI: MISO
|
||||
14 11 11 23 SPI: SCLK
|
||||
15 14 14 8 TxD
|
||||
16 15 16 10 RxD
|
||||
17 - 28
|
||||
18 - 29
|
||||
19 - 30
|
||||
@@ -286,7 +349,7 @@ pin numbers.
|
||||
.LP
|
||||
WiringPi's home page
|
||||
.IP
|
||||
https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
http://wiringpi.com/
|
||||
|
||||
.SH AUTHOR
|
||||
|
||||
@@ -298,7 +361,7 @@ Please report bugs to <projects@drogon.net>
|
||||
|
||||
.SH COPYRIGHT
|
||||
|
||||
Copyright (c) 2012 Gordon Henderson
|
||||
Copyright (c) 2012-2013 Gordon Henderson
|
||||
This is free software; see the source for copying conditions. There is NO
|
||||
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
|
||||
|
||||
@@ -30,232 +30,74 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include <gertboard.h>
|
||||
#include <piFace.h>
|
||||
#include <sr595.h>
|
||||
#include <mcp23008.h>
|
||||
#include <mcp23017.h>
|
||||
#include <mcp23s08.h>
|
||||
#include <mcp23s17.h>
|
||||
|
||||
#include "extensions.h"
|
||||
|
||||
extern int wiringPiDebug ;
|
||||
|
||||
// External functions I can't be bothered creating a separate .h file for:
|
||||
|
||||
extern void doReadall (void) ;
|
||||
extern void doPins (void) ;
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
#define VERSION "2.00"
|
||||
#define VERSION "2.20"
|
||||
#define PI_USB_POWER_CONTROL 38
|
||||
#define I2CDETECT "/usr/sbin/i2cdetect"
|
||||
|
||||
static int wpMode ;
|
||||
int wpMode ;
|
||||
|
||||
char *usage = "Usage: gpio -v\n"
|
||||
" gpio -h\n"
|
||||
" gpio [-g|-1] [-x module:params] ...\n"
|
||||
" gpio [-g|-1] [-x extension:params] ...\n"
|
||||
" gpio [-p] <read/write/wb> ...\n"
|
||||
" gpio <read/write/aread/awritewb/pwm/clock/mode> ...\n"
|
||||
" gpio readall/reset\n"
|
||||
" gpio unexportall/exports\n"
|
||||
" gpio export/edge/unexport ...\n"
|
||||
" gpio wfi <pin> <mode>\n"
|
||||
" gpio drive <group> <value>\n"
|
||||
" gpio pwm-bal/pwm-ms \n"
|
||||
" gpio pwmr <range> \n"
|
||||
" gpio pwmc <divider> \n"
|
||||
" gpio load spi/i2c\n"
|
||||
" gpio i2cd/i2cdetect\n"
|
||||
" gpio usbp high/low\n"
|
||||
" gpio gbr <channel>\n"
|
||||
" gpio gbw <channel> <value>" ; // No trailing newline needed here.
|
||||
|
||||
struct moduleFunctionStruct
|
||||
|
||||
#ifdef NOT_FOR_NOW
|
||||
/*
|
||||
* decodePin:
|
||||
* Decode a pin "number" which can actually be a pin name to represent
|
||||
* one of the Pi's on-board pins.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int decodePin (const char *str)
|
||||
{
|
||||
const char *name ;
|
||||
int (*function)(char *progName, int pinBase, char *params) ;
|
||||
} ;
|
||||
|
||||
static int doModuleMcp23008 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
// The first case - see if it's a number:
|
||||
|
||||
// Extract the I2C address:
|
||||
if (isdigit (str [0]))
|
||||
return atoi (str) ;
|
||||
|
||||
if (*params != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
++params ;
|
||||
if (!isdigit (*params))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
i2c = strtol (params, NULL, 0) ;
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23008Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
static int doModuleMcp23017 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int i2c ;
|
||||
|
||||
// Extract the I2C address:
|
||||
|
||||
if (*params != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
++params ;
|
||||
if (!isdigit (*params))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
i2c = strtol (params, NULL, 0) ;
|
||||
if ((i2c < 0x03) || (i2c > 0x77))
|
||||
{
|
||||
fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23017Setup (pinBase, i2c) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
static int doModuleMcp23s08 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi, port ;
|
||||
|
||||
// Extract the SPI address:
|
||||
|
||||
if (*params != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
++params ;
|
||||
if (!isdigit (*params))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
spi = *params - '0' ;
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
// Extract the port:
|
||||
|
||||
if (*++params != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected after SPI address\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
++params ;
|
||||
if (!isdigit (*params))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected after SPI address\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
port = strtol (params, NULL, 0) ;
|
||||
if ((port < 0) || (port > 7))
|
||||
{
|
||||
fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23s08Setup (pinBase, spi, port) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
static int doModuleMcp23s17 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi, port ;
|
||||
|
||||
// Extract the SPI address:
|
||||
|
||||
if (*params != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
++params ;
|
||||
if (!isdigit (*params))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected after pin-base number\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
spi = *params - '0' ;
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
// Extract the port:
|
||||
|
||||
if (*++params != ':')
|
||||
{
|
||||
fprintf (stderr, "%s: colon expected after SPI address\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
++params ;
|
||||
if (!isdigit (*params))
|
||||
{
|
||||
fprintf (stderr, "%s: digit expected after SPI address\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
port = strtol (params, NULL, 0) ;
|
||||
if ((port < 0) || (port > 7))
|
||||
{
|
||||
fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
mcp23s17Setup (pinBase, spi, port) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
struct moduleFunctionStruct moduleFunctions [] =
|
||||
{
|
||||
{ "mcp23008", &doModuleMcp23008 },
|
||||
{ "mcp23017", &doModuleMcp23017 },
|
||||
{ "mcp23s08", &doModuleMcp23s08 },
|
||||
{ "mcp23s17", &doModuleMcp23s17 },
|
||||
{ NULL, NULL },
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ -392,58 +234,33 @@ static void doLoad (int argc, char *argv [])
|
||||
|
||||
|
||||
/*
|
||||
* doReadall:
|
||||
* Read all the GPIO pins
|
||||
* doI2Cdetect:
|
||||
* Run the i2cdetect command with the right runes for this Pi revision
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static char *pinNames [] =
|
||||
static void doI2Cdetect (int argc, char *argv [])
|
||||
{
|
||||
"GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
|
||||
"SDA ", "SCL ",
|
||||
"CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
|
||||
"TxD ", "RxD ",
|
||||
"GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
|
||||
} ;
|
||||
int port = piBoardRev () == 1 ? 0 : 1 ;
|
||||
char command [128] ;
|
||||
struct stat statBuf ;
|
||||
|
||||
static char *alts [] =
|
||||
{
|
||||
"IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
|
||||
} ;
|
||||
|
||||
static int wpiToPhys [64] =
|
||||
{
|
||||
11, 12, 13, 15, 16, 18, 22, 7, // 0...7
|
||||
3, 5, // 8...9
|
||||
24, 26, 19, 21, 23, // 10..14
|
||||
8, 10, // 15..16
|
||||
3, 4, 5, 6, // 17..20
|
||||
0,0,0,0,0,0,0,0,0,0,0, // 20..31
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
|
||||
} ;
|
||||
|
||||
static void doReadall (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
|
||||
printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
|
||||
printf ("+----------+------+------+--------+------+-------+\n") ;
|
||||
|
||||
for (pin = 0 ; pin < 64 ; ++pin)
|
||||
if (stat (I2CDETECT, &statBuf) < 0)
|
||||
{
|
||||
if (wpiPinToGpio (pin) == -1)
|
||||
continue ;
|
||||
|
||||
printf ("| %6d | %3d | %3d | %s | %s | %s |\n",
|
||||
pin, wpiPinToGpio (pin), wpiToPhys [pin],
|
||||
pinNames [pin],
|
||||
alts [getAlt (pin)],
|
||||
digitalRead (pin) == HIGH ? "High" : "Low ") ;
|
||||
fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
|
||||
return ;
|
||||
}
|
||||
|
||||
printf ("+----------+------+------+--------+------+-------+\n") ;
|
||||
if (!moduleLoaded ("i2c_dev"))
|
||||
{
|
||||
fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
|
||||
return ;
|
||||
}
|
||||
|
||||
sprintf (command, "%s -y %d", I2CDETECT, port) ;
|
||||
if (system (command) < 0)
|
||||
fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -460,9 +277,7 @@ static void doExports (int argc, char *argv [])
|
||||
char fName [128] ;
|
||||
char buf [16] ;
|
||||
|
||||
// Rather crude, but who knows what others are up to...
|
||||
|
||||
for (first = 0, i = 0 ; i < 64 ; ++i)
|
||||
for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
|
||||
{
|
||||
|
||||
// Try to read the direction
|
||||
@@ -594,6 +409,52 @@ void doExport (int argc, char *argv [])
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doWfi:
|
||||
* gpio wfi pin mode
|
||||
* Wait for Interrupt on a given pin.
|
||||
* Slight cheat here - it's easier to actually use ISR now (which calls
|
||||
* gpio to set the pin modes!) then we simply sleep, and expect the thread
|
||||
* to exit the program. Crude but effective.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void wfi (void)
|
||||
{ exit (0) ; }
|
||||
|
||||
void doWfi (int argc, char *argv [])
|
||||
{
|
||||
int pin, mode ;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
pin = atoi (argv [2]) ;
|
||||
|
||||
/**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
|
||||
else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
|
||||
else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
if (wiringPiISR (pin, mode, &wfi) < 0)
|
||||
{
|
||||
fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
delay (9999) ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* doEdge:
|
||||
* gpio edge pin mode
|
||||
@@ -725,6 +586,24 @@ void doUnexportall (char *progName)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doResetExternal:
|
||||
* Load readallExternal, we try to do this with an external device.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void doResetExternal (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
|
||||
{
|
||||
pinMode (pin, INPUT) ;
|
||||
pullUpDnControl (pin, PUD_OFF) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doReset:
|
||||
* Reset the GPIO pins - as much as we can do
|
||||
@@ -735,16 +614,21 @@ static void doReset (char *progName)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
doUnexportall (progName) ;
|
||||
|
||||
for (pin = 0 ; pin < 64 ; ++pin)
|
||||
if (wiringPiNodes != NULL) // External reset
|
||||
doResetExternal () ;
|
||||
else
|
||||
{
|
||||
if (wpiPinToGpio (pin) == -1)
|
||||
continue ;
|
||||
doUnexportall (progName) ;
|
||||
|
||||
digitalWrite (pin, LOW) ;
|
||||
pinMode (pin, INPUT) ;
|
||||
pullUpDnControl (pin, PUD_OFF) ;
|
||||
for (pin = 0 ; pin < 64 ; ++pin)
|
||||
{
|
||||
if (wpiPinToGpio (pin) == -1)
|
||||
continue ;
|
||||
|
||||
digitalWrite (pin, LOW) ;
|
||||
pinMode (pin, INPUT) ;
|
||||
pullUpDnControl (pin, PUD_OFF) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,13 +654,26 @@ void doMode (int argc, char *argv [])
|
||||
|
||||
mode = argv [3] ;
|
||||
|
||||
/**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
|
||||
else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
|
||||
else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
|
||||
else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
|
||||
else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
|
||||
else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
|
||||
else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
|
||||
/**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
|
||||
else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
|
||||
else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
|
||||
else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
|
||||
else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
|
||||
else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
|
||||
else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
|
||||
else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
|
||||
else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
|
||||
else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
|
||||
else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
|
||||
|
||||
// Undocumented
|
||||
|
||||
else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
|
||||
else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
|
||||
else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
|
||||
else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
|
||||
else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
|
||||
else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
|
||||
@@ -820,6 +717,58 @@ static void doPadDrive (int argc, char *argv [])
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doUsbP:
|
||||
* Control USB Power - High (1.2A) or Low (600mA)
|
||||
* gpio usbp high/low
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void doUsbP (int argc, char *argv [])
|
||||
{
|
||||
int model, rev, mem, maker, overVolted ;
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
// Make sure we're on a B+
|
||||
|
||||
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
|
||||
|
||||
if (model != PI_MODEL_BP)
|
||||
{
|
||||
fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
// Need to force BCM_GPIO mode:
|
||||
|
||||
wiringPiSetupGpio () ;
|
||||
|
||||
if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
|
||||
{
|
||||
digitalWrite (PI_USB_POWER_CONTROL, 1) ;
|
||||
pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
|
||||
printf ("Switched to HIGH current USB (1.2A)\n") ;
|
||||
return ;
|
||||
}
|
||||
|
||||
if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
|
||||
{
|
||||
digitalWrite (PI_USB_POWER_CONTROL, 0) ;
|
||||
pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
|
||||
printf ("Switched to LOW current USB (600mA)\n") ;
|
||||
return ;
|
||||
}
|
||||
|
||||
fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doGbw:
|
||||
* gpio gbw channel value
|
||||
@@ -833,7 +782,7 @@ static void doGbw (int argc, char *argv [])
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
|
||||
fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
@@ -842,13 +791,13 @@ static void doGbw (int argc, char *argv [])
|
||||
|
||||
if ((channel < 0) || (channel > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
|
||||
fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
if ((value < 0) || (value > 1023))
|
||||
if ((value < 0) || (value > 255))
|
||||
{
|
||||
fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
|
||||
fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
@@ -883,7 +832,7 @@ static void doGbr (int argc, char *argv [])
|
||||
|
||||
if ((channel < 0) || (channel > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
|
||||
fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
@@ -992,7 +941,6 @@ void doRead (int argc, char *argv [])
|
||||
}
|
||||
|
||||
pin = atoi (argv [2]) ;
|
||||
|
||||
val = digitalRead (pin) ;
|
||||
|
||||
printf ("%s\n", val == 0 ? "0" : "1") ;
|
||||
@@ -1007,19 +955,58 @@ void doRead (int argc, char *argv [])
|
||||
|
||||
void doAread (int argc, char *argv [])
|
||||
{
|
||||
int pin, val ;
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
printf ("%d\n", analogRead (atoi (argv [2]))) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doToggle:
|
||||
* Toggle an IO pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void doToggle (int argc, char *argv [])
|
||||
{
|
||||
int pin ;
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
pin = atoi (argv [2]) ;
|
||||
|
||||
val = analogRead (pin) ;
|
||||
digitalWrite (pin, !digitalRead (pin)) ;
|
||||
}
|
||||
|
||||
printf ("%s\n", val == 0 ? "0" : "1") ;
|
||||
|
||||
/*
|
||||
* doPwmTone:
|
||||
* Output a tone in a PWM pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void doPwmTone (int argc, char *argv [])
|
||||
{
|
||||
int pin, freq ;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
pin = atoi (argv [2]) ;
|
||||
freq = atoi (argv [3]) ;
|
||||
|
||||
pwmToneWrite (pin, freq) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -1125,72 +1112,6 @@ static void doPwmClock (int argc, char *argv [])
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doModule:
|
||||
* Load in a wiringPi extension module
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doModule (char *progName, char *moduleData)
|
||||
{
|
||||
char *p ;
|
||||
char *module = moduleData ;
|
||||
struct moduleFunctionStruct *modFn ;
|
||||
int pinBase = 0 ;
|
||||
|
||||
// Get the module name by finding the first :
|
||||
|
||||
p = module ;
|
||||
while (*p != ':')
|
||||
{
|
||||
if (!*p) // ran out of characters
|
||||
{
|
||||
fprintf (stderr, "%s: module name not terminated by a colon\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
++p ;
|
||||
}
|
||||
|
||||
*p++ = 0 ;
|
||||
|
||||
if (!isdigit (*p))
|
||||
{
|
||||
fprintf (stderr, "%s: pinBase number expected after module name\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
while (isdigit (*p))
|
||||
{
|
||||
if (pinBase > 1000000000)
|
||||
{
|
||||
fprintf (stderr, "%s: pinBase too large\n", progName) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
pinBase = pinBase * 10 + (*p - '0') ;
|
||||
++p ;
|
||||
}
|
||||
|
||||
if (pinBase < 64)
|
||||
{
|
||||
fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
// Search for modules:
|
||||
|
||||
for (modFn = moduleFunctions ; modFn->name != NULL ; ++modFn)
|
||||
{
|
||||
if (strcmp (modFn->name, module) == 0)
|
||||
return modFn->function (progName, pinBase, p) ;
|
||||
}
|
||||
|
||||
fprintf (stderr, "%s: module %s not found\n", progName, module) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* main:
|
||||
* Start here
|
||||
@@ -1200,6 +1121,7 @@ static int doModule (char *progName, char *moduleData)
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
int i ;
|
||||
int model, rev, mem, maker, overVolted ;
|
||||
|
||||
if (getenv ("WIRINGPI_DEBUG") != NULL)
|
||||
{
|
||||
@@ -1213,27 +1135,57 @@ int main (int argc, char *argv [])
|
||||
return 1 ;
|
||||
}
|
||||
|
||||
// Help
|
||||
|
||||
if (strcasecmp (argv [1], "-h") == 0)
|
||||
{
|
||||
printf ("%s: %s\n", argv [0], usage) ;
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
if (strcasecmp (argv [1], "-v") == 0)
|
||||
// Sort of a special:
|
||||
|
||||
if (strcmp (argv [1], "-R") == 0)
|
||||
{
|
||||
printf ("%d\n", piBoardRev ()) ;
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
// Version & Warranty
|
||||
|
||||
if (strcmp (argv [1], "-V") == 0)
|
||||
{
|
||||
printf ("%d\n", piBoardRev ()) ;
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
if (strcmp (argv [1], "-v") == 0)
|
||||
{
|
||||
printf ("gpio version: %s\n", VERSION) ;
|
||||
printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
|
||||
printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
|
||||
printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
|
||||
printf ("For details type: %s -warranty\n", argv [0]) ;
|
||||
printf ("\n") ;
|
||||
printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
|
||||
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
|
||||
if (model == PI_MODEL_UNKNOWN)
|
||||
{
|
||||
printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
|
||||
printf (" projects@drogon.net\n") ;
|
||||
printf ("with a copy of your /proc/cpuinfo if possible\n") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf ("Raspberry Pi Details:\n") ;
|
||||
printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
|
||||
piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
if (strcasecmp (argv [1], "-warranty") == 0)
|
||||
{
|
||||
printf ("gpio version: %s\n", VERSION) ;
|
||||
printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
|
||||
printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
|
||||
printf ("\n") ;
|
||||
printf (" This program is free software; you can redistribute it and/or modify\n") ;
|
||||
printf (" it under the terms of the GNU Leser General Public License as published\n") ;
|
||||
@@ -1318,17 +1270,17 @@ int main (int argc, char *argv [])
|
||||
wpMode = WPI_MODE_PINS ;
|
||||
}
|
||||
|
||||
// Check for -x argument to load in a new module
|
||||
// Check for -x argument to load in a new extension
|
||||
|
||||
if (strcasecmp (argv [1], "-x") == 0)
|
||||
{
|
||||
if (argc < 3)
|
||||
{
|
||||
fprintf (stderr, "%s: -x missing module specification.\n", argv [0]) ;
|
||||
fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
|
||||
exit (EXIT_FAILURE) ;
|
||||
}
|
||||
|
||||
if (!doModule (argv [0], argv [2])) // Prints its own error messages
|
||||
if (!doExtension (argv [0], argv [2])) // Prints its own error messages
|
||||
exit (EXIT_FAILURE) ;
|
||||
|
||||
for (i = 3 ; i < argc ; ++i)
|
||||
@@ -1351,17 +1303,28 @@ int main (int argc, char *argv [])
|
||||
else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
|
||||
|
||||
// GPIO Nicies
|
||||
|
||||
else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
|
||||
|
||||
// Pi Specifics
|
||||
|
||||
else if (strcasecmp (argv [1], "pwm-bal") == 0) doPwmMode (PWM_MODE_BAL) ;
|
||||
else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
|
||||
else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "readall") == 0) doReadall () ;
|
||||
else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
|
||||
else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
|
||||
else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
|
||||
else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
|
||||
else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
|
||||
else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ;
|
||||
else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
|
||||
else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
|
||||
|
||||
33
WiringPi/gpio/pins.c
Normal file
33
WiringPi/gpio/pins.c
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* pins.c:
|
||||
* Just display a handy Pi pinnout diagram.
|
||||
* 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>
|
||||
|
||||
void doPins (void)
|
||||
{
|
||||
printf ("The pins command has been deprecated - sorry. Please use the\n") ;
|
||||
printf (" gpio readall\n") ;
|
||||
printf ("command to get a list of the pinnouts for your Pi.\n") ;
|
||||
}
|
||||
|
||||
193
WiringPi/gpio/pintest
Executable file
193
WiringPi/gpio/pintest
Executable file
@@ -0,0 +1,193 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# pintest
|
||||
# Test the Pi's GPIO port
|
||||
# Copyright (c) 2013 Gordon Henderson
|
||||
#################################################################################
|
||||
# This file is part of wiringPi:
|
||||
# Wiring Compatable library for the Raspberry Pi
|
||||
#
|
||||
# wiringPi is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# wiringPi is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
#################################################################################
|
||||
|
||||
|
||||
# logErr pin, expected got
|
||||
################################################################################
|
||||
|
||||
logErr ()
|
||||
{
|
||||
if [ $errs = 0 ]; then
|
||||
echo ""
|
||||
fi
|
||||
echo " --> Pin $1 failure. Expected $2, got $3"
|
||||
let errs+=1
|
||||
}
|
||||
|
||||
|
||||
# printErrorCount
|
||||
################################################################################
|
||||
|
||||
printErrCount()
|
||||
{
|
||||
if [ $errs = 0 ]; then
|
||||
echo "No faults detected."
|
||||
elif [ $errs = 1 ]; then
|
||||
echo "One fault detected."
|
||||
else
|
||||
echo "$errs faults detected"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# testPins start end
|
||||
################################################################################
|
||||
|
||||
testPins()
|
||||
{
|
||||
start=$1
|
||||
end=$2
|
||||
errs=0
|
||||
|
||||
printf "%30s %2d:%2d: " "$3" $1 $2
|
||||
|
||||
# Set range to inputs
|
||||
|
||||
for i in `seq $start $end`; do
|
||||
gpio mode $i in
|
||||
done
|
||||
|
||||
# Enable internal pull-ups and expect to read high
|
||||
|
||||
for i in `seq $start $end`; do
|
||||
gpio mode $i up
|
||||
if [ `gpio read $i` = 0 ]; then
|
||||
logErr $i 1 0
|
||||
fi
|
||||
done
|
||||
|
||||
# Enable internal pull-downs and expect to read low
|
||||
|
||||
for i in `seq $start $end`; do
|
||||
gpio mode $i down
|
||||
if [ `gpio read $i` = 1 ]; then
|
||||
echo "Pin $i failure - expected 0, got 1"
|
||||
let errs+=1
|
||||
fi
|
||||
done
|
||||
|
||||
# Remove the internal pull up/downs
|
||||
|
||||
for i in `seq $start $end`; do
|
||||
gpio mode $i tri
|
||||
done
|
||||
|
||||
if [ $errs = 0 ]; then
|
||||
echo " OK"
|
||||
else
|
||||
printErrCount
|
||||
fi
|
||||
|
||||
let totErrs+=errs
|
||||
}
|
||||
|
||||
|
||||
intro()
|
||||
{
|
||||
revision=`gpio -V`
|
||||
cat <<EOF
|
||||
PinTest
|
||||
=======
|
||||
|
||||
This is a simple utility to test the GPIO pins on your revision $revision
|
||||
Raspberry Pi.
|
||||
|
||||
NOTE: All GPIO peripherals must be removed to perform this test. This
|
||||
includes serial, I2C and SPI connections. You may get incorrect results
|
||||
if something is connected and it interferes with the test.
|
||||
|
||||
This test can only test the input side of things. It uses the internal
|
||||
pull-up and pull-down resistors to simulate inputs. It does not test
|
||||
the output drivers.
|
||||
|
||||
You will need to reboot your Pi after this test if you wish to use the
|
||||
serial port as it will be left in GPIO mode rather than serial mode.
|
||||
|
||||
Please make sure everything is removed and press the ENTER key to continue,
|
||||
EOF
|
||||
|
||||
echo -n "or Control-C to abort... "
|
||||
read a
|
||||
}
|
||||
|
||||
|
||||
# Start here
|
||||
################################################################################
|
||||
|
||||
intro
|
||||
gpio unexportall
|
||||
gpio reset
|
||||
|
||||
errs=0
|
||||
totErrs=0
|
||||
|
||||
echo ""
|
||||
|
||||
# Main pins
|
||||
|
||||
testPins 0 7 "The main 8 GPIO pins"
|
||||
|
||||
# P5 pins, if a rev 2:
|
||||
|
||||
if [ $revision = 2 ]; then
|
||||
testPins 17 20 "The 4 pins on the P5 connector"
|
||||
fi
|
||||
|
||||
# SPI
|
||||
|
||||
testPins 10 14 "The 5 SPI pins"
|
||||
|
||||
# Serial
|
||||
|
||||
testPins 15 16 "The serial pins"
|
||||
|
||||
# I2C - Needs somewhat different testing
|
||||
# due to the on-board pull-up's
|
||||
|
||||
echo -n " The I2C pins 8: 9: "
|
||||
errs=0
|
||||
gpio mode 8 in
|
||||
gpio mode 9 in
|
||||
|
||||
if [ `gpio read 8` = 0 ]; then
|
||||
echo "Pin 8 failure - expected 1, got 0"
|
||||
let errs+=1
|
||||
fi
|
||||
|
||||
if [ `gpio read 9` = 0 ]; then
|
||||
echo "Pin 9 failure - expected 1, got 0"
|
||||
let errs+=1
|
||||
fi
|
||||
|
||||
if [ $errs = 0 ]; then
|
||||
echo " OK"
|
||||
else
|
||||
printErrCount
|
||||
fi
|
||||
|
||||
echo ""
|
||||
if [ $totErrs != 0 ]; then
|
||||
echo ""
|
||||
echo "Faults detected! Output of 'readall':"
|
||||
gpio readall
|
||||
fi
|
||||
321
WiringPi/gpio/readall.c
Normal file
321
WiringPi/gpio/readall.c
Normal file
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
* readall.c:
|
||||
* The readall functions - getting a bit big, so split them out.
|
||||
* 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 <ctype.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
extern int wpMode ;
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* doReadallExternal:
|
||||
* A relatively crude way to read the pins on an external device.
|
||||
* We don't know the input/output mode of pins, but we can tell
|
||||
* if it's an analog pin or a digital one...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void doReadallExternal (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
printf ("+------+---------+--------+\n") ;
|
||||
printf ("| Pin | Digital | Analog |\n") ;
|
||||
printf ("+------+---------+--------+\n") ;
|
||||
|
||||
for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
|
||||
printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
|
||||
|
||||
printf ("+------+---------+--------+\n") ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doReadall:
|
||||
* Read all the GPIO pins
|
||||
* We also want to use this to read the state of pins on an externally
|
||||
* connected device, so we need to do some fiddling with the internal
|
||||
* wiringPi node structures - since the gpio command can only use
|
||||
* one external device at a time, we'll use that to our advantage...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static char *alts [] =
|
||||
{
|
||||
"IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
|
||||
} ;
|
||||
|
||||
static int physToWpi [64] =
|
||||
{
|
||||
-1, // 0
|
||||
-1, -1, // 1, 2
|
||||
8, -1,
|
||||
9, -1,
|
||||
7, 15,
|
||||
-1, 16,
|
||||
0, 1,
|
||||
2, -1,
|
||||
3, 4,
|
||||
-1, 5,
|
||||
12, -1,
|
||||
13, 6,
|
||||
14, 10,
|
||||
-1, 11, // 25, 26
|
||||
30, 31, // Actually I2C, but not used
|
||||
21, -1,
|
||||
22, 26,
|
||||
23, -1,
|
||||
24, 27,
|
||||
25, 28,
|
||||
-1, 29,
|
||||
-1, -1,
|
||||
-1, -1,
|
||||
-1, -1,
|
||||
-1, -1,
|
||||
-1, -1,
|
||||
17, 18,
|
||||
19, 20,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1
|
||||
} ;
|
||||
|
||||
static char *physNames [64] =
|
||||
{
|
||||
NULL,
|
||||
|
||||
" 3.3v", "5v ",
|
||||
" SDA.1", "5V ",
|
||||
" SCL.1", "0v ",
|
||||
"GPIO. 7", "TxD ",
|
||||
" 0v", "RxD ",
|
||||
"GPIO. 0", "GPIO. 1",
|
||||
"GPIO. 2", "0v ",
|
||||
"GPIO. 3", "GPIO. 4",
|
||||
" 3.3v", "GPIO. 5",
|
||||
" MOSI", "0v ",
|
||||
" MISO", "GPIO. 6",
|
||||
" SCLK", "CE0 ",
|
||||
" 0v", "CE1 ",
|
||||
" SDA.0", "SCL.0 ",
|
||||
"GPIO.21", "0v ",
|
||||
"GPIO.22", "GPIO.26",
|
||||
"GPIO.23", "0v ",
|
||||
"GPIO.24", "GPIO.27",
|
||||
"GPIO.25", "GPIO.28",
|
||||
" 0v", "GPIO.29",
|
||||
NULL, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL,
|
||||
"GPIO.17", "GPIO.18",
|
||||
"GPIO.19", "GPIO.20",
|
||||
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
|
||||
} ;
|
||||
|
||||
|
||||
/*
|
||||
* readallPhys:
|
||||
* Given a physical pin output the data on it and the next pin:
|
||||
*| BCM | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi | BCM |
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void readallPhys (int physPin)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
if (physPinToGpio (physPin) == -1)
|
||||
printf (" | | ") ;
|
||||
else
|
||||
printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]) ;
|
||||
|
||||
printf (" | %s", physNames [physPin]) ;
|
||||
|
||||
if (physToWpi [physPin] == -1)
|
||||
printf (" | | ") ;
|
||||
else
|
||||
{
|
||||
/**/ if (wpMode == WPI_MODE_GPIO)
|
||||
pin = physPinToGpio (physPin) ;
|
||||
else if (wpMode == WPI_MODE_PHYS)
|
||||
pin = physPin ;
|
||||
else
|
||||
pin = physToWpi [physPin] ;
|
||||
|
||||
printf (" | %4s", alts [getAlt (pin)]) ;
|
||||
printf (" | %d", digitalRead (pin)) ;
|
||||
}
|
||||
|
||||
// Pin numbers:
|
||||
|
||||
printf (" | %2d", physPin) ;
|
||||
++physPin ;
|
||||
printf (" || %-2d", physPin) ;
|
||||
|
||||
// Same, reversed
|
||||
|
||||
if (physToWpi [physPin] == -1)
|
||||
printf (" | | ") ;
|
||||
else
|
||||
{
|
||||
/**/ if (wpMode == WPI_MODE_GPIO)
|
||||
pin = physPinToGpio (physPin) ;
|
||||
else if (wpMode == WPI_MODE_PHYS)
|
||||
pin = physPin ;
|
||||
else
|
||||
pin = physToWpi [physPin] ;
|
||||
|
||||
printf (" | %d", digitalRead (pin)) ;
|
||||
printf (" | %-4s", alts [getAlt (pin)]) ;
|
||||
}
|
||||
|
||||
printf (" | %-5s", physNames [physPin]) ;
|
||||
|
||||
if (physToWpi [physPin] == -1)
|
||||
printf (" | | ") ;
|
||||
else
|
||||
printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin)) ;
|
||||
|
||||
printf (" |\n") ;
|
||||
}
|
||||
|
||||
|
||||
void cmReadall (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
|
||||
printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
|
||||
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
|
||||
|
||||
for (pin = 0 ; pin < 28 ; ++pin)
|
||||
{
|
||||
printf ("| %3d ", pin) ;
|
||||
printf ("| %-4s ", alts [getAlt (pin)]) ;
|
||||
printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
|
||||
printf ("| ") ;
|
||||
printf ("| %3d ", pin + 28) ;
|
||||
printf ("| %-4s ", alts [getAlt (pin + 28)]) ;
|
||||
printf ("| %s ", digitalRead (pin + 28) == HIGH ? "High" : "Low ") ;
|
||||
printf ("|\n") ;
|
||||
}
|
||||
|
||||
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* abReadall:
|
||||
* Read all the pins on the model A or B.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void abReadall (int model, int rev)
|
||||
{
|
||||
int pin ;
|
||||
char *type ;
|
||||
|
||||
if (model == PI_MODEL_A)
|
||||
type = " A" ;
|
||||
else
|
||||
if (rev == PI_VERSION_2)
|
||||
type = "B2" ;
|
||||
else
|
||||
type = "B1" ;
|
||||
|
||||
printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
|
||||
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
|
||||
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
|
||||
for (pin = 1 ; pin <= 26 ; pin += 2)
|
||||
readallPhys (pin) ;
|
||||
|
||||
if (rev == PI_VERSION_2) // B version 2
|
||||
{
|
||||
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
|
||||
for (pin = 51 ; pin <= 54 ; pin += 2)
|
||||
readallPhys (pin) ;
|
||||
}
|
||||
|
||||
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
|
||||
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
|
||||
printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* bPlusReadall:
|
||||
* Read all the pins on the model B+
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void bPlusReadall (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
|
||||
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
|
||||
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
|
||||
for (pin = 1 ; pin <= 40 ; pin += 2)
|
||||
readallPhys (pin) ;
|
||||
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
|
||||
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
|
||||
printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
|
||||
}
|
||||
|
||||
|
||||
void doReadall (void)
|
||||
{
|
||||
int model, rev, mem, maker, overVolted ;
|
||||
|
||||
if (wiringPiNodes != NULL) // External readall
|
||||
{
|
||||
doReadallExternal () ;
|
||||
return ;
|
||||
}
|
||||
|
||||
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
|
||||
|
||||
/**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
|
||||
abReadall (model, rev) ;
|
||||
else if (model == PI_MODEL_BP)
|
||||
bPlusReadall () ;
|
||||
else if (model == PI_MODEL_CM)
|
||||
cmReadall () ;
|
||||
else
|
||||
printf ("Oops - unable to determine board type... model: %d\n", model) ;
|
||||
}
|
||||
165
WiringPi/wiringPi/COPYING.LESSER
Normal file
165
WiringPi/wiringPi/COPYING.LESSER
Normal 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.
|
||||
@@ -35,7 +35,8 @@ DYNAMIC=libwiringPi.so.$(VERSION)
|
||||
DEBUG = -O2
|
||||
CC = gcc
|
||||
INCLUDE = -I.
|
||||
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC
|
||||
DEFS = -D_GNU_SOURCE
|
||||
CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC
|
||||
|
||||
LIBS =
|
||||
|
||||
@@ -47,10 +48,14 @@ SRC = wiringPi.c \
|
||||
piHiPri.c piThread.c \
|
||||
wiringPiSPI.c wiringPiI2C.c \
|
||||
softPwm.c softTone.c \
|
||||
mcp23s08.c mcp23008.c \
|
||||
mcp23s17.c mcp23017.c sr595.c \
|
||||
piFace.c gertboard.c \
|
||||
piNes.c ds1302.c lcd.c
|
||||
mcp23008.c mcp23016.c mcp23017.c \
|
||||
mcp23s08.c mcp23s17.c \
|
||||
sr595.c \
|
||||
pcf8574.c pcf8591.c \
|
||||
mcp3002.c mcp3004.c mcp4802.c mcp3422.c \
|
||||
max31855.c max5322.c \
|
||||
sn3218.c \
|
||||
drcSerial.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
@@ -74,42 +79,55 @@ $(DYNAMIC): $(OBJ)
|
||||
|
||||
.PHONEY: clean
|
||||
clean:
|
||||
rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
|
||||
|
||||
.PHONEY: tags
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
.PHONEY: install
|
||||
install: $(DYNAMIC)
|
||||
@echo "[Install]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
|
||||
.PHONEY: install-headers
|
||||
install-headers:
|
||||
@echo "[Install Headers]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 ds1302.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 drcSerial.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 max5322.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 sn3218.h $(DESTDIR)$(PREFIX)/include
|
||||
|
||||
.PHONEY: install
|
||||
install: $(DYNAMIC) install-headers
|
||||
@echo "[Install Dynamic Lib]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION)
|
||||
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so
|
||||
@ldconfig
|
||||
|
||||
.PHONEY: install-static
|
||||
install-static: $(STATIC)
|
||||
@echo "[Install Static]"
|
||||
install-static: $(STATIC) install-headers
|
||||
@echo "[Install Static Lib]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib
|
||||
|
||||
.PHONEY: uninstall
|
||||
@@ -118,20 +136,26 @@ uninstall:
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringSerial.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringShift.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/piFace.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/piNes.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/softTone.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/lcd.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/drcSerial.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/max31855.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/max5322.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/sr595.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/pcf8591.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/sn3218.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.*
|
||||
@ldconfig
|
||||
|
||||
@@ -142,22 +166,28 @@ depend:
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
wiringPi.o: wiringPi.h
|
||||
wiringPi.o: softPwm.h softTone.h wiringPi.h
|
||||
wiringSerial.o: wiringSerial.h
|
||||
wiringShift.o: wiringPi.h wiringShift.h
|
||||
piHiPri.o: wiringPi.h
|
||||
piThread.o: wiringPi.h
|
||||
wiringPiSPI.o: wiringPiSPI.h
|
||||
wiringPiSPI.o: wiringPi.h wiringPiSPI.h
|
||||
wiringPiI2C.o: wiringPi.h wiringPiI2C.h
|
||||
softPwm.o: wiringPi.h softPwm.h
|
||||
softTone.o: wiringPi.h softTone.h
|
||||
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
|
||||
mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h
|
||||
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
|
||||
mcp23016.o: wiringPi.h wiringPiI2C.h mcp23016.h mcp23016reg.h
|
||||
mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h
|
||||
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
|
||||
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
|
||||
sr595.o: wiringPi.h sr595.h
|
||||
piFace.o: wiringPi.h wiringPiSPI.h piFace.h mcp23x0817.h
|
||||
gertboard.o: wiringPi.h wiringPiSPI.h gertboard.h
|
||||
piNes.o: wiringPi.h piNes.h
|
||||
ds1302.o: wiringPi.h ds1302.h
|
||||
lcd.o: wiringPi.h lcd.h
|
||||
pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h
|
||||
pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h
|
||||
mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h
|
||||
mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h
|
||||
mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h
|
||||
mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h
|
||||
max31855.o: wiringPi.h wiringPiSPI.h max31855.h
|
||||
max5322.o: wiringPi.h wiringPiSPI.h max5322.h
|
||||
sn3218.o: wiringPi.h wiringPiI2C.h sn3218.h
|
||||
drcSerial.o: wiringPi.h wiringSerial.h drcSerial.h
|
||||
|
||||
201
WiringPi/wiringPi/drcSerial.c
Normal file
201
WiringPi/wiringPi/drcSerial.c
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* drcSerial.c:
|
||||
* Extend wiringPi with the DRC Serial protocol (e.g. to Arduino)
|
||||
* 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 <time.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringSerial.h"
|
||||
|
||||
#include "drcSerial.h"
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
* Change the pin mode on the remote DRC device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
/**/ if (mode == OUTPUT)
|
||||
serialPutchar (node->fd, 'o') ; // Input
|
||||
else if (mode == PWM_OUTPUT)
|
||||
serialPutchar (node->fd, 'p') ; // PWM
|
||||
else
|
||||
serialPutchar (node->fd, 'i') ; // Default to input
|
||||
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
* ATmegas only have pull-up's on of off. No pull-downs.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
|
||||
// Force pin into input mode
|
||||
|
||||
serialPutchar (node->fd, 'i' ) ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
|
||||
/**/ if (mode == PUD_UP)
|
||||
{
|
||||
serialPutchar (node->fd, '1') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
else if (mode == PUD_OFF)
|
||||
{
|
||||
serialPutchar (node->fd, '0') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
serialPutchar (node->fd, value == 0 ? '0' : '1') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPwmWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
serialPutchar (node->fd, 'v') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
serialPutchar (node->fd, value & 0xFF) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int vHi, vLo ;
|
||||
|
||||
serialPutchar (node->fd, 'a') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
vHi = serialGetchar (node->fd) ;
|
||||
vLo = serialGetchar (node->fd) ;
|
||||
|
||||
return (vHi << 8) | vLo ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
serialPutchar (node->fd, 'r') ; // Send read command
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
return (serialGetchar (node->fd) == '0') ? 0 : 1 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* drcSetup:
|
||||
* Create a new instance of an DRC GPIO interface.
|
||||
* Could be a variable nunber of pins here - we might not know in advance
|
||||
* if it's an ATmega with 14 pins, or something with less or more!
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud)
|
||||
{
|
||||
int fd ;
|
||||
int ok, tries ;
|
||||
time_t then ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = serialOpen (device, baud)) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ;
|
||||
|
||||
delay (10) ; // May need longer if it's an Uno that reboots on the open...
|
||||
|
||||
// Flush any pending input
|
||||
|
||||
while (serialDataAvail (fd))
|
||||
(void)serialGetchar (fd) ;
|
||||
|
||||
ok = FALSE ;
|
||||
for (tries = 1 ; (tries < 5) && (!ok) ; ++tries)
|
||||
{
|
||||
serialPutchar (fd, '@') ; // Ping
|
||||
then = time (NULL) + 2 ;
|
||||
while (time (NULL) < then)
|
||||
if (serialDataAvail (fd))
|
||||
{
|
||||
if (serialGetchar (fd) == '@')
|
||||
{
|
||||
ok = TRUE ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
serialClose (fd) ;
|
||||
return wiringPiFailure (WPI_FATAL, "Unable to communicate with DRC serial device") ;
|
||||
}
|
||||
|
||||
node = wiringPiNewNode (pinBase, numPins) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->pwmWrite = myPwmWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/drcSerial.h
Normal file
33
WiringPi/wiringPi/drcSerial.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* drcSerial.h:
|
||||
* Extend wiringPi with the DRC Serial protocol (e.g. to Arduino)
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
81
WiringPi/wiringPi/max31855.c
Normal file
81
WiringPi/wiringPi/max31855.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* max31855.c:
|
||||
* Extend wiringPi with the max31855 SPI Analog to Digital convertor
|
||||
* 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 <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "max31855.h"
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
* Return the analog value of the given pin
|
||||
* Note: The chip really only has one read "channel", but we're faking it
|
||||
* here so we can read the error registers. Channel 0 will be the data
|
||||
* channel, and 1 is the error register code.
|
||||
* Note: Temperature returned is temp in C * 4, so divide result by 4
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
unsigned int spiData ;
|
||||
int temp ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
|
||||
|
||||
if (chan == 0) // Read temp in C
|
||||
{
|
||||
spiData >>= 18 ;
|
||||
temp = spiData & 0x3FFF ; // Bottom 13 bits
|
||||
if ((spiData & 0x2000) != 0) // Negative
|
||||
temp = -temp ;
|
||||
return temp ;
|
||||
}
|
||||
else // Return error bits
|
||||
return spiData & 0x7 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* max31855Setup:
|
||||
* Create a new wiringPi device node for an max31855 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int max31855Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/max31855.h
Normal file
33
WiringPi/wiringPi/max31855.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* max31855.c:
|
||||
* Extend wiringPi with the MAX31855 SPI Thermocouple driver
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int max31855Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
84
WiringPi/wiringPi/max5322.c
Normal file
84
WiringPi/wiringPi/max5322.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* max5322.c:
|
||||
* Extend wiringPi with the MAX5322 SPI Digital to Analog convertor
|
||||
* 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 <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "max5322.h"
|
||||
|
||||
/*
|
||||
* myAnalogWrite:
|
||||
* Write analog value on the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
unsigned char spiData [2] ;
|
||||
unsigned char chanBits, dataBits ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0b01000000 ;
|
||||
else
|
||||
chanBits = 0b01010000 ;
|
||||
|
||||
chanBits |= ((value >> 12) & 0x0F) ;
|
||||
dataBits = ((value ) & 0xFF) ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = dataBits ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* max5322Setup:
|
||||
* Create a new wiringPi device node for an max5322 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int max5322Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
unsigned char spiData [2] ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
// Enable both DACs
|
||||
|
||||
spiData [0] = 0b11100000 ;
|
||||
spiData [1] = 0 ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/max5322.h
Normal file
33
WiringPi/wiringPi/max5322.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* max5322.h:
|
||||
* Extend wiringPi with the MAX5322 SPI Digital to Analog convertor
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int max5322Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -39,19 +39,18 @@
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, ddr ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
ddr = MCP23x08_IODIR ;
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
|
||||
reg = MCP23x08_IODIR ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,20 +61,19 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
pud = MCP23x08_GPPU ;
|
||||
mask = 1 << pin ;
|
||||
reg = MCP23x08_GPPU ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
|
||||
old = wiringPiI2CReadReg8 (node->fd, pud) ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,8 +86,7 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
bit = 1 << (pin & 7) ;
|
||||
bit = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
old = node->data2 ;
|
||||
if (value == LOW)
|
||||
@@ -109,13 +106,10 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value, gpio ;
|
||||
int mask, value ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
gpio = MCP23x08_GPIO ;
|
||||
mask = 1 << pin ;
|
||||
|
||||
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
|
||||
mask = 1 << ((pin - node->pinBase) & 7) ;
|
||||
value = wiringPiI2CReadReg8 (node->fd, MCP23x08_GPIO) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
@@ -127,12 +121,12 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
/*
|
||||
* mcp23008Setup:
|
||||
* Create a new instance of an MCP23008 I2C GPIO interface. We know it
|
||||
* has 16 pins, so all we need to know here is the I2C address and the
|
||||
* has 8 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23008Setup (int pinBase, int i2cAddress)
|
||||
int mcp23008Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
@@ -142,7 +136,7 @@ int mcp23008Setup (int pinBase, int i2cAddress)
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
node = wiringPiNewNode (pinBase, 8) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp23008Setup (int pinBase, int i2cAddress) ;
|
||||
extern int mcp23008Setup (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
164
WiringPi/wiringPi/mcp23016.c
Normal file
164
WiringPi/wiringPi/mcp23016.c
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* mcp23016.c:
|
||||
* Extend wiringPi with the MCP 23016 I2C GPIO expander chip
|
||||
* 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 <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiI2C.h"
|
||||
#include "mcp23016.h"
|
||||
|
||||
#include "mcp23016reg.h"
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
reg = MCP23016_IODIR0 ;
|
||||
else
|
||||
{
|
||||
reg = MCP23016_IODIR1 ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ; // Pin now 0-15
|
||||
|
||||
bit = 1 << (pin & 7) ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
{
|
||||
old = node->data2 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23016_GP0, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
else // Bank B
|
||||
{
|
||||
old = node->data3 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23016_GP1, old) ;
|
||||
node->data3 = old ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value, gpio ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
gpio = MCP23016_GP0 ;
|
||||
else
|
||||
{
|
||||
gpio = MCP23016_GP1 ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp23016Setup:
|
||||
* Create a new instance of an MCP23016 I2C GPIO interface. We know it
|
||||
* has 16 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23016Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON0, IOCON_INIT) ;
|
||||
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON1, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT0) ;
|
||||
node->data3 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT1) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/mcp23016.h
Normal file
33
WiringPi/wiringPi/mcp23016.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* mcp23016.h:
|
||||
* Extend wiringPi with the MCP 23016 I2C GPIO expander chip
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp23016Setup (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
48
WiringPi/wiringPi/mcp23016reg.h
Normal file
48
WiringPi/wiringPi/mcp23016reg.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* mcp23016:
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
*
|
||||
* Header file for code using the MCP23016 GPIO expander
|
||||
* chip.
|
||||
***********************************************************************
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
// MCP23016 Registers
|
||||
|
||||
#define MCP23016_GP0 0x00
|
||||
#define MCP23016_GP1 0x01
|
||||
#define MCP23016_OLAT0 0x02
|
||||
#define MCP23016_OLAT1 0x03
|
||||
#define MCP23016_IPOL0 0x04
|
||||
#define MCP23016_IPOL1 0x05
|
||||
#define MCP23016_IODIR0 0x06
|
||||
#define MCP23016_IODIR1 0x07
|
||||
#define MCP23016_INTCAP0 0x08
|
||||
#define MCP23016_INTCAP1 0x09
|
||||
#define MCP23016_IOCON0 0x0A
|
||||
#define MCP23016_IOCON1 0x0B
|
||||
|
||||
// Bits in the IOCON register
|
||||
|
||||
#define IOCON_IARES 0x01
|
||||
|
||||
// Default initialisation mode
|
||||
|
||||
#define IOCON_INIT 0
|
||||
@@ -39,27 +39,27 @@
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, ddr ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
ddr = MCP23x17_IODIRA ;
|
||||
reg = MCP23x17_IODIRA ;
|
||||
else
|
||||
{
|
||||
ddr = MCP23x17_IODIRB ;
|
||||
reg = MCP23x17_IODIRB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,27 +70,27 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
pud = MCP23x17_GPPUA ;
|
||||
reg = MCP23x17_GPPUA ;
|
||||
else
|
||||
{
|
||||
pud = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
reg = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, pud) ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -146,7 +146,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
gpio = MCP23x17_GPIOA ;
|
||||
gpio = MCP23x17_GPIOA ;
|
||||
else
|
||||
{
|
||||
gpio = MCP23x17_GPIOB ;
|
||||
@@ -171,7 +171,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23017Setup (int pinBase, int i2cAddress)
|
||||
int mcp23017Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp23017Setup (int pinBase, int i2cAddress) ;
|
||||
extern int mcp23017Setup (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -78,19 +78,18 @@ static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, ddr ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
ddr = MCP23x08_IODIR ;
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, ddr) ;
|
||||
reg = MCP23x08_IODIR ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (node->data0, node->data1, ddr, old) ;
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -101,20 +100,19 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
pud = MCP23x08_GPPU ;
|
||||
mask = 1 << pin ;
|
||||
reg = MCP23x08_GPPU ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
|
||||
old = readByte (node->data0, node->data1, pud) ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
writeByte (node->data0, node->data1, pud, old) ;
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -127,8 +125,7 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
bit = 1 << pin ;
|
||||
bit = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
old = node->data2 ;
|
||||
if (value == LOW)
|
||||
@@ -148,13 +145,10 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value, gpio ;
|
||||
int mask, value ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
gpio = MCP23x08_GPIO ;
|
||||
mask = 1 << pin ;
|
||||
|
||||
value = readByte (node->data0, node->data1, gpio) ;
|
||||
mask = 1 << ((pin - node->pinBase) & 7) ;
|
||||
value = readByte (node->data0, node->data1, MCP23x08_GPIO) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
@@ -166,12 +160,12 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
/*
|
||||
* mcp23s08Setup:
|
||||
* Create a new instance of an MCP23s08 SPI GPIO interface. We know it
|
||||
* has 16 pins, so all we need to know here is the SPI address and the
|
||||
* has 8 pins, so all we need to know here is the SPI address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23s08Setup (int pinBase, int spiPort, int devId)
|
||||
int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
@@ -181,7 +175,7 @@ int mcp23s08Setup (int pinBase, int spiPort, int devId)
|
||||
|
||||
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
node = wiringPiNewNode (pinBase, 8) ;
|
||||
|
||||
node->data0 = spiPort ;
|
||||
node->data1 = devId ;
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp23s08Setup (int pinBase, int spiPort, int devId) ;
|
||||
extern int mcp23s08Setup (const int pinBase, const int spiPort, const int devId) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -78,27 +78,27 @@ static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, ddr ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
ddr = MCP23x17_IODIRA ;
|
||||
reg = MCP23x17_IODIRA ;
|
||||
else
|
||||
{
|
||||
ddr = MCP23x17_IODIRB ;
|
||||
reg = MCP23x17_IODIRB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, ddr) ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (node->data0, node->data1, ddr, old) ;
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -109,27 +109,27 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
pud = MCP23x17_GPPUA ;
|
||||
reg = MCP23x17_GPPUA ;
|
||||
else
|
||||
{
|
||||
pud = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
reg = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, pud) ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
writeByte (node->data0, node->data1, pud, old) ;
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
gpio = MCP23x17_GPIOA ;
|
||||
gpio = MCP23x17_GPIOA ;
|
||||
else
|
||||
{
|
||||
gpio = MCP23x17_GPIOB ;
|
||||
@@ -210,7 +210,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23s17Setup (int pinBase, int spiPort, int devId)
|
||||
int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
76
WiringPi/wiringPi/mcp3002.c
Normal file
76
WiringPi/wiringPi/mcp3002.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* mcp3002.c:
|
||||
* Extend wiringPi with the MCP3002 SPI Analog to Digital convertor
|
||||
* 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 <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "mcp3002.h"
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
* Return the analog value of the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
unsigned char spiData [2] ;
|
||||
unsigned char chanBits ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0b11010000 ;
|
||||
else
|
||||
chanBits = 0b11110000 ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = 0 ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
|
||||
return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp3002Setup:
|
||||
* Create a new wiringPi device node for an mcp3002 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp3002Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user