From 143b383b8da7d6aed23dd8644f2b9a07a695a4e1 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 28 Jan 2016 19:20:31 +0000 Subject: [PATCH] Added support for the Pimoroni scrollPhat Added support for the ADS1115 16-bit ADC Updated the gpio readall command to correctly with with the Compute Module and fixed a resulting bug in wiringPi... --- devLib/Makefile | 5 +- devLib/scrollPhat.c | 430 ++++++++++++++++++++++++++++++++++ devLib/scrollPhat.h | 39 ++++ devLib/scrollPhatFont.h | 544 +++++++++++++++++++++++++++++++++++++++++++ examples/scrollPhat/Makefile | 79 +++++++ examples/scrollPhat/scphat.c | 230 ++++++++++++++++++ examples/scrollPhat/test.c | 115 +++++++++ gpio/readall.c | 9 +- wiringPi/Makefile | 4 +- wiringPi/ads1115.c | 293 +++++++++++++++++++++++ wiringPi/ads1115.h | 55 +++++ wiringPi/wiringPi.c | 24 +- wiringPi/wpiExtensions.c | 28 +++ 13 files changed, 1847 insertions(+), 8 deletions(-) create mode 100644 devLib/scrollPhat.c create mode 100644 devLib/scrollPhat.h create mode 100644 devLib/scrollPhatFont.h create mode 100644 examples/scrollPhat/Makefile create mode 100644 examples/scrollPhat/scphat.c create mode 100644 examples/scrollPhat/test.c create mode 100644 wiringPi/ads1115.c create mode 100644 wiringPi/ads1115.h diff --git a/devLib/Makefile b/devLib/Makefile index 0fb0033..1b1ebe0 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -48,11 +48,13 @@ LIBS = SRC = ds1302.c maxdetect.c piNes.c \ gertboard.c piFace.c \ lcd128x64.c lcd.c \ + scrollPhat.c \ piGlow.c OBJ = $(SRC:.c=.o) -HEADERS = ds1302.h gertboard.h lcd128x64.h lcd.h maxdetect.h piFace.h piGlow.h piNes.h +HEADERS = ds1302.h gertboard.h lcd128x64.h lcd.h maxdetect.h piFace.h piGlow.h piNes.h\ + scrollPhat.h all: $(DYNAMIC) @@ -134,4 +136,5 @@ gertboard.o: gertboard.h piFace.o: piFace.h lcd128x64.o: font.h lcd128x64.h lcd.o: lcd.h +scrollPhat.o: scrollPhatFont.h scrollPhat.h piGlow.o: piGlow.h diff --git a/devLib/scrollPhat.c b/devLib/scrollPhat.c new file mode 100644 index 0000000..c1a6f11 --- /dev/null +++ b/devLib/scrollPhat.c @@ -0,0 +1,430 @@ +/* + * scrollPhat.c: + * Simple driver for the Pimoroni Scroll Phat device + * + * Copyright (c) 2015 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + +#include + +#include "scrollPhatFont.h" +#include "scrollPhat.h" + +// Size + +#define SP_WIDTH 11 +#define SP_HEIGHT 5 + +// I2C + +#define PHAT_I2C_ADDR 0x60 + +// Software copy of the framebuffer +// it's 8-bit deep although the display itself is only 1-bit deep. + +static unsigned char frameBuffer [SP_WIDTH * SP_HEIGHT] ; + +static int lastX, lastY ; +static int printDelayFactor ; +static int scrollPhatFd ; + +static int putcharX ; + +#undef DEBUG + + +/* + * delay: + * Wait for some number of milliseconds. + * This taken from wiringPi as there is no-need to include the whole of + * wiringPi just for the delay function. + ********************************************************************************* + */ + +static void delay (unsigned int howLong) +{ + struct timespec sleeper, dummy ; + + sleeper.tv_sec = (time_t)(howLong / 1000) ; + sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ; + + nanosleep (&sleeper, &dummy) ; +} + + + +/* + * scrollPhatUpdate: + * Copy our software version to the real display + ********************************************************************************* + */ + +void scrollPhatUpdate (void) +{ + register int x, y ; + register unsigned char data, pixel ; + unsigned char pixels [SP_WIDTH] ; + +#ifdef DEBUG + printf ("+-----------+\n") ; + for (y = 0 ; y < SP_HEIGHT ; ++y) + { + putchar ('|') ; + for (x = 0 ; x < SP_WIDTH ; ++x) + { + pixel = frameBuffer [x + y * SP_WIDTH] ; + putchar (pixel == 0 ? ' ' : '*') ; + } + printf ("|\n") ; + } + printf ("+-----------+\n") ; +#endif + + for (x = 0 ; x < SP_WIDTH ; ++x) + { + data = 0 ; + for (y = 0 ; y < SP_HEIGHT ; ++y) + { + pixel = frameBuffer [x + y * SP_WIDTH] ; + data = (data << 1) | ((pixel == 0) ? 0 : 1) ; + } + pixels [x] = data ; + } + + for (x = 0 ; x < SP_WIDTH ; ++x) + wiringPiI2CWriteReg8 (scrollPhatFd, 1 + x, pixels [x]) ; + + wiringPiI2CWriteReg8 (scrollPhatFd, 0x0C, 0) ; +} + + +/* + ********************************************************************************* + * Standard Graphical Functions + ********************************************************************************* + */ + + +/* + * scrollPhatPoint: + * Plot a pixel. Crude clipping - speed is not the essence here. + ********************************************************************************* + */ + +void scrollPhatPoint (int x, int y, int colour) +{ + lastX = x ; + lastY = y ; + + if ((x < 0) || (x >= SP_WIDTH) || (y < 0) || (y >= SP_HEIGHT)) + return ; + + frameBuffer [x + y * SP_WIDTH] = colour ; +} + + +/* + * scrollPhatLine: scrollPhatLineTo: + * Classic Bressenham Line code - rely on the point function to do the + * clipping for us here. + ********************************************************************************* + */ + +void scrollPhatLine (int x0, int y0, int x1, int y1, int colour) +{ + int dx, dy ; + int sx, sy ; + int err, e2 ; + + lastX = x1 ; + lastY = y1 ; + + dx = abs (x1 - x0) ; + dy = abs (y1 - y0) ; + + sx = (x0 < x1) ? 1 : -1 ; + sy = (y0 < y1) ? 1 : -1 ; + + err = dx - dy ; + + for (;;) + { + scrollPhatPoint (x0, y0, colour) ; + + if ((x0 == x1) && (y0 == y1)) + break ; + + e2 = 2 * err ; + + if (e2 > -dy) + { + err -= dy ; + x0 += sx ; + } + + if (e2 < dx) + { + err += dx ; + y0 += sy ; + } + } + +} + +void scrollPhatLineTo (int x, int y, int colour) +{ + scrollPhatLine (lastX, lastY, x, y, colour) ; +} + + +/* + * scrollPhatRectangle: + * A rectangle is a spoilt days fishing + ********************************************************************************* + */ + +void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled) +{ + register int x ; + + if (filled) + { + /**/ if (x1 == x2) + scrollPhatLine (x1, y1, x2, y2, colour) ; + else if (x1 < x2) + for (x = x1 ; x <= x2 ; ++x) + scrollPhatLine (x, y1, x, y2, colour) ; + else + for (x = x2 ; x <= x1 ; ++x) + scrollPhatLine (x, y1, x, y2, colour) ; + } + else + { + scrollPhatLine (x1, y1, x2, y1, colour) ; + scrollPhatLineTo (x2, y2, colour) ; + scrollPhatLineTo (x1, y2, colour) ; + scrollPhatLineTo (x1, y1, colour) ; + } +} + + +/* + * scrollPhatPutchar: + * Print a single character to the screen then advance the pointer by an + * appropriate ammount (variable width font). + * We rely on the clipping done by the pixel plot function to keep us + * out of trouble. + * Return the width + space + ********************************************************************************* + */ + +int scrollPhatPutchar (int c) +{ + register int x, y ; + + unsigned char line ; + unsigned char *fontPtr ; + unsigned char *p2 ; + int lineWidth, width, mask ; + +// The font is printable characters, uppercase only... +// and somewhat varaible width... + + c &= 0x7F ; + if (c > 0x60) + c -= 64 ; + else + c -= 32 ; + + fontPtr = scrollPhatFont + c * fontHeight ; + +// Work out width of this character +// There probably is a more efficient way to do this, but... + + p2 = fontPtr ; + width = 0 ; + for (y = 0 ; y < fontHeight ; ++y) + { + mask = 0x80 ; + for (lineWidth = 8 ; lineWidth > 0 ; --lineWidth) + { + if ((*p2 & mask) != 0) + break ; + mask >>= 1 ; + } + if (lineWidth > width) + width = lineWidth ; + + ++p2 ; + } + + if (width == 0) // Likely to be a blank or space character + width = 3 ; + + for (y = fontHeight - 1 ; y >= 0 ; --y) + { + x = 0 ; + line = *fontPtr++ ; + for (mask = 1 << (width - 1) ; mask != 0 ; mask >>= 1) + { + scrollPhatPoint (putcharX + x, y, (line & mask)) ; + ++x ; + } + } + +// make a line of space + + for (y = fontHeight - 1 ; y >= 0 ; --y) + scrollPhatPoint (putcharX + width, y, 0) ; + + putcharX = putcharX + width + 1 ; + + return width + 1 ; +} + + +/* + * scrollPhatPuts: + * Send a string to the display - and scroll it across. + * This is somewhat of a hack in that we print the entire string to the + * display and let the point clipping take care of what's off-screen... + ********************************************************************************* + */ + +void scrollPhatPuts (const char *str) +{ + int i ; + int movingX = 0 ; + const char *s ; + int pixelLen ; + +// Print it once, then we know the width in pixels... + + putcharX = 0 ; + s = str ; + while (*s) + scrollPhatPutchar (*s++) ; + + pixelLen = putcharX ; + +// Now scroll it by printing it and moving left one pixel + + movingX = 0 ; + for (i = 0 ; i < pixelLen ; ++i) + { + putcharX = movingX ; + s = str ; + while (*s) + scrollPhatPutchar (*s++) ; + --movingX ; + scrollPhatUpdate () ; + delay (printDelayFactor) ; + } +} + + +/* + * scrollPhatPrintf: + * Does what it says + ********************************************************************************* + */ + +void scrollPhatPrintf (const char *message, ...) +{ + va_list argp ; + char buffer [1024] ; + + va_start (argp, message) ; + vsnprintf (buffer, 1023, message, argp) ; + va_end (argp) ; + + scrollPhatPuts (buffer) ; +} + + +/* + * scrollPhatPrintSpeed: + * Change the print speed - mS per shift by 1 pixel + ********************************************************************************* + */ + +void scrollPhatPrintSpeed (const int pps) +{ + if (pps < 0) + printDelayFactor = 0 ; + else + printDelayFactor = pps ; +} + + +/* + * scrollPhatClear: + * Clear the display + ********************************************************************************* + */ + +void scrollPhatClear (void) +{ + register int i ; + register unsigned char *ptr = frameBuffer ; + + for (i = 0 ; i < (SP_WIDTH * SP_HEIGHT) ; ++i) + *ptr++ = 0 ; + + scrollPhatUpdate () ; +} + + +/* + * scrollPhatIntensity: + * Set the display brightness - percentage + ********************************************************************************* + */ + +void scrollPhatIntensity (const int percent) +{ + wiringPiI2CWriteReg8 (scrollPhatFd, 0x19, (127 * percent) / 100) ; +} + + +/* + * scrollPhatSetup: + * Initialise the Scroll Phat display + ********************************************************************************* + */ + +int scrollPhatSetup (void) +{ + if ((scrollPhatFd = wiringPiI2CSetup (PHAT_I2C_ADDR)) < 0) + return scrollPhatFd ; + + wiringPiI2CWriteReg8 (scrollPhatFd, 0x00, 0x03) ; // Enable display, set to 5x11 mode + scrollPhatIntensity (10) ; + scrollPhatClear () ; + scrollPhatPrintSpeed (100) ; + + return 0 ; +} diff --git a/devLib/scrollPhat.h b/devLib/scrollPhat.h new file mode 100644 index 0000000..0e762b1 --- /dev/null +++ b/devLib/scrollPhat.h @@ -0,0 +1,39 @@ +/* + * scrollPhat.h: + * Simple driver for the Pimoroni Scroll Phat device + * + * Copyright (c) 2015 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +extern void scrollPhatPoint (int x, int y, int colour) ; +extern void scrollPhatLine (int x0, int y0, int x1, int y1, int colour) ; +extern void scrollPhatLineTo (int x, int y, int colour) ; +extern void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled) ; +extern void scrollPhatUpdate (void) ; +extern void scrollPhatClear (void) ; + +extern int scrollPhatPutchar (int c) ; +//extern void scrollPhatPutchar (int c) ; +extern void scrollPhatPuts (const char *str) ; +extern void scrollPhatPrintf (const char *message, ...) ; +extern void scrollPhatPrintSpeed (const int cps10) ; + +extern void scrollPhatIntensity (const int percent) ; +extern int scrollPhatSetup (void) ; diff --git a/devLib/scrollPhatFont.h b/devLib/scrollPhatFont.h new file mode 100644 index 0000000..35481f8 --- /dev/null +++ b/devLib/scrollPhatFont.h @@ -0,0 +1,544 @@ +/* + * scrollPhatFont.h: + * Simple font for the Pimoroni Scroll Phat. + * Note: this is a very much reduced font - 5 pixels high and + * mostly 4 pixels wide - sometimes 5. Also only + * printable characters from space to _ uppercase only. + * + * Copyright (c) 2015-2016 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 . + *********************************************************************** + */ + +static const int fontHeight = 5 ; + +static unsigned char scrollPhatFont [] = +{ + +// 0x20, Space. Handeled as a special case in the code. + + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0x0, // .... + +// 0x21, ! + + 0x1, // * + 0x1, // * + 0x1, // * + 0x0, // . + 0x1, // * + +// 0x22, " + + 0x5, // *..* + 0x5, // *..* + 0x0, // .... + 0x0, // .... + 0x0, // .... + +// 0x23, # + + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + +// 0x24, $ + + 0x1, // ...* + 0x7, // .*** + 0x2, // ..*. + 0xE, // ***. + 0x8, // *... + +// 0x25, % + + 0x9, // *..* + 0x1, // ...* + 0x6, // .**. + 0x8, // *... + 0x9, // *..* + +// 0x26, & + + 0x6, // .**. + 0x8, // *... + 0x4, // .*.. + 0xA, // *.*. + 0x5, // .*.* + +// 0x27, ' + + 0x1, // .* + 0x2, // *. + 0x0, // .. + 0x0, // .. + 0x0, // .. + +// 0x28, ( + + 0x3, // ..** + 0x4, // .*.. + 0x8, // *... + 0x4, // .*.. + 0x3, // ..** + +// 0x29, ) + + 0xC, // **.. + 0x2, // ..*. + 0x1, // ...* + 0x2, // ..*. + 0xC, // **.. + +// 0x2A, * + + 0x9, // *..* + 0x6, // .**. + 0xF, // **** + 0x6, // .**. + 0x9, // *..* + +// 0x2B, + + + 0x6, // .**. + 0x6, // .**. + 0xF, // **** + 0x6, // .**. + 0x6, // .**. + +// 0x2C, , + + 0x0, // .. + 0x0, // .. + 0x0, // .. + 0x1, // .* + 0x2, // *. + +// 0x2D, - + + 0x0, // .... + 0x0, // .... + 0xF, // **** + 0x0, // .... + 0x0, // .... + +// 0x2E, . + + 0x0, // . + 0x0, // . + 0x0, // . + 0x0, // . + 0x1, // * + +// 0x2F, / + + 0x1, // ...* + 0x3, // ..** + 0x4, // ..*. + 0xC, // **.. + 0x8, // *... + +// 0x30, 0 + + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + +// 0x31, 1 + + 0x2, // ..*. + 0x6, // .**. + 0x2, // ..*. + 0x2, // ..*. + 0x7, // .*** + +// 0x32, 2 + + 0x6, // .**. + 0x1, // ...* + 0x6, // .**. + 0x8, // *... + 0xF, // **** + +// 0x33, 3 + + 0xE, // ***. + 0x1, // ...* + 0xE, // ***. + 0x1, // ...* + 0xE, // ***. + +// 0x34, 4 + + 0x6, // .**. + 0xA, // *.*. + 0xF, // **** + 0x2, // ..*. + 0x2, // ..*. + +// 0x35, 5 + + 0xF, // **** + 0x8, // *... + 0xF, // **** + 0x1, // ...* + 0xE, // ***. + +// 0x36, 6 + + 0x2, // ..*. + 0x4, // .*.. + 0xA, // *.*. + 0x9, // *..* + 0x6, // .**. + +// 0x37, 7 + + 0xF, // **** + 0x1, // ...* + 0x2, // ..*. + 0x4, // .*.. + 0x8, // *... + +// 0x38, 8 + + 0x6, // .**. + 0x9, // *..* + 0x6, // .**. + 0x9, // *..* + 0x6, // .**. + +// 0x39, 9 + + 0x6, // .**. + 0x9, // *..* + 0x7, // .*.* + 0x1, // ..*. + 0x2, // .*.. + +// 0x3A, : + + 0x0, // . + 0x1, // * + 0x0, // . + 0x1, // * + 0x0, // . + +// 0x3B, ; + + 0x0, // .. + 0x1, // .* + 0x0, // .. + 0x1, // .* + 0x2, // *. + +// 0x3C, < + + 0x2, // ..*. + 0x4, // .*.. + 0x8, // *... + 0x4, // .*.. + 0x2, // ..*. + +// 0x3D, = + + 0x0, // .... + 0xF, // **** + 0x0, // .... + 0xF, // **** + 0x0, // .... + +// 0x3E, > + + 0x0, // .*.. + 0x0, // ..*. + 0x0, // ...* + 0x0, // ..*. + 0x0, // .*.. + +// 0x3F, ? + + 0x6, // .**. + 0x1, // ...* + 0x2, // ..*. + 0x0, // .... + 0x2, // ..*. + +// 0x40, @ + + 0x6, // .**. + 0xD, // **.* + 0x8, // *... + 0x4, // .*.. + 0x3, // ..** + +// 0x41, A + + 0x6, // .**. + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + 0x9, // *..* + +// 0x42, B + + 0xE, // ***. + 0x9, // *..* + 0xE, // ***. + 0x9, // *..* + 0xE, // ***. + +// 0x43, C + + 0x6, // .**. + 0x9, // *..* + 0x8, // *... + 0x9, // *..* + 0x6, // .**. + +// 0x44, D + + 0xE, // ***. + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0xE, // ***. + +// 0x45, E + + 0xF, // **** + 0x8, // *... + 0xE, // ***. + 0x8, // *... + 0xF, // **** + +// 0x46, F + + 0xF, // **** + 0x8, // *... + 0xE, // ***. + 0x8, // *... + 0x8, // *... + +// 0x47, G + + 0x6, // .**. + 0x9, // *..* + 0x8, // *... + 0xB, // *.** + 0x6, // .**. + +// 0x48, H + + 0x9, // *..* + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + 0x9, // *..* + +// 0x49, I + + 0x7, // *** + 0x2, // .*. + 0x2, // .*. + 0x2, // .*. + 0x7, // *** + +// 0x4A, J + + 0x7, // .*** + 0x2, // ..*. + 0x2, // ..*. + 0xA, // *.*. + 0x4, // .*.. + +// 0x4B, K + + 0x9, // *..* + 0xA, // *.*. + 0xC, // **.. + 0xA, // *.*. + 0x9, // *..* + +// 0x4C, L + + 0x4, // *.. + 0x4, // *.. + 0x4, // *.. + 0x4, // *.. + 0x7, // *** + +// 0x4D, M + + 0x11, // *...* + 0x1B, // **.** + 0x15, // *.*.* + 0x11, // *...* + 0x11, // *...* + +// 0x4E, N + + 0x9, // *..* + 0xD, // **.* + 0xB, // *.** + 0x9, // *..* + 0x9, // *..* + +// 0x4F, O + + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + +// 0x50, P + + 0xE, // ***. + 0x9, // *..* + 0xE, // ***. + 0x8, // *... + 0x8, // *... + +// 0x51, Q + + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + 0xA, // *.*. + 0x5, // .*.* + +// 0x52, R + + 0xE, // ***. + 0x9, // *..* + 0xF, // ***. + 0xA, // *.*. + 0x9, // *..* + +// 0x53, S + + 0x6, // .**. + 0x8, // *... + 0x6, // .**. + 0x1, // ...* + 0x6, // .**. + +// 0x54, T + + 0x7, // .*** + 0x2, // ..*. + 0x2, // ..*. + 0x2, // ..*. + 0x2, // ..*. + +// 0x55, U + + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + +// 0x56, V + + 0x11, // *...* + 0x11, // *...* + 0x11, // *...* + 0x0A, // .*.*. + 0x04, // ..*.. + +// 0x57, W + + 0x11, // *...* + 0x11, // *...* + 0x11, // *...* + 0x15, // *.*.* + 0x1B, // **.** + +// 0x58, X + + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + +// 0x59, Y + + 0x11, // *...* + 0x0A, // .*.*. + 0x04, // ..*.. + 0x04, // ..*.. + 0x04, // ..*.. + +// 0x5A, Z + + 0xF, // **** + 0x1, // ...* + 0x6, // .**. + 0x8, // *... + 0xF, // **** + +// 0x5B, [ + + 0xE, // ***. + 0x8, // *... + 0x8, // *... + 0x8, // *... + 0xE, // ***. + +// 0x5C, Backslash + + 0x8, // *... + 0xC, // **.. + 0x6, // .**. + 0x3, // ..** + 0x1, // ...* + +// 0x5D, ] + + 0x7, // .*** + 0x1, // ...* + 0x1, // ...* + 0x1, // ...* + 0x7, // .*** + +// 0x5E, ^ + + 0x6, // .**. + 0x9, // *..* + 0x0, // .... + 0x0, // .... + 0x0, // .... + +// 0x5F, _ + + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0xF, // **** +} ; diff --git a/examples/scrollPhat/Makefile b/examples/scrollPhat/Makefile new file mode 100644 index 0000000..c0b7241 --- /dev/null +++ b/examples/scrollPhat/Makefile @@ -0,0 +1,79 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012-2015 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# Wiring Compatable library for the Raspberry Pi +# +# wiringPi is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wiringPi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with wiringPi. If not, see . +################################################################################# + +ifneq ($V,1) +Q ?= @ +endif + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = scphat.c test.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +test: test.o + $Q echo [link] + $Q $(CC) -o $@ test.o $(LDFLAGS) $(LDLIBS) + +scphat: scphat.o + $Q echo [link] + $Q $(CC) -o $@ scphat.o $(LDFLAGS) $(LDLIBS) + + +.c.o: + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ + +clean: + $Q echo "[Clean]" + $Q rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) + +install: scphat + $Q echo Installing scphat into /usr/local/bin + $Q cp -a scphat /usr/local/bin/scphat + $Q chmod 755 /usr/local/bin/scphat + $Q echo Done. Remember to load the I2C drivers if needed. + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/scrollPhat/scphat.c b/examples/scrollPhat/scphat.c new file mode 100644 index 0000000..8f90bad --- /dev/null +++ b/examples/scrollPhat/scphat.c @@ -0,0 +1,230 @@ +/* + * scphat.c: + * Little program to allow use of the Pimoroni Sctoll Phat + * from the command-line. + * + * Copyright (c) 2015-2016 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include + +#include +#include + +static char *progName ; + + +/* + * checkArgs: + * Count the arguments for each little function + ********************************************************************************* + */ + +static void checkArgs (char *command, int num, int arg, int argc) +{ + if ((arg + num) < argc) + return ; + + fprintf (stderr, "%s: Not enough data for %s command.\n", progName, command) ; + exit (EXIT_FAILURE) ; +} + + +/* + * doClear: + * Clear the display + ********************************************************************************* + */ + +static int doClear (void) +{ + scrollPhatClear () ; + return 1 ; +} + + +/* + * doBright + ********************************************************************************* + */ + +static int doBright (int arg, int argc, char *argv []) +{ + checkArgs ("bright", 1, arg, argc) ; + scrollPhatIntensity (atoi (argv [arg+1])) ; + return 2 ; +} + + + +/* + * doPlot + ********************************************************************************* + */ + +static int doPlot (int arg, int argc, char *argv []) +{ + checkArgs ("plot", 2, arg, argc) ; + scrollPhatPoint (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ; + scrollPhatUpdate () ; + return 3 ; +} + + +/* + * doLine + ********************************************************************************* + */ + +static int doLine (int arg, int argc, char *argv []) +{ + checkArgs ("line", 4, arg, argc) ; + scrollPhatLine (atoi (argv [arg+1]), atoi (argv [arg+2]), + atoi (argv [arg+3]), atoi (argv [arg+4]), 1) ; + scrollPhatUpdate () ; + return 5 ; +} + + +/* + * doLineTo + ********************************************************************************* + */ + +static int doLineTo (int arg, int argc, char *argv []) +{ + checkArgs ("lineto", 2, arg, argc) ; + scrollPhatLineTo (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ; + scrollPhatUpdate () ; + return 3 ; +} + + +/* + * doWait + ********************************************************************************* + */ + +static int doWait (int arg, int argc, char *argv []) +{ + checkArgs ("wait", 1, arg, argc) ; + delay (atoi (argv [arg+1]) * 100) ; + scrollPhatUpdate () ; + return 2 ; +} + + +/* + * doSpeed + ********************************************************************************* + */ + +static int doSpeed (int arg, int argc, char *argv []) +{ + checkArgs ("speed", 1, arg, argc) ; + scrollPhatPrintSpeed (atoi (argv [arg+1])) ; + return 2 ; +} + + +/* + * doScroll + ********************************************************************************* + */ + +static int doScroll (int arg, int argc, char *argv []) +{ + checkArgs ("scroll", 1, arg, argc) ; + scrollPhatPuts (argv [arg+1]) ; + return 2 ; +} + + +static void failUsage (void) +{ + fprintf (stderr, "Usage: %s command [paremters] ...\n", progName) ; + fprintf (stderr, " commands:\n") ; + fprintf (stderr, " clear/cls - Clear the display\n") ; + fprintf (stderr, " bright N - Set display brightness; 1-100\n") ; + fprintf (stderr, " plot X Y - Set a single pixel at location X Y; 0-10, 0-4\n") ; + fprintf (stderr, " line X1 Y1 X2 Y2 - Draw a line from the 2 points\n") ; + fprintf (stderr, " lineto X2 Y2 - Draw a line from the last point to the new one\n") ; + fprintf (stderr, " wait/delay N - Wait for N 10ths seconds\n") ; + fprintf (stderr, " speed N - Set scrolling speed (cps)\n") ; + fprintf (stderr, " scroll S - Scroll the given string\n") ; + fprintf (stderr, "\n") ; + fprintf (stderr, " Example: %s plot 0 0 wait 50 scroll \" Hello \"\n", progName) ; + exit (EXIT_FAILURE) ; +} + + +/* + * the works + ********************************************************************************* + */ + +int main (int argc, char *argv []) +{ + int arg = 1 ; + char *command ; + + progName = argv [0] ; + + wiringPiSetupSys () ; + + if (scrollPhatSetup () != 0) + { + fprintf (stderr, "%s: Unable to initialise the scrollPhat: %s\n", progName, strerror (errno)) ; + exit (EXIT_FAILURE) ; + } + + progName = argv [0] ; + + if (argc < 2) + { + fprintf (stderr, "%s: Nothing to do...\n", argv [0]) ; + failUsage () ; + } + + while (arg != argc) + { + command = argv [arg] ; + /**/ if (strcasecmp (command, "clear") == 0) arg += doClear () ; + else if (strcasecmp (command, "cls") == 0) arg += doClear () ; + else if (strcasecmp (command, "bright") == 0) arg += doBright (arg, argc, argv) ; + else if (strcasecmp (command, "plot") == 0) arg += doPlot (arg, argc, argv) ; + else if (strcasecmp (command, "line") == 0) arg += doLine (arg, argc, argv) ; + else if (strcasecmp (command, "lineto") == 0) arg += doLineTo (arg, argc, argv) ; + else if (strcasecmp (command, "wait") == 0) arg += doWait (arg, argc, argv) ; + else if (strcasecmp (command, "delay") == 0) arg += doWait (arg, argc, argv) ; + else if (strcasecmp (command, "speed") == 0) arg += doSpeed (arg, argc, argv) ; + else if (strcasecmp (command, "scroll") == 0) arg += doScroll (arg, argc, argv) ; + else + { + fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [arg]) ; + failUsage () ; + } + } + + return 0 ; +} diff --git a/examples/scrollPhat/test.c b/examples/scrollPhat/test.c new file mode 100644 index 0000000..31e4311 --- /dev/null +++ b/examples/scrollPhat/test.c @@ -0,0 +1,115 @@ +/* + * test.c: + * Little test program forthe Pimoroni Scroll Phat. + * + * Copyright (c) 2015-2016 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include + +#include + + +/* + * prompt: + * Simple prompt & wait + ********************************************************************************* + */ + +static void prompt (const char *p) +{ + printf (" %s. Press ENTER: ", p) ; + (void)getchar () ; +} + + +/* + * the works + ********************************************************************************* + */ + +int main (void) +{ + int x, y ; + + printf ("\n") ; + printf ("Scroll Phat Test program\n") ; + printf ("========================\n") ; + + if (scrollPhatSetup () != 0) + { + printf ("Unable to initialise the scrollPhat: %s\n", strerror (errno)) ; + exit (1) ; + } + + printf ("-> Scroll Phat initialised OK\n") ; + printf ("... Basic display tests.\n\n") ; + + prompt ("Display ought to be blank") ; + +// Light all pixels using one point at a time + + for (y = 0 ; y < 5 ; ++y) + for (x = 0 ; x < 12 ; ++x) + scrollPhatPoint (x, y, 1) ; + scrollPhatUpdate () ; + + prompt ("Display ought to be all lit-up") ; + +// Big rectangle + + scrollPhatClear () ; + scrollPhatRectangle (0,0, 10, 4, 1, 0) ; + scrollPhatUpdate () ; + + prompt ("There should now be a rectangle round the outside") ; + + scrollPhatLine (0,0, 10,4, 1) ; + scrollPhatLine (0,4, 10,0, 1) ; + scrollPhatUpdate () ; + + prompt ("Diagonal lines") ; + + scrollPhatIntensity (1) ; + + prompt ("Minimum brightness") ; + + scrollPhatIntensity (100) ; + + prompt ("Maximum brightness") ; + + scrollPhatIntensity (10) ; + + prompt ("Default brightness") ; + + scrollPhatClear () ; + + printf (" Message Test...Press Ctrl-C to exit: ") ; + fflush (stdout) ; + + scrollPhatPrintSpeed (20) ; + for (;;) + scrollPhatPuts (" Welcome to the scroll phat from Pimoroni ") ; + + return 0 ; +} diff --git a/gpio/readall.c b/gpio/readall.c index 2d90cd1..278e6ee 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -223,19 +223,20 @@ void cmReadall (void) printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ; printf ("+-----+------+-------+ +-----+------+-------+\n") ; - for (pin = 0 ; pin < 28 ; ++pin) + for (pin = 0 ; pin < 27 ; ++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 ("| %3d ", pin + 27) ; + printf ("| %-4s ", alts [getAlt (pin + 27)]) ; + printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ; printf ("|\n") ; } printf ("+-----+------+-------+ +-----+------+-------+\n") ; + } diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 6bbcc5d..acb6e58 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -55,7 +55,7 @@ SRC = wiringPi.c \ sr595.c \ pcf8574.c pcf8591.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ - max31855.c max5322.c \ + max31855.c max5322.c ads1115.c \ sn3218.c \ drcSerial.c \ wpiExtensions.c @@ -69,7 +69,7 @@ HEADERS = wiringPi.h \ sr595.h \ pcf8574.h pcf8591.h \ mcp3002.h mcp3004.h mcp4802.h mcp3422.h \ - max31855.h max5322.h \ + max31855.h max5322.h ads1115.h \ sn3218.h \ drcSerial.h \ wpiExtensions.h diff --git a/wiringPi/ads1115.c b/wiringPi/ads1115.c new file mode 100644 index 0000000..62e2a7f --- /dev/null +++ b/wiringPi/ads1115.c @@ -0,0 +1,293 @@ +/* + * ads1115.c: + * Extend wiringPi with the ADS1115 I2C 16-bit ADC + * Copyright (c) 2016 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 . + *********************************************************************** + */ + +/* + ********************************************************************************* + * We're going to work in a hybrid mode to fit in with the wiringPi way of + * doing things, so there will be 4 analog pin which read the 4 single-ended + * channels as usual, also some fake digitalOutputs - these are the control + * registers that allow the user to put it into single/diff mode, set the + * gain and data rates. + ********************************************************************************* + */ + +#include +#include +#include + +#include +#include + +#include "ads1115.h" + +// Bits in the config register (it's a 16-bit register) + +#define CONFIG_OS_MASK (0x8000) // Operational Status Register +#define CONFIG_OS_SINGLE (0x8000) // Write - Starts a single-conversion + // Read 1 = Conversion complete + +// The multiplexor + +#define CONFIG_MUX_MASK (0x7000) + +// Differential modes + +#define CONFIG_MUX_DIFF_0_1 (0x0000) // Pos = AIN0, Neg = AIN1 (default) +#define CONFIG_MUX_DIFF_0_3 (0x1000) // Pos = AIN0, Neg = AIN3 +#define CONFIG_MUX_DIFF_1_3 (0x2000) // Pos = AIN1, Neg = AIN3 +#define CONFIG_MUX_DIFF_2_3 (0x3000) // Pos = AIN2, Neg = AIN3 (2nd differential channel) + +// Single-ended modes + +#define CONFIG_MUX_SINGLE_0 (0x4000) // AIN0 +#define CONFIG_MUX_SINGLE_1 (0x5000) // AIN1 +#define CONFIG_MUX_SINGLE_2 (0x6000) // AIN2 +#define CONFIG_MUX_SINGLE_3 (0x7000) // AIN3 + +// Programmable Gain Amplifier + +#define CONFIG_PGA_MASK (0x0E00) +#define CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3 +#define CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1 +#define CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default) +#define CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4 +#define CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8 +#define CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16 + +#define CONFIG_MODE (0x0100) // 0 is continuous, 1 is single-shot (default) + +// Data Rate + +#define CONFIG_DR_MASK (0x00E0) +#define CONFIG_DR_8SPS (0x0000) // 8 samples per second +#define CONFIG_DR_16SPS (0x0020) // 16 samples per second +#define CONFIG_DR_32SPS (0x0040) // 32 samples per second +#define CONFIG_DR_64SPS (0x0060) // 64 samples per second +#define CONFIG_DR_128SPS (0x0080) // 128 samples per second (default) +#define CONFIG_DR_475SPS (0x00A0) // 475 samples per second +#define CONFIG_DR_860SPS (0x00C0) // 860 samples per second + +// Comparator mode + +#define CONFIG_CMODE_MASK (0x0010) +#define CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default) +#define CONFIG_CMODE_WINDOW (0x0010) // Window comparator + +// Comparator polarity - the polarity of the output alert/rdy pin + +#define CONFIG_CPOL_MASK (0x0008) +#define CONFIG_CPOL_ACTVLOW (0x0000) // Active low (default) +#define CONFIG_CPOL_ACTVHI (0x0008) // Active high + +// Latching comparator - does the alert/rdy pin latch + +#define CONFIG_CLAT_MASK (0x0004) +#define CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default) +#define CONFIG_CLAT_LATCH (0x0004) // Latching comparator + +// Comparitor queue + +#define CONFIG_CQUE_MASK (0x0003) +#define CONFIG_CQUE_1CONV (0x0000) // Assert after one conversions +#define CONFIG_CQUE_2CONV (0x0001) // Assert after two conversions +#define CONFIG_CQUE_4CONV (0x0002) // Assert after four conversions +#define CONFIG_CQUE_NONE (0x0003) // Disable the comparator (default) + +#define CONFIG_DEFAULT (0x8583) // From the datasheet + + +static const uint16_t dataRates [8] = +{ + CONFIG_DR_8SPS, CONFIG_DR_16SPS, CONFIG_DR_32SPS, CONFIG_DR_64SPS, CONFIG_DR_128SPS, CONFIG_DR_475SPS, CONFIG_DR_860SPS +} ; + +static const uint16_t gains [6] = +{ + CONFIG_PGA_6_144V, CONFIG_PGA_4_096V, CONFIG_PGA_2_048V, CONFIG_PGA_1_024V, CONFIG_PGA_0_512V, CONFIG_PGA_0_256V +} ; + + +/* + * analogRead: + * Pin is the channel to sample on the device. + * Channels 0-3 are single ended inputs, + * channels 4-7 are the various differential combinations. + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + int chan = pin - node->pinBase ; + int16_t result ; + uint16_t config = CONFIG_DEFAULT ; + + chan &= 7 ; + +// Setup the configuration register + +// Set PGA/voltage range + + config &= ~CONFIG_PGA_MASK ; + config |= node->data0 ; + +// Set sample speed + + config &= ~CONFIG_DR_MASK ; + config |= node->data1 ; + +// Set single-ended channel or differential mode + + config &= ~CONFIG_MUX_MASK ; + + switch (chan) + { + case 0: config |= CONFIG_MUX_SINGLE_0 ; break ; + case 1: config |= CONFIG_MUX_SINGLE_1 ; break ; + case 2: config |= CONFIG_MUX_SINGLE_2 ; break ; + case 3: config |= CONFIG_MUX_SINGLE_3 ; break ; + + case 4: config |= CONFIG_MUX_DIFF_0_1 ; break ; + case 5: config |= CONFIG_MUX_DIFF_2_3 ; break ; + case 6: config |= CONFIG_MUX_DIFF_0_3 ; break ; + case 7: config |= CONFIG_MUX_DIFF_1_3 ; break ; + } + +// Start a single conversion + + config |= CONFIG_OS_SINGLE ; + config = __bswap_16 (config) ; + wiringPiI2CWriteReg16 (node->fd, 1, config) ; + +// Wait for the conversion to complete + + for (;;) + { + result = wiringPiI2CReadReg16 (node->fd, 1) ; + result = __bswap_16 (result) ; + if ((result & CONFIG_OS_MASK) != 0) + break ; + delayMicroseconds (100) ; + } + + result = wiringPiI2CReadReg16 (node->fd, 0) ; + result = __bswap_16 (result) ; + +// Sometimes with a 0v input on a single-ended channel the internal 0v reference +// can be higher than the input, so you get a negative result... + + if ( (chan < 4) && (result < 0) ) + return 0 ; + else + return (int)result ; +} + + +/* + * digitalWrite: + * It may seem odd to have a digital write here, but it's the best way + * to pass paramters into the chip in the wiringPi way of things. + * We have 2 digital registers: + * 0 is the gain control + * 1 is the data rate control + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int data) +{ + int chan = pin - node->pinBase ; + chan &= 3 ; + + if (chan == 0) // Gain Control + { + if ( (data < 0) || (data > 6) ) // Use default if out of range + data = 2 ; + node->data0 = gains [data] ; + } + else // Data rate control + { + if ( (data < 0) || (data > 7) ) // Use default if out of range + data = 4 ; + node->data0 = dataRates [data] ; + } + +} + + +/* + * analogWrite: + * We're using this to write to the 2 comparitor threshold registers. + * We could use a digitalWrite here but as it's an analog comparison + * then it feels better to do it this way. + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int data) +{ + int chan = pin - node->pinBase ; + int reg ; + int16_t ndata ; + + chan &= 3 ; + + reg = chan + 2 ; + + /**/ if (data < -32767) + ndata = -32767 ; + else if (data > 32767) + ndata = 32767 ; + else + ndata = (int16_t)data ; + + ndata = __bswap_16 (ndata) ; + wiringPiI2CWriteReg16 (node->fd, reg, data) ; +} + + + +/* + * ads1115Setup: + * Create a new wiringPi device node for an ads1115 on the Pi's + * I2C interface. + ********************************************************************************* + */ + +int ads1115Setup (const int pinBase, int i2cAddr) +{ + struct wiringPiNodeStruct *node ; + int fd ; + + if ((fd = wiringPiI2CSetup (i2cAddr)) < 0) + return -1 ; + + node = wiringPiNewNode (pinBase, 8) ; + + node->fd = fd ; + node->data0 = CONFIG_PGA_4_096V ; // Gain in data0 + node->data1 = CONFIG_DR_128SPS ; // Samples/sec in data1 + node->analogRead = myAnalogRead ; + node->analogWrite = myAnalogWrite ; + node->digitalWrite = myDigitalWrite ; + + return 0 ; +} diff --git a/wiringPi/ads1115.h b/wiringPi/ads1115.h new file mode 100644 index 0000000..5c91735 --- /dev/null +++ b/wiringPi/ads1115.h @@ -0,0 +1,55 @@ +/* + * ads1115.c: + * Extend wiringPi with the ADS1115 I2C 16-bit ADC + * Copyright (c) 2016 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 . + *********************************************************************** + */ + +// Constants for some of the internal functions + +// Gain + +#define ADS1115_GAIN_6 0 +#define ADS1115_GAIN_4 1 +#define ADS1115_GAIN_2 2 +#define ADS1115_GAIN_1 3 +#define ADS1115_GAIN_HALF 4 +#define ADS1115_GAIN_QUARTER 5 + +// Data rate + +#define ADS1115_DR_8 0 +#define ADS1115_DR_16 1 +#define ADS1115_DR_32 2 +#define ADS1115_DR_64 3 +#define ADS1115_DR_128 4 +#define ADS1115_DR_250 5 +#define ADS1115_DR_475 6 +#define ADS1115_DR_860 7 + +#ifdef __cplusplus +extern "C" { +#endif + +extern int ads1115Setup (int pinBase, int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 503151f..03c97c5 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -461,6 +461,7 @@ static uint8_t gpioToShift [] = 0,3,6,9,12,15,18,21,24,27, 0,3,6,9,12,15,18,21,24,27, 0,3,6,9,12,15,18,21,24,27, + 0,3,6,9,12,15,18,21,24,27, } ; @@ -1970,6 +1971,16 @@ int wiringPiSetup (void) int fd ; int boardRev ; int model, rev, mem, maker, overVolted ; + static int alreadyCalled = FALSE ; + +// This is here to trap the unwary - those who's program appears to work then fails some +// time later with a weird error message because you run out of file-handles. + + if (alreadyCalled) + (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup*: You must only call this once per program run. This is a fatal error. Please fix your code.\n") ; + + alreadyCalled = TRUE ; + if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; @@ -1994,12 +2005,14 @@ int wiringPiSetup (void) pinToGpio = pinToGpioR1 ; physToGpio = physToGpioR1 ; } - else // A, B, Rev 2, B+, CM, Pi2 + else // A, B, Rev 2, B+, CM, Pi2, Zero { pinToGpio = pinToGpioR2 ; physToGpio = physToGpioR2 ; } +// Note that a Zero is a model 1 + if (piModel2) RASPBERRY_PI_PERI_BASE = 0x3F000000 ; else @@ -2153,6 +2166,15 @@ int wiringPiSetupSys (void) int boardRev ; int pin ; char fName [128] ; + static int alreadyCalled = FALSE ; + +// This is here to trap the unwary - those who's program appears to work then fails some +// time later with a weird error message because you run out of file-handles. + + if (alreadyCalled) + (void)wiringPiFailure (WPI_FATAL, "wiringPiSetupSys: You must only call this once per program run. This is a fatal error. Please fix your code.\n") ; + + alreadyCalled = TRUE ; if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; diff --git a/wiringPi/wpiExtensions.c b/wiringPi/wpiExtensions.c index 4cae9c4..b25428c 100644 --- a/wiringPi/wpiExtensions.c +++ b/wiringPi/wpiExtensions.c @@ -52,6 +52,7 @@ #include "mcp3422.h" #include "max31855.h" #include "max5322.h" +#include "ads1115.h" #include "sn3218.h" #include "drcSerial.h" @@ -375,6 +376,32 @@ static int doExtensionPcf8574 (char *progName, int pinBase, char *params) /* + * doExtensionAds1115: + * Analog Input + * ads1115:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionAds1115 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; + return FALSE ; + } + + ads1115Setup (pinBase, i2c) ; + + return TRUE ; +} + + +/* * doExtensionPcf8591: * Analog IO * pcf8591:base:i2cAddr @@ -654,6 +681,7 @@ static struct extensionFunctionStruct extensionFunctions [] = { "mcp4802", &doExtensionMcp4802 }, { "mcp3422", &doExtensionMcp3422 }, { "max31855", &doExtensionMax31855 }, + { "ads1115", &doExtensionAds1115 }, { "max5322", &doExtensionMax5322 }, { "sn3218", &doExtensionSn3218 }, { "drcs", &doExtensionDrcS },