Update librairies + optimizations

loop time and memory optimization
This commit is contained in:
sinseman
2015-02-11 18:26:16 +01:00
parent 982dcb7ff9
commit 5737aff17a
20 changed files with 1400 additions and 418 deletions

View File

@@ -104,9 +104,40 @@ Adafruit_PCD8544::Adafruit_PCD8544(int8_t SCLK, int8_t DIN, int8_t DC,
_cs = -1;
}
Adafruit_PCD8544::Adafruit_PCD8544(int8_t DC, int8_t CS, int8_t RST):
Adafruit_GFX(LCDWIDTH, LCDHEIGHT) {
// -1 for din and sclk specify using hardware SPI
_din = -1;
_sclk = -1;
_dc = DC;
_rst = RST;
_cs = CS;
}
// the most basic function, set a single pixel
void Adafruit_PCD8544::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((x < 0) || (x >= _width) || (y < 0) || (y >= _height))
return;
int16_t t;
switch(rotation){
case 1:
t = x;
x = y;
y = LCDHEIGHT - 1 - t;
break;
case 2:
x = LCDWIDTH - 1 - x;
y = LCDHEIGHT - 1 - y;
break;
case 3:
t = x;
x = LCDWIDTH - 1 - y;
y = t;
break;
}
if ((x < 0) || (x >= LCDWIDTH) || (y < 0) || (y >= LCDHEIGHT))
return;
@@ -129,15 +160,34 @@ uint8_t Adafruit_PCD8544::getPixel(int8_t x, int8_t y) {
}
void Adafruit_PCD8544::begin(uint8_t contrast) {
// set pin directions
pinMode(_din, OUTPUT);
pinMode(_sclk, OUTPUT);
void Adafruit_PCD8544::begin(uint8_t contrast, uint8_t bias) {
if (isHardwareSPI()) {
// Setup hardware SPI.
SPI.begin();
SPI.setClockDivider(PCD8544_SPI_CLOCK_DIV);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
}
else {
// Setup software SPI.
// Set software SPI specific pin outputs.
pinMode(_din, OUTPUT);
pinMode(_sclk, OUTPUT);
// Set software SPI ports and masks.
clkport = portOutputRegister(digitalPinToPort(_sclk));
clkpinmask = digitalPinToBitMask(_sclk);
mosiport = portOutputRegister(digitalPinToPort(_din));
mosipinmask = digitalPinToBitMask(_din);
}
// Set common pin outputs.
pinMode(_dc, OUTPUT);
if (_rst > 0)
pinMode(_rst, OUTPUT);
pinMode(_rst, OUTPUT);
if (_cs > 0)
pinMode(_cs, OUTPUT);
pinMode(_cs, OUTPUT);
// toggle RST low to reset
if (_rst > 0) {
@@ -146,20 +196,11 @@ void Adafruit_PCD8544::begin(uint8_t contrast) {
digitalWrite(_rst, HIGH);
}
clkport = portOutputRegister(digitalPinToPort(_sclk));
clkpinmask = digitalPinToBitMask(_sclk);
mosiport = portOutputRegister(digitalPinToPort(_din));
mosipinmask = digitalPinToBitMask(_din);
csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
dcport = portOutputRegister(digitalPinToPort(_dc));
dcpinmask = digitalPinToBitMask(_dc);
// get into the EXTENDED mode!
command(PCD8544_FUNCTIONSET | PCD8544_EXTENDEDINSTRUCTION );
// LCD bias select (4 is optimal?)
command(PCD8544_SETBIAS | 0x4);
command(PCD8544_SETBIAS | bias);
// set VOP
if (contrast > 0x7f)
@@ -187,25 +228,31 @@ void Adafruit_PCD8544::begin(uint8_t contrast) {
}
inline void Adafruit_PCD8544::fastSPIwrite(uint8_t d) {
for(uint8_t bit = 0x80; bit; bit >>= 1) {
*clkport &= ~clkpinmask;
if(d & bit) *mosiport |= mosipinmask;
else *mosiport &= ~mosipinmask;
*clkport |= clkpinmask;
inline void Adafruit_PCD8544::spiWrite(uint8_t d) {
if (isHardwareSPI()) {
// Hardware SPI write.
SPI.transfer(d);
}
else {
// Software SPI write with bit banging.
for(uint8_t bit = 0x80; bit; bit >>= 1) {
*clkport &= ~clkpinmask;
if(d & bit) *mosiport |= mosipinmask;
else *mosiport &= ~mosipinmask;
*clkport |= clkpinmask;
}
}
}
inline void Adafruit_PCD8544::slowSPIwrite(uint8_t c) {
shiftOut(_din, _sclk, MSBFIRST, c);
bool Adafruit_PCD8544::isHardwareSPI() {
return (_din == -1 && _sclk == -1);
}
void Adafruit_PCD8544::command(uint8_t c) {
digitalWrite(_dc, LOW);
if (_cs > 0)
digitalWrite(_cs, LOW);
fastSPIwrite(c);
spiWrite(c);
if (_cs > 0)
digitalWrite(_cs, HIGH);
}
@@ -214,7 +261,7 @@ void Adafruit_PCD8544::data(uint8_t c) {
digitalWrite(_dc, HIGH);
if (_cs > 0)
digitalWrite(_cs, LOW);
fastSPIwrite(c);
spiWrite(c);
if (_cs > 0)
digitalWrite(_cs, HIGH);
}
@@ -242,7 +289,7 @@ void Adafruit_PCD8544::display(void) {
}
if (yUpdateMax < p*8) {
break;
}d
}
#endif
command(PCD8544_SETYADDR | p);
@@ -263,9 +310,7 @@ void Adafruit_PCD8544::display(void) {
if (_cs > 0)
digitalWrite(_cs, LOW);
for(; col <= maxcol; col++) {
//uart_putw_dec(col);
//uart_putchar(' ');
fastSPIwrite(pcd8544_buffer[(LCDWIDTH*p)+col]);
spiWrite(pcd8544_buffer[(LCDWIDTH*p)+col]);
}
if (_cs > 0)
digitalWrite(_cs, HIGH);

View File

@@ -15,6 +15,8 @@ Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/
#ifndef _ADAFRUIT_PCD8544_H
#define _ADAFRUIT_PCD8544_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
@@ -23,6 +25,8 @@ All text above, and the splash screen must be included in any redistribution
#include "pins_arduino.h"
#endif
#include <SPI.h>
#define BLACK 1
#define WHITE 0
@@ -49,12 +53,21 @@ All text above, and the splash screen must be included in any redistribution
#define PCD8544_SETBIAS 0x10
#define PCD8544_SETVOP 0x80
// Default to max SPI clock speed for PCD8544 of 4 mhz (16mhz / 4) for normal Arduinos.
// This can be modified to change the clock speed if necessary (like for supporting other hardware).
#define PCD8544_SPI_CLOCK_DIV SPI_CLOCK_DIV4
class Adafruit_PCD8544 : public Adafruit_GFX {
public:
// Software SPI with explicit CS pin.
Adafruit_PCD8544(int8_t SCLK, int8_t DIN, int8_t DC, int8_t CS, int8_t RST);
// Software SPI with CS tied to ground. Saves a pin but other pins can't be shared with other hardware.
Adafruit_PCD8544(int8_t SCLK, int8_t DIN, int8_t DC, int8_t RST);
// Hardware SPI based on hardware controlled SCK (SCLK) and MOSI (DIN) pins. CS is still controlled by any IO pin.
// NOTE: MISO and SS will be set as an input and output respectively, so be careful sharing those pins!
Adafruit_PCD8544(int8_t DC, int8_t CS, int8_t RST);
void begin(uint8_t contrast = 40);
void begin(uint8_t contrast = 40, uint8_t bias = 0x04);
void command(uint8_t c);
void data(uint8_t c);
@@ -68,9 +81,11 @@ class Adafruit_PCD8544 : public Adafruit_GFX {
private:
int8_t _din, _sclk, _dc, _rst, _cs;
volatile uint8_t *mosiport, *clkport, *csport, *dcport;
uint8_t mosipinmask, clkpinmask, cspinmask, dcpinmask;
volatile uint8_t *mosiport, *clkport;
uint8_t mosipinmask, clkpinmask;
void slowSPIwrite(uint8_t c);
void fastSPIwrite(uint8_t c);
void spiWrite(uint8_t c);
bool isHardwareSPI();
};
#endif

View File

@@ -0,0 +1,350 @@
/*********************************************************************
This is an example sketch for our Monochrome Nokia 5110 LCD Displays
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/338
These displays use SPI to communicate, 4 or 5 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
// Hardware SPI (faster, but must use certain hardware pins):
// SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
// MOSI is LCD DIN - this is pin 11 on an Arduino Uno
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
// Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
// Note with hardware SPI MISO and SS pins aren't used but will still be read
// and written to during SPI transfer. Be careful sharing these pins!
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
void setup() {
Serial.begin(9600);
display.begin();
// init done
// you can change the contrast around to adapt the display
// for the best viewing!
display.setContrast(50);
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer
// draw a single pixel
display.drawPixel(10, 10, BLACK);
display.display();
delay(2000);
display.clearDisplay();
// draw many lines
testdrawline();
display.display();
delay(2000);
display.clearDisplay();
// draw rectangles
testdrawrect();
display.display();
delay(2000);
display.clearDisplay();
// draw multiple rectangles
testfillrect();
display.display();
delay(2000);
display.clearDisplay();
// draw mulitple circles
testdrawcircle();
display.display();
delay(2000);
display.clearDisplay();
// draw a circle, 10 pixel radius
display.fillCircle(display.width()/2, display.height()/2, 10, BLACK);
display.display();
delay(2000);
display.clearDisplay();
testdrawroundrect();
delay(2000);
display.clearDisplay();
testfillroundrect();
delay(2000);
display.clearDisplay();
testdrawtriangle();
delay(2000);
display.clearDisplay();
testfilltriangle();
delay(2000);
display.clearDisplay();
// draw the first ~12 characters in the font
testdrawchar();
display.display();
delay(2000);
display.clearDisplay();
// text display tests
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Hello, world!");
display.setTextColor(WHITE, BLACK); // 'inverted' text
display.println(3.141592);
display.setTextSize(2);
display.setTextColor(BLACK);
display.print("0x"); display.println(0xDEADBEEF, HEX);
display.display();
delay(2000);
// rotation example
display.clearDisplay();
display.setRotation(1); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further.
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Rotation");
display.setTextSize(2);
display.println("Example!");
display.display();
delay(2000);
// revert back to no rotation
display.setRotation(0);
// miniature bitmap display
display.clearDisplay();
display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1);
display.display();
// invert the display
display.invertDisplay(true);
delay(1000);
display.invertDisplay(false);
delay(1000);
// draw a bitmap icon and 'animate' movement
testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT);
}
void loop() {
}
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
uint8_t icons[NUMFLAKES][3];
srandom(666); // whatever seed
// initialize
for (uint8_t f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random() % display.width();
icons[f][YPOS] = 0;
icons[f][DELTAY] = random() % 5 + 1;
Serial.print("x: ");
Serial.print(icons[f][XPOS], DEC);
Serial.print(" y: ");
Serial.print(icons[f][YPOS], DEC);
Serial.print(" dy: ");
Serial.println(icons[f][DELTAY], DEC);
}
while (1) {
// draw each icon
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, BLACK);
}
display.display();
delay(200);
// then erase it + move it
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, WHITE);
// move it
icons[f][YPOS] += icons[f][DELTAY];
// if its gone, reinit
if (icons[f][YPOS] > display.height()) {
icons[f][XPOS] = random() % display.width();
icons[f][YPOS] = 0;
icons[f][DELTAY] = random() % 5 + 1;
}
}
}
}
void testdrawchar(void) {
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
for (uint8_t i=0; i < 168; i++) {
if (i == '\n') continue;
display.write(i);
//if ((i > 0) && (i % 14 == 0))
//display.println();
}
display.display();
}
void testdrawcircle(void) {
for (int16_t i=0; i<display.height(); i+=2) {
display.drawCircle(display.width()/2, display.height()/2, i, BLACK);
display.display();
}
}
void testfillrect(void) {
uint8_t color = 1;
for (int16_t i=0; i<display.height()/2; i+=3) {
// alternate colors
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
display.display();
color++;
}
}
void testdrawtriangle(void) {
for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
display.drawTriangle(display.width()/2, display.height()/2-i,
display.width()/2-i, display.height()/2+i,
display.width()/2+i, display.height()/2+i, BLACK);
display.display();
}
}
void testfilltriangle(void) {
uint8_t color = BLACK;
for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
display.fillTriangle(display.width()/2, display.height()/2-i,
display.width()/2-i, display.height()/2+i,
display.width()/2+i, display.height()/2+i, color);
if (color == WHITE) color = BLACK;
else color = WHITE;
display.display();
}
}
void testdrawroundrect(void) {
for (int16_t i=0; i<display.height()/2-2; i+=2) {
display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, BLACK);
display.display();
}
}
void testfillroundrect(void) {
uint8_t color = BLACK;
for (int16_t i=0; i<display.height()/2-2; i+=2) {
display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
if (color == WHITE) color = BLACK;
else color = WHITE;
display.display();
}
}
void testdrawrect(void) {
for (int16_t i=0; i<display.height()/2; i+=2) {
display.drawRect(i, i, display.width()-2*i, display.height()-2*i, BLACK);
display.display();
}
}
void testdrawline() {
for (int16_t i=0; i<display.width(); i+=4) {
display.drawLine(0, 0, i, display.height()-1, BLACK);
display.display();
}
for (int16_t i=0; i<display.height(); i+=4) {
display.drawLine(0, 0, display.width()-1, i, BLACK);
display.display();
}
delay(250);
display.clearDisplay();
for (int16_t i=0; i<display.width(); i+=4) {
display.drawLine(0, display.height()-1, i, 0, BLACK);
display.display();
}
for (int8_t i=display.height()-1; i>=0; i-=4) {
display.drawLine(0, display.height()-1, display.width()-1, i, BLACK);
display.display();
}
delay(250);
display.clearDisplay();
for (int16_t i=display.width()-1; i>=0; i-=4) {
display.drawLine(display.width()-1, display.height()-1, i, 0, BLACK);
display.display();
}
for (int16_t i=display.height()-1; i>=0; i-=4) {
display.drawLine(display.width()-1, display.height()-1, 0, i, BLACK);
display.display();
}
delay(250);
display.clearDisplay();
for (int16_t i=0; i<display.height(); i+=4) {
display.drawLine(display.width()-1, 0, 0, i, BLACK);
display.display();
}
for (int16_t i=0; i<display.width(); i+=4) {
display.drawLine(display.width()-1, 0, i, display.height()-1, BLACK);
display.display();
}
delay(250);
}