From ba91e0b6c2de45511bc553f1b3c0c8abe79eed10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 23 Mar 2017 17:53:32 +0100 Subject: [PATCH] MPPT algorithm update, 20A support and OLED display --- img/arrow_right.bmp | Bin 0 -> 166 bytes img/arrow_right.c | 8 + img/display_top.png | Bin 0 -> 1274 bytes img/load.bmp | Bin 0 -> 822 bytes img/load.c | 9 + img/load.xbm | 5 + img/panel.bmp | Bin 0 -> 822 bytes img/panel.c | 10 + img/panel.png | Bin 0 -> 978 bytes img/panel.xbm | 5 + img/x.bmp | Bin 0 -> 822 bytes img/x.c | 9 + lib/Adafruit_GFX/Adafruit_GFX.cpp | 454 +++++++++++++++++ lib/Adafruit_GFX/Adafruit_GFX.h | 183 +++++++ lib/Adafruit_GFX/Adafruit_GFX_Config.h | 14 + lib/Adafruit_GFX/Adafruit_SSD1306.cpp | 305 ++++++++++++ lib/Adafruit_GFX/Adafruit_SSD1306.h | 220 +++++++++ lib/Adafruit_GFX/glcdfont.h | 285 +++++++++++ lib/BuckConverter/BuckConverter.cpp | 217 +++++++++ lib/BuckConverter/BuckConverter.h | 108 +++++ src/config.h | 104 ++++ src/config_12a.h | 104 ++++ src/display.h | 20 + src/main.cpp | 646 ++++++++++++++----------- 24 files changed, 2413 insertions(+), 293 deletions(-) create mode 100644 img/arrow_right.bmp create mode 100644 img/arrow_right.c create mode 100644 img/display_top.png create mode 100644 img/load.bmp create mode 100644 img/load.c create mode 100644 img/load.xbm create mode 100644 img/panel.bmp create mode 100644 img/panel.c create mode 100644 img/panel.png create mode 100644 img/panel.xbm create mode 100644 img/x.bmp create mode 100644 img/x.c create mode 100644 lib/Adafruit_GFX/Adafruit_GFX.cpp create mode 100644 lib/Adafruit_GFX/Adafruit_GFX.h create mode 100644 lib/Adafruit_GFX/Adafruit_GFX_Config.h create mode 100644 lib/Adafruit_GFX/Adafruit_SSD1306.cpp create mode 100644 lib/Adafruit_GFX/Adafruit_SSD1306.h create mode 100644 lib/Adafruit_GFX/glcdfont.h create mode 100644 lib/BuckConverter/BuckConverter.cpp create mode 100644 lib/BuckConverter/BuckConverter.h create mode 100644 src/config.h create mode 100644 src/config_12a.h create mode 100644 src/display.h diff --git a/img/arrow_right.bmp b/img/arrow_right.bmp new file mode 100644 index 0000000000000000000000000000000000000000..3e40fd46dfdccb9538cbcf41b0e65bc5011ded9e GIT binary patch literal 166 zcmZ?rUB&kMT02jMS A7XSbN literal 0 HcmV?d00001 diff --git a/img/arrow_right.c b/img/arrow_right.c new file mode 100644 index 0000000..9d4f227 --- /dev/null +++ b/img/arrow_right.c @@ -0,0 +1,8 @@ +//------------------------------------------------------------------------------ +// File generated by LCD Assistant +// http://en.radzio.dxp.pl/bitmap_converter/ +//------------------------------------------------------------------------------ + +const unsigned char arrow_right [] = { +0x41, 0x63, 0x36, 0x1C, +}; diff --git a/img/display_top.png b/img/display_top.png new file mode 100644 index 0000000000000000000000000000000000000000..33fd9f3c8bf565b79d268c4d688d5ce955a9b387 GIT binary patch literal 1274 zcmeAS@N?(olHy`uVBq!ia0vp^4M6O`!3-pya`gKGDaPU;cPEB*=VV?2IV|apzK#qG z8~eHcB(gFvFf#=Bgt!95|Ns9FBuBw$2#mlGNH2Sn4D>c6&HLz5&p1J9^zw7)UCs`bFT>)))NZ>wKp&vauOt3j#Dy9<+d^2|}_58gRxae7*kRMe+`=7X{>&2Ay3ev10G3KjI8u=WL8J;c8o5p}l?=qi0l_BK6PcN;~}f6n4SXNTb@b?z8fY zPg6ICEM1baPwzm+PyLQIDa|SS+79qJeiLfy<5|ZArJju<>${an^LB{Ts5 DoY~Bt literal 0 HcmV?d00001 diff --git a/img/load.bmp b/img/load.bmp new file mode 100644 index 0000000000000000000000000000000000000000..f1ee91fa7b96a8f1856a9a761e0061493bedef45 GIT binary patch literal 822 zcmd^*K@Na02m~?SjYps03;F=>{@Q;jcH0esV0xDXvR!65^f=2bwTjMNdu=_*R{eKe z3%VQkkE{QI4E}-gN@9LQ2soa8mQ?_i0oGTNPpfG{@StU&xpEN(>Dw&LC~$BDj!blT O->x!E*a^$K6`l`xm!p3G literal 0 HcmV?d00001 diff --git a/img/load.c b/img/load.c new file mode 100644 index 0000000..ad893d6 --- /dev/null +++ b/img/load.c @@ -0,0 +1,9 @@ +//------------------------------------------------------------------------------ +// File generated by LCD Assistant +// http://en.radzio.dxp.pl/bitmap_converter/ +//------------------------------------------------------------------------------ + +const unsigned char load [] = { +0x20, 0x22, 0x04, 0x70, 0x88, 0x8B, 0x88, 0x70, 0x04, 0x22, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x01, 0x00, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; diff --git a/img/load.xbm b/img/load.xbm new file mode 100644 index 0000000..45c0d8b --- /dev/null +++ b/img/load.xbm @@ -0,0 +1,5 @@ +#define load_width 11 +#define load_height 11 +static unsigned char load_bits[] = { + 0x20, 0x00, 0x22, 0x02, 0x04, 0x01, 0x70, 0x00, 0x88, 0x00, 0x8b, 0x06, + 0x88, 0x00, 0x70, 0x00, 0x52, 0x02, 0x50, 0x00, 0x70, 0x00 }; diff --git a/img/panel.bmp b/img/panel.bmp new file mode 100644 index 0000000000000000000000000000000000000000..8c999881b13feb297830ffd59a7a7b9aba645284 GIT binary patch literal 822 zcmeH@!3}^Q3`J|a8;_3Q1Ps94t=&s2ADW-GCJrD;oBDWs4D~3pv{kh4T5GRKw#J`! zE~o*nf6)t!R}o^#7&Rr)a~eBthKU5Xr4WR>L3bqP#59~WGYUrF;L{*q&09RDf0ZV< QG<8@*4m+6NH1oe}Hx~C&&;S4c literal 0 HcmV?d00001 diff --git a/img/panel.c b/img/panel.c new file mode 100644 index 0000000..08a5416 --- /dev/null +++ b/img/panel.c @@ -0,0 +1,10 @@ +//------------------------------------------------------------------------------ +// File generated by LCD Assistant +// http://en.radzio.dxp.pl/bitmap_converter/ +//------------------------------------------------------------------------------ + +const unsigned char panel. [] = { +0x10, 0x00, 0x10, 0x00, +0x60, 0x98, 0x86, 0xC9, 0x31, 0x19, 0x96, 0x62, 0x32, 0x2C, 0xC4, 0x64, 0x98, 0x08, 0xC8, 0x30, +0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x04, 0x03, 0x00, 0x00 +}; diff --git a/img/panel.png b/img/panel.png new file mode 100644 index 0000000000000000000000000000000000000000..6cbb4c7b25ba2d33cd12f90540e35e0e9b2af91a GIT binary patch literal 978 zcmeAS@N?(olHy`uVBq!ia0vp^0zk~o!3-oFZT@ltDaPU;cPEB*=VV?2IV|apzK#qG z8~eHcB(gFvFf#=Bgt!95|Ns9FBuBw$2#mlGNH2Sn4D>cYijj{&frH86 z$N%M%RW_w)TQPe&EMNXAn?0!@>GwrdvuSNJ)}7jV?!W&6t=R#}%HrCG8OnX4gKI!R N?&<31vd$@?2>?1pKgR$7 literal 0 HcmV?d00001 diff --git a/img/panel.xbm b/img/panel.xbm new file mode 100644 index 0000000..199e7a9 --- /dev/null +++ b/img/panel.xbm @@ -0,0 +1,5 @@ +#define panel_width 16 +#define panel_height 11 +static unsigned char panel_bits[] = { + 0x38, 0x00, 0xc4, 0x01, 0x44, 0x0e, 0x2a, 0x72, 0x72, 0x91, 0x91, 0x8b, + 0x89, 0x4c, 0x4e, 0x54, 0x70, 0x22, 0x80, 0x23, 0x00, 0x1c }; diff --git a/img/x.bmp b/img/x.bmp new file mode 100644 index 0000000000000000000000000000000000000000..f0f3cd0f1d3fb170c38321bb3b0837a2c5259ff6 GIT binary patch literal 822 zcmZ?rHDhJ~12Z700mKSG%mKuV3=%*R0*>%8Fn}-&{HHH~Sc_~YrUSgII~%P364ajC@RNIc?HGz!xeWS1keF;$Wwf@vV82ua2Q0Mm_{kN^Mx literal 0 HcmV?d00001 diff --git a/img/x.c b/img/x.c new file mode 100644 index 0000000..6ae957b --- /dev/null +++ b/img/x.c @@ -0,0 +1,9 @@ +//------------------------------------------------------------------------------ +// File generated by LCD Assistant +// http://en.radzio.dxp.pl/bitmap_converter/ +//------------------------------------------------------------------------------ + +const unsigned char x [] = { +0x08, 0x08, 0x08, 0x08, 0x00, 0x41, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x41, 0x00, 0x08, 0x08, +0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; diff --git a/lib/Adafruit_GFX/Adafruit_GFX.cpp b/lib/Adafruit_GFX/Adafruit_GFX.cpp new file mode 100644 index 0000000..ed4ae9f --- /dev/null +++ b/lib/Adafruit_GFX/Adafruit_GFX.cpp @@ -0,0 +1,454 @@ +/*********************************** +This is a our graphics core library, for all our displays. +We'll be adapting all the +existing libaries to use this core to make updating, support +and upgrading easier! + +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 must be included in any redistribution +****************************************/ + +/* + * Modified by Neal Horman 7/14/2012 for use in mbed + */ + +#include "mbed.h" + +#include "Adafruit_GFX.h" +#include "glcdfont.h" + +#if defined(GFX_WANT_ABSTRACTS) +// draw a circle outline +void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) +{ + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + + drawPixel(x0, y0+r, color); + drawPixel(x0, y0-r, color); + drawPixel(x0+r, y0, color); + drawPixel(x0-r, y0, color); + + while (x= 0) + { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + + drawPixel(x0 + x, y0 + y, color); + drawPixel(x0 - x, y0 + y, color); + drawPixel(x0 + x, y0 - y, color); + drawPixel(x0 - x, y0 - y, color); + drawPixel(x0 + y, y0 + x, color); + drawPixel(x0 - y, y0 + x, color); + drawPixel(x0 + y, y0 - x, color); + drawPixel(x0 - y, y0 - x, color); + } +} + +void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color) +{ + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + + while (x= 0) + { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + + if (cornername & 0x4) + { + drawPixel(x0 + x, y0 + y, color); + drawPixel(x0 + y, y0 + x, color); + } + + if (cornername & 0x2) + { + drawPixel(x0 + x, y0 - y, color); + drawPixel(x0 + y, y0 - x, color); + } + + if (cornername & 0x8) + { + drawPixel(x0 - y, y0 + x, color); + drawPixel(x0 - x, y0 + y, color); + } + + if (cornername & 0x1) + { + drawPixel(x0 - y, y0 - x, color); + drawPixel(x0 - x, y0 - y, color); + } + } +} + +void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) +{ + drawFastVLine(x0, y0-r, 2*r+1, color); + fillCircleHelper(x0, y0, r, 3, 0, color); +} + +// used to do circles and roundrects! +void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color) +{ + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + + while (x= 0) + { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + + if (cornername & 0x1) + { + drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); + drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); + } + + if (cornername & 0x2) + { + drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); + drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); + } + } +} +#endif + +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) +// bresenham's algorithm - thx wikpedia +void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) +{ + int16_t steep = abs(y1 - y0) > abs(x1 - x0); + + if (steep) + { + swap(x0, y0); + swap(x1, y1); + } + + if (x0 > x1) + { + swap(x0, x1); + swap(y0, y1); + } + + int16_t dx, dy; + dx = x1 - x0; + dy = abs(y1 - y0); + + int16_t err = dx / 2; + int16_t ystep; + + if (y0 < y1) + ystep = 1; + else + ystep = -1; + + for (; x0<=x1; x0++) + { + if (steep) + drawPixel(y0, x0, color); + else + drawPixel(x0, y0, color); + + err -= dy; + if (err < 0) + { + y0 += ystep; + err += dx; + } + } +} + +void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) +{ + // stupidest version - update in subclasses if desired! + drawLine(x, y, x, y+h-1, color); +} + +void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) +{ + // stupidest version - update in subclasses if desired! + for (int16_t i=x; i= y1 >= y0) + if (y0 > y1) + swap(y0, y1); swap(x0, x1); + + if (y1 > y2) + swap(y2, y1); swap(x2, x1); + + if (y0 > y1) + swap(y0, y1); swap(x0, x1); + + + if(y0 == y2) + { // Handle awkward all-on-same-line case as its own thing + a = b = x0; + if(x1 < a) + a = x1; + else if(x1 > b) + b = x1; + + if(x2 < a) + a = x2; + else if(x2 > b) b = x2; + drawFastHLine(a, y0, b-a+1, color); + return; + } + + int16_t + dx01 = x1 - x0, + dy01 = y1 - y0, + dx02 = x2 - x0, + dy02 = y2 - y0, + dx12 = x2 - x1, + dy12 = y2 - y1, + sa = 0, + sb = 0; + + // For upper part of triangle, find scanline crossings for segments + // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 + // is included here (and second loop will be skipped, avoiding a /0 + // error there), otherwise scanline y1 is skipped here and handled + // in the second loop...which also avoids a /0 error here if y0=y1 + // (flat-topped triangle). + if(y1 == y2) + last = y1; // Include y1 scanline + else + last = y1-1; // Skip it + + for(y=y0; y<=last; y++) + { + a = x0 + sa / dy01; + b = x0 + sb / dy02; + sa += dx01; + sb += dx02; + /* longhand: + a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); + b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); + */ + if(a > b) + swap(a,b); + drawFastHLine(a, y, b-a+1, color); + } + + // For lower part of triangle, find scanline crossings for segments + // 0-2 and 1-2. This loop is skipped if y1=y2. + sa = dx12 * (y - y1); + sb = dx02 * (y - y0); + for(; y<=y2; y++) + { + a = x1 + sa / dy12; + b = x0 + sb / dy02; + sa += dx12; + sb += dx02; + /* longhand: + a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); + b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); + */ + if(a > b) + swap(a,b); + drawFastHLine(a, y, b-a+1, color); + } +} + +void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) +{ + for (int16_t j=0; j (_width - textsize*6))) + { + cursor_y += textsize*8; + cursor_x = 0; + } + } + return 1; +} + +// draw a character +void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size) +{ + if( + (x >= _width) || // Clip right + (y >= _height) || // Clip bottom + ((x + 5 * size - 1) < 0) || // Clip left + ((y + 8 * size - 1) < 0) // Clip top + ) + return; + + for (int8_t i=0; i<6; i++ ) + { + uint8_t line = 0; + + if (i == 5) + line = 0x0; + else + line = font[(c*5)+i]; + + for (int8_t j = 0; j<8; j++) + { + if (line & 0x1) + { +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) + if (size == 1) // default size + drawPixel(x+i, y+j, color); + else // big size + fillRect(x+(i*size), y+(j*size), size, size, color); +#else + drawPixel(x+i, y+j, color); +#endif + } + else if (bg != color) + { +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) + if (size == 1) // default size + drawPixel(x+i, y+j, bg); + else // big size + fillRect(x+i*size, y+j*size, size, size, bg); +#else + drawPixel(x+i, y+j, bg); +#endif + } + line >>= 1; + } + } +} + +void Adafruit_GFX::setRotation(uint8_t x) +{ + x %= 4; // cant be higher than 3 + rotation = x; + switch (x) + { + case 0: + case 2: + _width = _rawWidth; + _height = _rawHeight; + break; + case 1: + case 3: + _width = _rawHeight; + _height = _rawWidth; + break; + } +} diff --git a/lib/Adafruit_GFX/Adafruit_GFX.h b/lib/Adafruit_GFX/Adafruit_GFX.h new file mode 100644 index 0000000..d3c2240 --- /dev/null +++ b/lib/Adafruit_GFX/Adafruit_GFX.h @@ -0,0 +1,183 @@ +/*********************************** +This is a our graphics core library, for all our displays. +We'll be adapting all the +existing libaries to use this core to make updating, support +and upgrading easier! + +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 must be included in any redistribution +****************************************/ + +/* + * Modified by Neal Horman 7/14/2012 for use in mbed + */ + +#ifndef _ADAFRUIT_GFX_H_ +#define _ADAFRUIT_GFX_H_ + +#include "Adafruit_GFX_Config.h" + +static inline void swap(int16_t &a, int16_t &b) +{ + int16_t t = a; + + a = b; + b = t; +} + +#ifndef _BV +#define _BV(bit) (1<<(bit)) +#endif + +#define BLACK 0 +#define WHITE 1 + +/** + * This is a Text and Graphics element drawing class. + * These functions draw to the display buffer. + * + * Display drivers should be derived from here. + * The Display drivers push the display buffer to the + * hardware based on application control. + * + */ +class Adafruit_GFX : public Stream +{ + public: + Adafruit_GFX(int16_t w, int16_t h) + : _rawWidth(w) + , _rawHeight(h) + , _width(w) + , _height(h) + , cursor_x(0) + , cursor_y(0) + , textcolor(WHITE) + , textbgcolor(BLACK) + , textsize(1) + , rotation(0) + , wrap(true) + {}; + + /// Paint one BLACK or WHITE pixel in the display buffer + // this must be defined by the subclass + virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; + // this is optional + virtual void invertDisplay(bool i) {}; + + // Stream implementation - provides printf() interface + // You would otherwise be forced to use writeChar() + virtual int _putc(int value) { return writeChar(value); }; + virtual int _getc() { return -1; }; + +#ifdef GFX_WANT_ABSTRACTS + // these are 'generic' drawing functions, so we can share them! + + /** Draw a Horizontal Line + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); + /** Draw a rectangle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); + /** Fill the entire display + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + virtual void fillScreen(uint16_t color); + + /** Draw a circle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); + void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color); + + /** Draw and fill a circle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); + void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color); + + /** Draw a triangle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); + /** Draw and fill a triangle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); + + /** Draw a rounded rectangle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); + /** Draw and fill a rounded rectangle + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); + /** Draw a bitmap + * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h + */ + void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); +#endif + +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) + /** Draw a line + * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h + */ + virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); + /** Draw a vertical line + * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h + */ + virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); + /** Draw and fill a rectangle + * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h + */ + virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); +#endif + + /// Draw a text character at a specified pixel location + void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size); + /// Draw a text character at the text cursor location + size_t writeChar(uint8_t); + + /// Get the width of the display in pixels + inline int16_t width(void) { return _width; }; + /// Get the height of the display in pixels + inline int16_t height(void) { return _height; }; + + /// Set the text cursor location, based on the size of the text + inline void setTextCursor(int16_t x, int16_t y) { cursor_x = x; cursor_y = y; }; +#if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) + /** Set the size of the text to be drawn + * @note Make sure to enable either GFX_SIZEABLE_TEXT or GFX_WANT_ABSTRACTS + */ + inline void setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; }; +#endif + /// Set the text foreground and background colors to be the same + inline void setTextColor(uint16_t c) { textcolor = c; textbgcolor = c; } + /// Set the text foreground and background colors independantly + inline void setTextColor(uint16_t c, uint16_t b) { textcolor = c; textbgcolor = b; }; + /// Set text wraping mode true or false + inline void setTextWrap(bool w) { wrap = w; }; + + /// Set the display rotation, 1, 2, 3, or 4 + void setRotation(uint8_t r); + /// Get the current rotation + inline uint8_t getRotation(void) { rotation %= 4; return rotation; }; + +protected: + int16_t _rawWidth, _rawHeight; // this is the 'raw' display w/h - never changes + int16_t _width, _height; // dependent on rotation + int16_t cursor_x, cursor_y; + uint16_t textcolor, textbgcolor; + uint8_t textsize; + uint8_t rotation; + bool wrap; // If set, 'wrap' text at right edge of display +}; + +#endif diff --git a/lib/Adafruit_GFX/Adafruit_GFX_Config.h b/lib/Adafruit_GFX/Adafruit_GFX_Config.h new file mode 100644 index 0000000..cd7a71e --- /dev/null +++ b/lib/Adafruit_GFX/Adafruit_GFX_Config.h @@ -0,0 +1,14 @@ +#ifndef _ADAFRUIT_GFX_CONFIG_H_ +#define _ADAFRUIT_GFX_CONFIG_H_ + +// Uncomment this to turn off the builtin splash +//#define NO_SPLASH_ADAFRUIT + +// Uncomment this to enable all functionality +#define GFX_WANT_ABSTRACTS + +// Uncomment this to enable only runtime font scaling, without all the rest of the Abstracts +//#define GFX_SIZEABLE_TEXT + + +#endif diff --git a/lib/Adafruit_GFX/Adafruit_SSD1306.cpp b/lib/Adafruit_GFX/Adafruit_SSD1306.cpp new file mode 100644 index 0000000..c4ced13 --- /dev/null +++ b/lib/Adafruit_GFX/Adafruit_SSD1306.cpp @@ -0,0 +1,305 @@ +/********************************************************************* +This is a library for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + +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 below must be included in any redistribution +*********************************************************************/ + +/* + * Modified by Neal Horman 7/14/2012 for use in mbed + */ + +#include "mbed.h" +#include "Adafruit_SSD1306.h" + +#define SSD1306_SETCONTRAST 0x81 +#define SSD1306_DISPLAYALLON_RESUME 0xA4 +#define SSD1306_DISPLAYALLON 0xA5 +#define SSD1306_NORMALDISPLAY 0xA6 +#define SSD1306_INVERTDISPLAY 0xA7 +#define SSD1306_DISPLAYOFF 0xAE +#define SSD1306_DISPLAYON 0xAF +#define SSD1306_SETDISPLAYOFFSET 0xD3 +#define SSD1306_SETCOMPINS 0xDA +#define SSD1306_SETVCOMDETECT 0xDB +#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 +#define SSD1306_SETPRECHARGE 0xD9 +#define SSD1306_SETMULTIPLEX 0xA8 +#define SSD1306_SETLOWCOLUMN 0x00 +#define SSD1306_SETHIGHCOLUMN 0x10 +#define SSD1306_SETSTARTLINE 0x40 +#define SSD1306_MEMORYMODE 0x20 +#define SSD1306_COMSCANINC 0xC0 +#define SSD1306_COMSCANDEC 0xC8 +#define SSD1306_SEGREMAP 0xA0 +#define SSD1306_CHARGEPUMP 0x8D + +void Adafruit_SSD1306::begin(uint8_t vccstate) +{ + rst = 1; + // VDD (3.3V) goes high at start, lets just chill for a ms + wait_ms(1); + // bring reset low + rst = 0; + // wait 10ms + wait_ms(10); + // bring out of reset + rst = 1; + // turn on VCC (9V?) + + command(SSD1306_DISPLAYOFF); + command(SSD1306_SETDISPLAYCLOCKDIV); + command(0x80); // the suggested ratio 0x80 + + command(SSD1306_SETMULTIPLEX); + command(_rawHeight-1); + + command(SSD1306_SETDISPLAYOFFSET); + command(0x0); // no offset + + command(SSD1306_SETSTARTLINE | 0x0); // line #0 + + command(SSD1306_CHARGEPUMP); + command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14); + + command(SSD1306_MEMORYMODE); + command(0x00); // 0x0 act like ks0108 + + command(SSD1306_SEGREMAP | 0x1); + + command(SSD1306_COMSCANDEC); + + command(SSD1306_SETCOMPINS); + command(_rawHeight == 32 ? 0x02 : 0x12); // TODO - calculate based on _rawHieght ? + + command(SSD1306_SETCONTRAST); + command(_rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) ); + + command(SSD1306_SETPRECHARGE); + command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1); + + command(SSD1306_SETVCOMDETECT); + command(0x40); + + command(SSD1306_DISPLAYALLON_RESUME); + + command(SSD1306_NORMALDISPLAY); + + command(SSD1306_DISPLAYON); +} + +// Set a single pixel +void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) +{ + if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) + return; + + // check rotation, move pixel around if necessary + switch (getRotation()) + { + case 1: + swap(x, y); + x = _rawWidth - x - 1; + break; + case 2: + x = _rawWidth - x - 1; + y = _rawHeight - y - 1; + break; + case 3: + swap(x, y); + y = _rawHeight - y - 1; + break; + } + + // x is which column + if (color == WHITE) + buffer[x+ (y/8)*_rawWidth] |= _BV((y%8)); + else // else black + buffer[x+ (y/8)*_rawWidth] &= ~_BV((y%8)); +} + +void Adafruit_SSD1306::invertDisplay(bool i) +{ + command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY); +} + +// Send the display buffer out to the display +void Adafruit_SSD1306::display(void) +{ + command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 + command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 + command(SSD1306_SETSTARTLINE | 0x0); // line #0 + sendDisplayBuffer(); +} + +// Clear the display buffer. Requires a display() call at some point afterwards +void Adafruit_SSD1306::clearDisplay(void) +{ + std::fill(buffer.begin(),buffer.end(),0); +} + +void Adafruit_SSD1306::splash(void) +{ + + #ifndef NO_SPLASH_ADAFRUIT + uint8_t logoLibreSolar[64 * 128 / 8] = + { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, + 0xC0, 0xE0, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x3F, + 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x08, 0x08, 0x08, 0x08, 0x08, 0xE7, 0xC7, + 0xC3, 0x87, 0x07, 0x07, 0x0F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1E, 0x1F, 0x1F, 0x3F, + 0x3E, 0x7E, 0x7E, 0xFC, 0xFC, 0xF8, 0xF0, 0xC0, 0x01, 0x03, 0x87, 0x87, 0x87, 0x87, 0x87, 0x00, + 0x30, 0x30, 0x30, 0x20, 0x20, 0x60, 0x40, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFC, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, + 0xFE, 0xFC, 0xF8, 0xF0, 0xE3, 0xC7, 0x9F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x06, 0x18, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x03, 0x07, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, + 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x07, 0x03, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60, 0xC0, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xC0, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x80, 0xC0, + 0x60, 0x20, 0x20, 0x20, 0x20, 0x60, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0x40, 0x60, 0x20, + 0x20, 0x60, 0x40, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xE0, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x60, 0x40, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x18, 0x77, + 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x10, 0x10, 0x10, 0x10, 0x30, 0xD0, 0x08, 0x04, 0x03, 0x00, + 0x00, 0x00, 0x00, 0xFF, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F, + 0x08, 0x18, 0x18, 0x10, 0x10, 0x30, 0xE0, 0x80, 0x00, 0x00, 0x10, 0xFF, 0x81, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xC7, 0xFF, 0x10, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xF8, 0x8F, 0x81, 0x80, 0x87, 0x9C, 0xF0, 0x80, 0x00, 0x00, + 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x10, 0x10, 0x30, 0xF0, 0x88, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, + 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0C, 0x06, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0E, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x02, 0x06, + 0x04, 0x04, 0x0C, 0x04, 0x04, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x06, 0x04, 0x0C, + 0x0C, 0x04, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x00, 0x08, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0F, 0x0C, 0x00, + 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x0C, 0x00, 0x00, 0x00, 0x00 + }; + std::copy( + &logoLibreSolar[0] + , &logoLibreSolar[0] + sizeof(logoLibreSolar) + , buffer.begin() + ); + /* + uint8_t adaFruitLogo[64 * 128 / 8] = + { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, + 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF, + 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, + 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8, + 0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, + 0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01, + 0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, + 0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00, + 0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF, + 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, + 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F, + 0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, + 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, + 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, + 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03, + 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // 128x32^^^ 128x64vvv + 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, + 0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F, + 0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, + 0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E, + 0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC, + 0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06, + 0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8, + 0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, + 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C, + 0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, + 0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, + 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07, + 0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + std::copy( + &adaFruitLogo[0] + , &adaFruitLogo[0] + (_rawHeight == 32 ? sizeof(adaFruitLogo)/2 : sizeof(adaFruitLogo)) + , buffer.begin() + ); + */ +#endif +} diff --git a/lib/Adafruit_GFX/Adafruit_SSD1306.h b/lib/Adafruit_GFX/Adafruit_SSD1306.h new file mode 100644 index 0000000..3d84f9f --- /dev/null +++ b/lib/Adafruit_GFX/Adafruit_SSD1306.h @@ -0,0 +1,220 @@ +/********************************************************************* +This is a library for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + +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 +*********************************************************************/ + +/* + * Modified by Neal Horman 7/14/2012 for use in mbed + */ + +#ifndef _ADAFRUIT_SSD1306_H_ +#define _ADAFRUIT_SSD1306_H_ + +#include "mbed.h" +#include "Adafruit_GFX.h" + +#include +#include + +// A DigitalOut sub-class that provides a constructed default state +class DigitalOut2 : public DigitalOut +{ +public: + DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); }; + DigitalOut2& operator= (int value) { write(value); return *this; }; + DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; }; + operator int() { return read(); }; +}; + +#define SSD1306_EXTERNALVCC 0x1 +#define SSD1306_SWITCHCAPVCC 0x2 + +/** The pure base class for the SSD1306 display driver. + * + * You should derive from this for a new transport interface type, + * such as the SPI and I2C drivers. + */ +class Adafruit_SSD1306 : public Adafruit_GFX +{ +public: + Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128) + : Adafruit_GFX(rawWidth,rawHeight) + , rst(RST,false) + { + buffer.resize(rawHeight * rawWidth / 8); + }; + + void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); + + // These must be implemented in the derived transport driver + virtual void command(uint8_t c) = 0; + virtual void data(uint8_t c) = 0; + virtual void drawPixel(int16_t x, int16_t y, uint16_t color); + + /// Clear the display buffer + void clearDisplay(void); + virtual void invertDisplay(bool i); + + /// Cause the display to be updated with the buffer content. + void display(); + /// Fill the buffer with the AdaFruit splash screen. + virtual void splash(); + +protected: + virtual void sendDisplayBuffer() = 0; + DigitalOut2 rst; + + // the memory buffer for the LCD + std::vector buffer; +}; + + +/** This is the SPI SSD1306 display driver transport class + * + */ +class Adafruit_SSD1306_Spi : public Adafruit_SSD1306 +{ +public: + /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions + * + * Required parameters + * @param spi - a reference to an initialized SPI object + * @param DC (Data/Command) pin name + * @param RST (Reset) pin name + * @param CS (Chip Select) pin name + * + * Optional parameters + * @param rawHeight - the vertical number of pixels for the display, defaults to 32 + * @param rawWidth - the horizonal number of pixels for the display, defaults to 128 + */ + Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128) + : Adafruit_SSD1306(RST, rawHieght, rawWidth) + , cs(CS,true) + , dc(DC,false) + , mspi(spi) + { + begin(); + splash(); + display(); + }; + + virtual void command(uint8_t c) + { + cs = 1; + dc = 0; + cs = 0; + mspi.write(c); + cs = 1; + }; + + virtual void data(uint8_t c) + { + cs = 1; + dc = 1; + cs = 0; + mspi.write(c); + cs = 1; + }; + +protected: + virtual void sendDisplayBuffer() + { + cs = 1; + dc = 1; + cs = 0; + + for(uint16_t i=0, q=buffer.size(); i http://www.adafruit.com/category/63_98 + +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 +*********************************************************************/ + +/* + * Modified by Neal Horman 7/14/2012 for use in LPC1768 + */ + +#ifndef FONT5X7_H +#define FONT5X7_H + +// standard ascii 5x7 font + +static unsigned char font[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x3E, 0x5B, 0x4F, 0x5B, 0x3E, + 0x3E, 0x6B, 0x4F, 0x6B, 0x3E, + 0x1C, 0x3E, 0x7C, 0x3E, 0x1C, + 0x18, 0x3C, 0x7E, 0x3C, 0x18, + 0x1C, 0x57, 0x7D, 0x57, 0x1C, + 0x1C, 0x5E, 0x7F, 0x5E, 0x1C, + 0x00, 0x18, 0x3C, 0x18, 0x00, + 0xFF, 0xE7, 0xC3, 0xE7, 0xFF, + 0x00, 0x18, 0x24, 0x18, 0x00, + 0xFF, 0xE7, 0xDB, 0xE7, 0xFF, + 0x30, 0x48, 0x3A, 0x06, 0x0E, + 0x26, 0x29, 0x79, 0x29, 0x26, + 0x40, 0x7F, 0x05, 0x05, 0x07, + 0x40, 0x7F, 0x05, 0x25, 0x3F, + 0x5A, 0x3C, 0xE7, 0x3C, 0x5A, + 0x7F, 0x3E, 0x1C, 0x1C, 0x08, + 0x08, 0x1C, 0x1C, 0x3E, 0x7F, + 0x14, 0x22, 0x7F, 0x22, 0x14, + 0x5F, 0x5F, 0x00, 0x5F, 0x5F, + 0x06, 0x09, 0x7F, 0x01, 0x7F, + 0x00, 0x66, 0x89, 0x95, 0x6A, + 0x60, 0x60, 0x60, 0x60, 0x60, + 0x94, 0xA2, 0xFF, 0xA2, 0x94, + 0x08, 0x04, 0x7E, 0x04, 0x08, + 0x10, 0x20, 0x7E, 0x20, 0x10, + 0x08, 0x08, 0x2A, 0x1C, 0x08, + 0x08, 0x1C, 0x2A, 0x08, 0x08, + 0x1E, 0x10, 0x10, 0x10, 0x10, + 0x0C, 0x1E, 0x0C, 0x1E, 0x0C, + 0x30, 0x38, 0x3E, 0x38, 0x30, + 0x06, 0x0E, 0x3E, 0x0E, 0x06, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x5F, 0x00, 0x00, + 0x00, 0x07, 0x00, 0x07, 0x00, + 0x14, 0x7F, 0x14, 0x7F, 0x14, + 0x24, 0x2A, 0x7F, 0x2A, 0x12, + 0x23, 0x13, 0x08, 0x64, 0x62, + 0x36, 0x49, 0x56, 0x20, 0x50, + 0x00, 0x08, 0x07, 0x03, 0x00, + 0x00, 0x1C, 0x22, 0x41, 0x00, + 0x00, 0x41, 0x22, 0x1C, 0x00, + 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, + 0x08, 0x08, 0x3E, 0x08, 0x08, + 0x00, 0x80, 0x70, 0x30, 0x00, + 0x08, 0x08, 0x08, 0x08, 0x08, + 0x00, 0x00, 0x60, 0x60, 0x00, + 0x20, 0x10, 0x08, 0x04, 0x02, + 0x3E, 0x51, 0x49, 0x45, 0x3E, + 0x00, 0x42, 0x7F, 0x40, 0x00, + 0x72, 0x49, 0x49, 0x49, 0x46, + 0x21, 0x41, 0x49, 0x4D, 0x33, + 0x18, 0x14, 0x12, 0x7F, 0x10, + 0x27, 0x45, 0x45, 0x45, 0x39, + 0x3C, 0x4A, 0x49, 0x49, 0x31, + 0x41, 0x21, 0x11, 0x09, 0x07, + 0x36, 0x49, 0x49, 0x49, 0x36, + 0x46, 0x49, 0x49, 0x29, 0x1E, + 0x00, 0x00, 0x14, 0x00, 0x00, + 0x00, 0x40, 0x34, 0x00, 0x00, + 0x00, 0x08, 0x14, 0x22, 0x41, + 0x14, 0x14, 0x14, 0x14, 0x14, + 0x00, 0x41, 0x22, 0x14, 0x08, + 0x02, 0x01, 0x59, 0x09, 0x06, + 0x3E, 0x41, 0x5D, 0x59, 0x4E, + 0x7C, 0x12, 0x11, 0x12, 0x7C, + 0x7F, 0x49, 0x49, 0x49, 0x36, + 0x3E, 0x41, 0x41, 0x41, 0x22, + 0x7F, 0x41, 0x41, 0x41, 0x3E, + 0x7F, 0x49, 0x49, 0x49, 0x41, + 0x7F, 0x09, 0x09, 0x09, 0x01, + 0x3E, 0x41, 0x41, 0x51, 0x73, + 0x7F, 0x08, 0x08, 0x08, 0x7F, + 0x00, 0x41, 0x7F, 0x41, 0x00, + 0x20, 0x40, 0x41, 0x3F, 0x01, + 0x7F, 0x08, 0x14, 0x22, 0x41, + 0x7F, 0x40, 0x40, 0x40, 0x40, + 0x7F, 0x02, 0x1C, 0x02, 0x7F, + 0x7F, 0x04, 0x08, 0x10, 0x7F, + 0x3E, 0x41, 0x41, 0x41, 0x3E, + 0x7F, 0x09, 0x09, 0x09, 0x06, + 0x3E, 0x41, 0x51, 0x21, 0x5E, + 0x7F, 0x09, 0x19, 0x29, 0x46, + 0x26, 0x49, 0x49, 0x49, 0x32, + 0x03, 0x01, 0x7F, 0x01, 0x03, + 0x3F, 0x40, 0x40, 0x40, 0x3F, + 0x1F, 0x20, 0x40, 0x20, 0x1F, + 0x3F, 0x40, 0x38, 0x40, 0x3F, + 0x63, 0x14, 0x08, 0x14, 0x63, + 0x03, 0x04, 0x78, 0x04, 0x03, + 0x61, 0x59, 0x49, 0x4D, 0x43, + 0x00, 0x7F, 0x41, 0x41, 0x41, + 0x02, 0x04, 0x08, 0x10, 0x20, + 0x00, 0x41, 0x41, 0x41, 0x7F, + 0x04, 0x02, 0x01, 0x02, 0x04, + 0x40, 0x40, 0x40, 0x40, 0x40, + 0x00, 0x03, 0x07, 0x08, 0x00, + 0x20, 0x54, 0x54, 0x78, 0x40, + 0x7F, 0x28, 0x44, 0x44, 0x38, + 0x38, 0x44, 0x44, 0x44, 0x28, + 0x38, 0x44, 0x44, 0x28, 0x7F, + 0x38, 0x54, 0x54, 0x54, 0x18, + 0x00, 0x08, 0x7E, 0x09, 0x02, + 0x18, 0xA4, 0xA4, 0x9C, 0x78, + 0x7F, 0x08, 0x04, 0x04, 0x78, + 0x00, 0x44, 0x7D, 0x40, 0x00, + 0x20, 0x40, 0x40, 0x3D, 0x00, + 0x7F, 0x10, 0x28, 0x44, 0x00, + 0x00, 0x41, 0x7F, 0x40, 0x00, + 0x7C, 0x04, 0x78, 0x04, 0x78, + 0x7C, 0x08, 0x04, 0x04, 0x78, + 0x38, 0x44, 0x44, 0x44, 0x38, + 0xFC, 0x18, 0x24, 0x24, 0x18, + 0x18, 0x24, 0x24, 0x18, 0xFC, + 0x7C, 0x08, 0x04, 0x04, 0x08, + 0x48, 0x54, 0x54, 0x54, 0x24, + 0x04, 0x04, 0x3F, 0x44, 0x24, + 0x3C, 0x40, 0x40, 0x20, 0x7C, + 0x1C, 0x20, 0x40, 0x20, 0x1C, + 0x3C, 0x40, 0x30, 0x40, 0x3C, + 0x44, 0x28, 0x10, 0x28, 0x44, + 0x4C, 0x90, 0x90, 0x90, 0x7C, + 0x44, 0x64, 0x54, 0x4C, 0x44, + 0x00, 0x08, 0x36, 0x41, 0x00, + 0x00, 0x00, 0x77, 0x00, 0x00, + 0x00, 0x41, 0x36, 0x08, 0x00, + 0x02, 0x01, 0x02, 0x04, 0x02, + 0x3C, 0x26, 0x23, 0x26, 0x3C, + 0x1E, 0xA1, 0xA1, 0x61, 0x12, + 0x3A, 0x40, 0x40, 0x20, 0x7A, + 0x38, 0x54, 0x54, 0x55, 0x59, + 0x21, 0x55, 0x55, 0x79, 0x41, + 0x21, 0x54, 0x54, 0x78, 0x41, + 0x21, 0x55, 0x54, 0x78, 0x40, + 0x20, 0x54, 0x55, 0x79, 0x40, + 0x0C, 0x1E, 0x52, 0x72, 0x12, + 0x39, 0x55, 0x55, 0x55, 0x59, + 0x39, 0x54, 0x54, 0x54, 0x59, + 0x39, 0x55, 0x54, 0x54, 0x58, + 0x00, 0x00, 0x45, 0x7C, 0x41, + 0x00, 0x02, 0x45, 0x7D, 0x42, + 0x00, 0x01, 0x45, 0x7C, 0x40, + 0xF0, 0x29, 0x24, 0x29, 0xF0, + 0xF0, 0x28, 0x25, 0x28, 0xF0, + 0x7C, 0x54, 0x55, 0x45, 0x00, + 0x20, 0x54, 0x54, 0x7C, 0x54, + 0x7C, 0x0A, 0x09, 0x7F, 0x49, + 0x32, 0x49, 0x49, 0x49, 0x32, + 0x32, 0x48, 0x48, 0x48, 0x32, + 0x32, 0x4A, 0x48, 0x48, 0x30, + 0x3A, 0x41, 0x41, 0x21, 0x7A, + 0x3A, 0x42, 0x40, 0x20, 0x78, + 0x00, 0x9D, 0xA0, 0xA0, 0x7D, + 0x39, 0x44, 0x44, 0x44, 0x39, + 0x3D, 0x40, 0x40, 0x40, 0x3D, + 0x3C, 0x24, 0xFF, 0x24, 0x24, + 0x48, 0x7E, 0x49, 0x43, 0x66, + 0x2B, 0x2F, 0xFC, 0x2F, 0x2B, + 0xFF, 0x09, 0x29, 0xF6, 0x20, + 0xC0, 0x88, 0x7E, 0x09, 0x03, + 0x20, 0x54, 0x54, 0x79, 0x41, + 0x00, 0x00, 0x44, 0x7D, 0x41, + 0x30, 0x48, 0x48, 0x4A, 0x32, + 0x38, 0x40, 0x40, 0x22, 0x7A, + 0x00, 0x7A, 0x0A, 0x0A, 0x72, + 0x7D, 0x0D, 0x19, 0x31, 0x7D, + 0x26, 0x29, 0x29, 0x2F, 0x28, + 0x26, 0x29, 0x29, 0x29, 0x26, + 0x30, 0x48, 0x4D, 0x40, 0x20, + 0x38, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x38, + 0x2F, 0x10, 0xC8, 0xAC, 0xBA, + 0x2F, 0x10, 0x28, 0x34, 0xFA, + 0x00, 0x00, 0x7B, 0x00, 0x00, + 0x08, 0x14, 0x2A, 0x14, 0x22, + 0x22, 0x14, 0x2A, 0x14, 0x08, + 0xAA, 0x00, 0x55, 0x00, 0xAA, + 0xAA, 0x55, 0xAA, 0x55, 0xAA, + 0x00, 0x00, 0x00, 0xFF, 0x00, + 0x10, 0x10, 0x10, 0xFF, 0x00, + 0x14, 0x14, 0x14, 0xFF, 0x00, + 0x10, 0x10, 0xFF, 0x00, 0xFF, + 0x10, 0x10, 0xF0, 0x10, 0xF0, + 0x14, 0x14, 0x14, 0xFC, 0x00, + 0x14, 0x14, 0xF7, 0x00, 0xFF, + 0x00, 0x00, 0xFF, 0x00, 0xFF, + 0x14, 0x14, 0xF4, 0x04, 0xFC, + 0x14, 0x14, 0x17, 0x10, 0x1F, + 0x10, 0x10, 0x1F, 0x10, 0x1F, + 0x14, 0x14, 0x14, 0x1F, 0x00, + 0x10, 0x10, 0x10, 0xF0, 0x00, + 0x00, 0x00, 0x00, 0x1F, 0x10, + 0x10, 0x10, 0x10, 0x1F, 0x10, + 0x10, 0x10, 0x10, 0xF0, 0x10, + 0x00, 0x00, 0x00, 0xFF, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0xFF, 0x10, + 0x00, 0x00, 0x00, 0xFF, 0x14, + 0x00, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x00, 0x1F, 0x10, 0x17, + 0x00, 0x00, 0xFC, 0x04, 0xF4, + 0x14, 0x14, 0x17, 0x10, 0x17, + 0x14, 0x14, 0xF4, 0x04, 0xF4, + 0x00, 0x00, 0xFF, 0x00, 0xF7, + 0x14, 0x14, 0x14, 0x14, 0x14, + 0x14, 0x14, 0xF7, 0x00, 0xF7, + 0x14, 0x14, 0x14, 0x17, 0x14, + 0x10, 0x10, 0x1F, 0x10, 0x1F, + 0x14, 0x14, 0x14, 0xF4, 0x14, + 0x10, 0x10, 0xF0, 0x10, 0xF0, + 0x00, 0x00, 0x1F, 0x10, 0x1F, + 0x00, 0x00, 0x00, 0x1F, 0x14, + 0x00, 0x00, 0x00, 0xFC, 0x14, + 0x00, 0x00, 0xF0, 0x10, 0xF0, + 0x10, 0x10, 0xFF, 0x10, 0xFF, + 0x14, 0x14, 0x14, 0xFF, 0x14, + 0x10, 0x10, 0x10, 0x1F, 0x00, + 0x00, 0x00, 0x00, 0xF0, 0x10, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xFF, 0xFF, 0xFF, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xFF, 0xFF, + 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, + 0x38, 0x44, 0x44, 0x38, 0x44, + 0x7C, 0x2A, 0x2A, 0x3E, 0x14, + 0x7E, 0x02, 0x02, 0x06, 0x06, + 0x02, 0x7E, 0x02, 0x7E, 0x02, + 0x63, 0x55, 0x49, 0x41, 0x63, + 0x38, 0x44, 0x44, 0x3C, 0x04, + 0x40, 0x7E, 0x20, 0x1E, 0x20, + 0x06, 0x02, 0x7E, 0x02, 0x02, + 0x99, 0xA5, 0xE7, 0xA5, 0x99, + 0x1C, 0x2A, 0x49, 0x2A, 0x1C, + 0x4C, 0x72, 0x01, 0x72, 0x4C, + 0x30, 0x4A, 0x4D, 0x4D, 0x30, + 0x30, 0x48, 0x78, 0x48, 0x30, + 0xBC, 0x62, 0x5A, 0x46, 0x3D, + 0x3E, 0x49, 0x49, 0x49, 0x00, + 0x7E, 0x01, 0x01, 0x01, 0x7E, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x44, 0x44, 0x5F, 0x44, 0x44, + 0x40, 0x51, 0x4A, 0x44, 0x40, + 0x40, 0x44, 0x4A, 0x51, 0x40, + 0x00, 0x00, 0xFF, 0x01, 0x03, + 0xE0, 0x80, 0xFF, 0x00, 0x00, + 0x08, 0x08, 0x6B, 0x6B, 0x08, + 0x36, 0x12, 0x36, 0x24, 0x36, + 0x06, 0x0F, 0x09, 0x0F, 0x06, + 0x00, 0x00, 0x18, 0x18, 0x00, + 0x00, 0x00, 0x10, 0x10, 0x00, + 0x30, 0x40, 0xFF, 0x01, 0x01, + 0x00, 0x1F, 0x01, 0x01, 0x1E, + 0x00, 0x19, 0x1D, 0x17, 0x12, + 0x00, 0x3C, 0x3C, 0x3C, 0x3C, + 0x00, 0x00, 0x00, 0x00, 0x00, +}; +#endif diff --git a/lib/BuckConverter/BuckConverter.cpp b/lib/BuckConverter/BuckConverter.cpp new file mode 100644 index 0000000..9cbfe0e --- /dev/null +++ b/lib/BuckConverter/BuckConverter.cpp @@ -0,0 +1,217 @@ +/* mbed library for MPPT buck converter + * Copyright (c) 2016 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BuckConverter.h" + +BuckConverter::BuckConverter(int freq_kHz) +{ + init_registers(); + frequency_kHz(freq_kHz); + last_time_CV_reset(); + + _pwm_delta = 1; +} + +void BuckConverter::update(int input_voltage_mV, int output_voltage_mV, int output_current_mA) { + + int output_power_mW = output_voltage_mV * output_current_mA / 1000; + + //printf("P=%d P_prev=%d Vi=%d Vo=%d Io=%d\n", output_power_mW, _output_power_prev_mW, input_voltage_mV, output_voltage_mV, output_current_mA); + + if (output_voltage_mV > _max_voltage_mV) { + // increase input voltage to lower output voltage + duty_cycle_step(-1); + _time_voltage_limit_reached = time(NULL); + //printf("output_voltage_mV = %d, _max_voltage_mV = %d\n", output_voltage_mV, _max_voltage_mV); + fflush(stdout); + } + else if (output_current_mA > _max_current_mA) { + // increase input voltage to decrease current + duty_cycle_step(-1); + } + else { + // start MPPT + if (_output_power_prev_mW > output_power_mW) { + _pwm_delta = -_pwm_delta; + } + duty_cycle_step(_pwm_delta); + } + + _output_power_prev_mW = output_power_mW; +} + +void BuckConverter::init_registers() { + + // Enable peripheral clock of GPIOA and GPIOB + RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; + + // Enable TIM1 clock + RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; + + // Select alternate function mode on PA8 und PB13 + GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) | GPIO_MODER_MODER8_1; + GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER13)) | GPIO_MODER_MODER13_1; + + // Select AF2 on PA8 (TIM1_CH1) + GPIOA->AFR[1] |= 0x2 << ((8 - 8)*4); // AFR[1] for pins 8-15 + + // Select AF2 on PB13 (TIM1_CH1N) + GPIOB->AFR[1] |= 0x2 << ((13 - 8)*4); // AFR[1] for pins 8-15 + + // No prescaler --> timer frequency = 48 MHz + TIM1->PSC = 0; + + // Capture/Compare Mode Register 1 + // OC1M = 110: Select PWM mode 1 on OC1 + // OC1PE = 1: Enable preload register on OC1 (reset value) + TIM1->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE; + + // Capture/Compare Enable Register + // CC1E = 1: Enable the output on OC1 + // CC1P = 0: Active high polarity on OC1 (default) + // CC1NE = 1: Enable the output on OC1N + // CC1NP = 0: Active high polarity on OC1N (default) + TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE;// | TIM_CCER_CC1NP; + + // Control Register 1 + // TIM_CR1_CMS = 01: Select center-aligned mode 1 + // TIM_CR1_CEN = 1: Counter enable + TIM1->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN; + + // Control Register 2 + // OIS1 = OIS1N = 0: Output Idle State is set to off + //TIM1->CR2 |= ; + + // Force update generation (UG = 1) + TIM1->EGR |= TIM_EGR_UG; +} + +void BuckConverter::set_voltage_limit(int voltage_mV) { + _max_voltage_mV = voltage_mV; +} + +void BuckConverter::set_current_limit(int current_mA) { + _max_current_mA = current_mA; +} + +time_t BuckConverter::last_time_CV() { + return _time_voltage_limit_reached; +} + +void BuckConverter::last_time_CV_reset() { + _time_voltage_limit_reached = 0x7FFFFFFF; // maximum value +} + +void BuckConverter::frequency_kHz(int freq) { + + _pwm_resolution = SystemCoreClock / (freq * 1000); + + // Auto Reload Register + // center-aligned mode --> divide resolution by 2 + TIM1->ARR = _pwm_resolution / 2; +} + + +void BuckConverter::set_duty_cycle(float duty) { + + float duty_target; + // protection against wrong settings which could destroy the hardware + if (duty < _min_duty) { + duty_target = _min_duty; + } + else if (duty > _max_duty) { + duty_target = _max_duty; + } + else { + duty_target = duty; + } + + TIM1->CCR1 = _pwm_resolution / 2 * duty_target; +} + +void BuckConverter::duty_cycle_step(int delta) +{ + float duty_target; + duty_target = (float)(TIM1->CCR1 + delta) / (_pwm_resolution / 2); + + // protection against wrong settings which could destroy the hardware + if (duty_target < _min_duty) { + set_duty_cycle(_min_duty); + } + else if (duty_target > _max_duty) { + set_duty_cycle(_max_duty); + } + else { + TIM1->CCR1 = TIM1->CCR1 + delta; + } +} + + +float BuckConverter::get_duty_cycle() { + return (float)(TIM1->CCR1) / (_pwm_resolution / 2);; +} + + +void BuckConverter::deadtime_ns(int deadtime) { + + uint8_t deadtime_clocks = (SystemCoreClock / 1000 / 1000) * deadtime / 1000; + + // Break and Dead-Time Register + // MOE = 1: Main output enable + // OSSR = 0: Off-state selection for Run mode -> OC/OCN = 0 + // OSSI = 0: Off-state selection for Idle mode -> OC/OCN = 0 + TIM1->BDTR |= (deadtime_clocks & (uint32_t)0x7F); // ensure that only the last 7 bits are changed +} + + +void BuckConverter::start(float pwm_duty) { + set_duty_cycle(pwm_duty); + + // Break and Dead-Time Register + // MOE = 1: Main output enable + TIM1->BDTR |= TIM_BDTR_MOE; +} + + +void BuckConverter::stop() { + + // Break and Dead-Time Register + // MOE = 1: Main output enable + TIM1->BDTR &= ~(TIM_BDTR_MOE); +} + + +void BuckConverter::lock_settings() { + + // TODO: does not work properly... maybe HW bug? + + // Break and Dead-Time Register + TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0; +} + +void BuckConverter::duty_cycle_limits(float min_duty, float max_duty) { + + _min_duty = min_duty; + _max_duty = max_duty; + + // adjust set value to new limits + if (get_duty_cycle() < _min_duty) { + set_duty_cycle(_min_duty); + } + else if (get_duty_cycle() > _max_duty) { + set_duty_cycle(_max_duty); + } +} diff --git a/lib/BuckConverter/BuckConverter.h b/lib/BuckConverter/BuckConverter.h new file mode 100644 index 0000000..59d98e3 --- /dev/null +++ b/lib/BuckConverter/BuckConverter.h @@ -0,0 +1,108 @@ +/* mbed library for MPPT buck converter + * Copyright (c) 2016 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" + +class BuckConverter +{ +public: + + /** Create a half bridge object + */ + BuckConverter(int freq_kHz); + + void set_voltage_limit(int voltage_mV); + void set_current_limit(int current_mA); + time_t last_time_CV (); + void last_time_CV_reset(); + + /** Set the PWM frequency in kHz + * + * @param freq Frequency in kHz + */ + void frequency_kHz(int freq); + + /** Set the deadtime between switching the two FETs on/off + * + * @param deadtime Deadtime in ns + */ + void deadtime_ns(int deadtime); + + /** Lock the settings of PWM generation to prevent accidental changes + * (does not work properly, yet! --> TODO) + */ + void lock_settings(void); + + /** Start the PWM generation + * + * @param pwm_duty Duty cycle between 0.0 and 1.0 + */ + void start(float pwm_duty); + + /** Stop the PWM generation + */ + void stop(); + + /** Updates duty cycle to match voltage levels based on measured values + * + * @param input_voltage_mV Measured input voltage (mV) + * @param output_voltage_mV Measured output voltage (mA) + * @param output_current_mA Measured output current (mA) + */ + void update(int input_voltage_mV, int output_voltage_mV, int output_current_mA); + + /** Set the duty cycle of the PWM signal between 0.0 and 1.0 + * + * @param duty Duty cycle between 0.0 and 1.0 + */ + void set_duty_cycle(float duty); + + /** Adjust the duty cycle with minimum step size + * + * @param delta Number of steps (positive or negative) + */ + void duty_cycle_step(int delta); + + /** Read the currently set duty cycle + * + * @returns + * Duty cycle between 0.0 and 1.0 + */ + float get_duty_cycle(); + + /** Set limits for the duty cycle to prevent hardware damages + * + * @param min_duty Minimum duty cycle (e.g. 0.5 for limiting input voltage) + * @param max_duty Maximum duty cycle (e.g. 0.97 for charge pump) + */ + void duty_cycle_limits(float min_duty, float max_duty); + +private: + + /** Initiatializes the registers to generate the PWM signal + */ + void init_registers(); + + int _pwm_resolution; + float _min_duty; + float _max_duty; + int _pwm_delta; + + int _max_voltage_mV; + int _max_current_mA; + int _output_power_prev_mW; + time_t _time_voltage_limit_reached; +}; diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..c886b2d --- /dev/null +++ b/src/config.h @@ -0,0 +1,104 @@ +/* LibreSolar MPPT charge controller firmware + * Copyright (c) 2016-2017 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "mbed.h" + +#define MPPT_20A + +// DC/DC converter settings +const int _pwm_frequency = 70; // kHz + +const int charge_current_cutoff = 20; // mA --> if lower, charger is switched off +const int max_solar_voltage = 55000; // mV + +const int num_cells = 6; + +// State: Standby +const int solar_voltage_offset_start = 5000; // mV charging switched on if Vsolar > Vbat + offset +const int solar_voltage_offset_stop = 1000; // mV charging switched off if Vsolar < Vbat + offset +const int time_limit_recharge = 60;//5*60; // sec +const int cell_voltage_recharge = 2300; // mV + +// State: CC/bulk +const int charge_current_max = 10000; // mA PCB maximum: 20 A + +// State: CV/absorption +const int cell_voltage_max = 2400; // max voltage per cell +const int time_limit_CV = 120; // min +const int current_cutoff_CV = 2000; // mA + +// State: float/trickle +const bool trickle_enabled = true; +const int cell_voltage_trickle = 2300; // voltage for trickle charging of lead-acid batteries +const int time_trickle_recharge = 30; // min + +// State: equalization +const bool equalization_enabled = false; +const int cell_voltage_equalization = 2500; // mV +const int time_limit_equalization = 60; // min +const int current_limit_equalization = 1000; // mA +const int equalization_trigger_time = 8; // weeks +const int equalization_trigger_deep_cycles = 10; // times + +const int cell_voltage_load_disconnect = 1950; +const int cell_voltage_load_reconnect = 2200; + +/* +// typcial LiFePO4 battery +const int num_cells = 4; +const int v_cell_max = 3550; // max voltage per cell +const int v_cell_trickle = 3550; // voltage for trickle charging of lead-acid batteries +const int v_cell_load_disconnect = 3000; +const int v_cell_load_reconnect = 3150; +*/ +const int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 thermistor: 3435 + +//---------------------------------------------------------------------------- +// global variables + +// 20A board (rev. 0.6) +#define PIN_UEXT_TX PA_2 +#define PIN_UEXT_RX PA_3 +#define PIN_UEXT_SCL PB_6 +#define PIN_UEXT_SDA PB_7 +#define PIN_UEXT_MISO PB_4 +#define PIN_UEXT_MOSI PB_5 +#define PIN_UEXT_SCK PB_3 +#define PIN_UEXT_SSEL PA_0 + +#define PIN_SWD_TX PA_9 +#define PIN_SWD_RX PA_10 + +#define PIN_PWM_HS PA_8 +#define PIN_PWM_LS PB_13 + +#define PIN_LED_GREEN PB_14 +#define PIN_LED_RED PB_15 +#define PIN_LOAD_DIS PB_2 + +#define PIN_REF_I_DCDC PA_4 + +#define PIN_V_REF PA_5 +#define PIN_V_BAT PA_6 +#define PIN_V_SOLAR PA_7 +#define PIN_V_TEMP PA_1 +#define PIN_I_LOAD PB_0 +#define PIN_I_DCDC PB_1 + +#endif diff --git a/src/config_12a.h b/src/config_12a.h new file mode 100644 index 0000000..41193f3 --- /dev/null +++ b/src/config_12a.h @@ -0,0 +1,104 @@ +/* LibreSolar MPPT charge controller firmware + * Copyright (c) 2016-2017 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONFIG_12A_H +#define CONFIG_12A_H + +#include "mbed.h" + +#define MPPT_12A + +// DC/DC converter settings +const int _pwm_frequency = 100; // kHz + +const int charge_current_cutoff = 20; // mA --> if lower, charger is switched off +const int max_solar_voltage = 55000; // mV + +const int num_cells = 6; + +// State: Standby +const int solar_voltage_offset_start = 5000; // mV charging switched on if Vsolar > Vbat + offset +const int solar_voltage_offset_stop = 1000; // mV charging switched off if Vsolar < Vbat + offset +const int time_limit_recharge = 60; // sec +const int cell_voltage_recharge = 2300; // mV + +// State: CC/bulk +const int charge_current_max = 10000; // mA PCB maximum: 20 A + +// State: CV/absorption +const int cell_voltage_max = 2400; // max voltage per cell +const int time_limit_CV = 120; // min +const int current_cutoff_CV = 1000; //2000; // mA + +// State: float/trickle +const bool trickle_enabled = true; +const int cell_voltage_trickle = 2300; // voltage for trickle charging of lead-acid batteries +const int time_trickle_recharge = 30; // min + +// State: equalization +const bool equalization_enabled = false; +const int cell_voltage_equalization = 2500; // mV +const int time_limit_equalization = 60; // min +const int current_limit_equalization = 1000; // mA +const int equalization_trigger_time = 8; // weeks +const int equalization_trigger_deep_cycles = 10; // times + +const int cell_voltage_load_disconnect = 1950; +const int cell_voltage_load_reconnect = 2200; + +/* +// typcial LiFePO4 battery +const int num_cells = 4; +const int v_cell_max = 3550; // max voltage per cell +const int v_cell_trickle = 3550; // voltage for trickle charging of lead-acid batteries +const int v_cell_load_disconnect = 3000; +const int v_cell_load_reconnect = 3150; +*/ +const int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 thermistor: 3435 + +//---------------------------------------------------------------------------- +// global variables + +// 12A board (rev. 0.5) +#define PIN_UEXT_TX PA_2 +#define PIN_UEXT_RX PA_3 +#define PIN_UEXT_SCL PB_6 +#define PIN_UEXT_SDA PB_7 +#define PIN_UEXT_MISO PB_4 +#define PIN_UEXT_MOSI PB_5 +#define PIN_UEXT_SCK PB_3 +#define PIN_UEXT_SSEL PA_1 + +#define PIN_SWD_TX PB_10 +#define PIN_SWD_RX PB_11 + +#define PIN_PWM_HS PA_8 +#define PIN_PWM_LS PB_13 + +#define PIN_LED_GREEN PA_9 +#define PIN_LED_RED PB_12 +#define PIN_LOAD_DIS PB_2 + +#define PIN_REF_I_DCDC PB_0 + +#define PIN_V_REF PA_0 // not connected +#define PIN_V_BAT PA_4 +#define PIN_V_SOLAR PA_5 +#define PIN_V_TEMP PA_0 // not connected +#define PIN_I_LOAD PA_6 +#define PIN_I_DCDC PA_7 + +#endif diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..153bcfb --- /dev/null +++ b/src/display.h @@ -0,0 +1,20 @@ + + +static unsigned char bmp_load [] = { + 0x20, 0x22, 0x04, 0x70, 0x88, 0x8B, 0x88, 0x70, 0x04, 0x22, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +const unsigned char bmp_arrow_right [] = { + 0x41, 0x63, 0x36, 0x1C +}; + +const unsigned char bmp_pv_panel [] = { + 0x60, 0x98, 0x86, 0xC9, 0x31, 0x19, 0x96, 0x62, 0x32, 0x2C, 0xC4, 0x64, 0x98, 0x08, 0xC8, 0x30, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x04, 0x03, 0x00, 0x00 +}; + +const unsigned char bmp_disconnected [] = { + 0x08, 0x08, 0x08, 0x08, 0x00, 0x41, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x41, 0x00, 0x08, 0x08, + 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; diff --git a/src/main.cpp b/src/main.cpp index 90e215d..7219199 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ /* LibreSolar MPPT charge controller firmware - * Copyright (c) 2016 Martin Jäger (www.libre.solar) + * Copyright (c) 2016-2017 Martin Jäger (www.libre.solar) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,85 +15,74 @@ */ #include "mbed.h" -#include "HalfBridge.h" -#include "PID.h" -#include "DogLCD.h" -#include "font_6x8.h" -#include "N3310LCD.h" +//#include "config_12a.h" // choose old PCB revision (12A) +#include "config.h" // choose new PCB revision (20A) +#include "BuckConverter.h" +//#include "DogLCD.h" +//#include "font_6x8.h" +#include "Adafruit_SSD1306.h" +//#include "SDFileSystem.h" +#include // log for thermistor calculation +#include "display.h" + +//#define DOGLCD_ENABLED +//#define SDCARD_ENABLED +#define OLED_ENABLED -#define NOKIALCD_ENABLED // select DOGLCD or NOKIALCD -//#define DOGLCD_ENABLED // select DOGLCD or NOKIALCD #define ADC_AVG_SAMPLES 8 // number of ADC values to read for averaging -#define MPPT_SAFE_MODE 1 - -// DC/DC converter settings -const int _pwm_frequency = 100; // kHz -const int solar_voltage_offset_start = 5000; // mV charging switched on if Vsolar > Vbat + offset -const int solar_voltage_offset_stop = 1000; // mV charging switched on if Vsolar > Vbat + offset -const int max_charge_current = 10000; // mA PCB maximum: 12 A -const int charge_current_cutoff = 20; // mA --> if lower, charger is switched off -const int max_cv_chg_voltage_offset = 100; // mV max voltage offset vs. vmax to switch back into CC mode -const int max_solar_voltage = 55000; // mV - -// typcial lead-acid battery -const int num_cells = 6; -const int v_cell_max = 2400; // max voltage per cell -const int v_cell_trickle = 2300; // voltage for trickle charging of lead-acid batteries -const int v_cell_recharge = 2200; // voltage to switch from CV or trickle back to CC charging -const int v_cell_load_disconnect = 1950; -const int v_cell_load_reconnect = 2200; - -/* -// typcial LiFePO4 battery -const int num_cells = 4; -const int v_cell_max = 3550; // max voltage per cell -const int v_cell_trickle = 3550; // voltage for trickle charging of lead-acid batteries -const int v_cell_load_disconnect = 3000; -const int v_cell_load_reconnect = 3150; -*/ //---------------------------------------------------------------------------- // global variables - #ifdef DOGLCD_ENABLED -SPI spi(PB_5, NC, PB_3); -DogLCD lcd(spi, PA_1, PA_3, PA_2); // spi, cs, a0, reset -#endif -#ifdef NOKIALCD_ENABLED -// MOSI, MISO, SCK, CE, DC, RST, BL -N3310LCD lcd(PB_5, NC, PB_3, PA_1, PB_7, PB_6, NC); +SPI spi(PIN_UEXT_MOSI, NC, PIN_UEXT_SCK); +DogLCD lcd(spi, PIN_UEXT_SSEL, PIN_UEXT_RX, PIN_UEXT_TX); // spi, CS, CD, reset #endif -Serial serial(PB_10, PB_11); +#ifdef SDCARD_ENABLED +SDFileSystem sd(PIN_UEXT_MOSI, PIN_UEXT_MISO, PIN_UEXT_SCK, PIN_UEXT_SSEL, "sd"); +#endif -HalfBridge mppt(PA_8, PB_13); +#ifdef OLED_ENABLED +I2C i2c(PIN_UEXT_SDA, PIN_UEXT_SCL); +Adafruit_SSD1306_I2c oled(i2c, PB_2, 0x78, 64, 128); +#endif -DigitalOut led1(PA_9); -DigitalOut led2(PA_10); -DigitalOut led3(PB_12); -DigitalOut load_disable(PB_2); +Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial"); +Serial serial_uext(PIN_UEXT_TX, PIN_UEXT_RX, "serial_uext"); -AnalogIn v_bat(PA_4); -AnalogIn v_solar(PA_5); -AnalogIn i_load(PA_6); -AnalogIn i_dcdc(PA_7); +// PWM will be generated on pins PA8 (TIM1_CH1) and PB13 (TIM1_CH1N) +BuckConverter mppt(_pwm_frequency); -Ticker tick1, tick2, tick3; +DigitalOut led_green(PIN_LED_GREEN); +DigitalOut led_red(PIN_LED_RED); +DigitalOut load_disable(PIN_LOAD_DIS); + +//AnalogOut ref_i_dcdc(PIN_REF_I_DCDC); + +AnalogIn v_ref(PIN_V_REF); +AnalogIn v_bat(PIN_V_BAT); +AnalogIn v_solar(PIN_V_SOLAR); +AnalogIn v_temp(PIN_V_TEMP); +AnalogIn i_load(PIN_I_LOAD); +AnalogIn i_dcdc(PIN_I_DCDC); int solar_voltage; // mV int battery_voltage; // mV -int dcdc_current; // mA +int dcdc_current = 0; // mA int load_current; // mA - -int dcdc_current_target = 0; -int solar_power_prev = 0; // unit: mW +float temperature; // mA int solar_power = 0; // unit: mW -bool battery_full = false; +int dcdc_current_offset = 0; +int load_current_offset = 0; + +int time_state_changed = 0; // for daily energy counting -long seconds_zero_solar = 0; +int seconds_zero_solar = 0; +int seconds_day = 0; +int day_counter = 0; bool nighttime = false; float input_Wh_day = 0.0; float output_Wh_day = 0.0; @@ -102,78 +91,110 @@ float output_Wh_total = 0.0; int pwm_delta = 1; -enum charger_states {STANDBY, CHG_CC, CHG_CV, CHG_TRICKLE, BAT_ERROR, CHG_BIKE, TEST}; // states for charger state machine +enum charger_states {STANDBY, CHG_CC, CHG_CV, CHG_TRICKLE, CHG_EQUALIZATION, BAT_ERROR, CHG_BIKE, TEST}; // states for charger state machine int state = BAT_ERROR; //---------------------------------------------------------------------------- // function prototypes void setup(); -void update_measurements(bool init = false); +void update_measurements(); void update_controller(); void enter_state(int next_state); void state_machine(); void adjust_input_voltage(int delta_mV); void update_screen(); void energy_counter(); +void load_management(); +void log_data(); +void rtc_write_backup_reg(uint32_t BackupRegister, uint32_t data); +uint32_t rtc_read_backup_reg(uint32_t BackupRegister); +float adc_read_avg(AnalogIn& input); //---------------------------------------------------------------------------- int main() { - // boot from System Memory where USB DFU bootloader is located - //OB->USER |= 1 << 4; + Ticker tick1, tick2; - load_disable = 0; setup(); + wait(3); // safety feature: be able to re-flash before starting - enter_state(STANDBY); - //enter_state(TEST); + time_t last_second = time(NULL); - tick1.attach(&update_screen, 0.5); - - //tick2.attach(&update_controller, PID_UPDATE_RATE); + tick1.attach(&update_measurements, 0.01); tick2.attach(&state_machine, 0.1); - tick3.attach(&energy_counter, 1); + enter_state(STANDBY); + time_state_changed = time(NULL) - 60*60*24; // start immediately + //enter_state(TEST); while(1) { - update_measurements(); - //state_machine(); - - if (battery_voltage < v_cell_load_disconnect * num_cells) { - load_disable = 1; + // called once per second + if (time(NULL) - last_second >= 1) { + last_second = time(NULL); + energy_counter(); + update_screen(); + log_data(); } - if (battery_voltage >= v_cell_load_reconnect * num_cells) { - load_disable = 0; - } - - wait(0.01); + sleep(); // wake-up by ticker interrupts } } //---------------------------------------------------------------------------- void setup() { - #ifdef NOKIALCD_ENABLED - lcd.init(); - lcd.cls(); - #endif #ifdef DOGLCD_ENABLED lcd.init(); lcd.view(VIEW_TOP); #endif + led_green = 1; + load_disable = 0; + // ref_i_dcdc = 0; // 0 for buck, 1 for boost (TODO) + + // for RTC backup register + __PWR_CLK_ENABLE(); // normally in SystemClock_Config() + HAL_PWR_EnableBkUpAccess(); + + set_time(rtc_read_backup_reg(0)); + + input_Wh_day = float(rtc_read_backup_reg(1)) / 1000.0; // mWh + output_Wh_day = float(rtc_read_backup_reg(2)) / 1000.0; // mWh + input_Wh_total = float(rtc_read_backup_reg(3)); // Wh + output_Wh_total = float(rtc_read_backup_reg(4)); // Wh + mppt.frequency_kHz(_pwm_frequency); mppt.deadtime_ns(300); mppt.set_duty_cycle(0.90); mppt.lock_settings(); + mppt.set_current_limit(charge_current_max); + mppt.set_voltage_limit(num_cells * cell_voltage_max); + mppt.duty_cycle_limits((float) cell_voltage_load_disconnect * num_cells / max_solar_voltage, 0.97); - mppt.duty_cycle_limits((float) v_cell_load_disconnect * num_cells / max_solar_voltage, 0.97); + update_measurements(); - update_measurements(true); // initial setting of measurement values + // calibration of current sensors in no load condition + load_current_offset = -load_current; + dcdc_current_offset = -dcdc_current; serial.baud(115200); serial.printf("\nSerial interface started...\n"); + +#ifdef OLED_ENABLED + i2c.frequency(400000); +#endif + + freopen("/serial", "w", stdout); // retarget stdout +} + +void load_management() { + + if (battery_voltage < cell_voltage_load_disconnect * num_cells) { + load_disable = 1; + } + if (battery_voltage >= cell_voltage_load_reconnect * num_cells) { + load_disable = 0; + } } // actions to be performed upon entering a state @@ -184,44 +205,57 @@ void enter_state(int next_state) case STANDBY: serial.printf("Enter State: STANDBY\n"); mppt.stop(); - led1 = 1; - led3 = 0; + led_red = 0; break; case CHG_CC: serial.printf("Enter State: CHG_CC\n"); - mppt.set_duty_cycle((float) battery_voltage / (solar_voltage - 1000.0)); // start with proper PWM duty cycle - mppt.start(); - led3 = 1; + //serial.printf("P=%d Vsol=%d Vbat=%d Ibat=%d delta_time_CV=%d\n", solar_power, solar_voltage, battery_voltage, dcdc_current, time(NULL) - mppt.last_time_CV()); + mppt.set_current_limit(charge_current_max); + mppt.set_voltage_limit(num_cells * cell_voltage_max); + mppt.last_time_CV_reset(); + mppt.start((float) battery_voltage / (solar_voltage - 1000.0)); + led_red = 1; break; case CHG_CV: serial.printf("Enter State: CHG_CV\n"); - // coming from CHG_CC --> DC/DC FETs already on and charging enabled + //serial.printf("P=%d Vsol=%d Vbat=%d Ibat=%d delta_time_CV=%d\n", solar_power, solar_voltage, battery_voltage, dcdc_current, time(NULL) - mppt.last_time_CV()); + mppt.set_voltage_limit(num_cells * cell_voltage_max); break; case CHG_TRICKLE: serial.printf("Enter State: CHG_TRICKLE\n"); + //serial.printf("P=%d Vsol=%d Vbat=%d Ibat=%d delta_time_CV=%d\n", solar_power, solar_voltage, battery_voltage, dcdc_current, time(NULL) - mppt.last_time_CV()); + mppt.set_voltage_limit(num_cells * cell_voltage_trickle); + mppt.last_time_CV_reset(); + break; + + case CHG_EQUALIZATION: + serial.printf("Enter State: CHG_EQUALIZATION\n"); + mppt.set_voltage_limit(num_cells * cell_voltage_equalization); + mppt.last_time_CV_reset(); + mppt.set_current_limit(current_limit_equalization); break; case TEST: serial.printf("Enter State: TEST\n"); - mppt.set_duty_cycle(0.9); - mppt.start(); - led3 = 1; + mppt.start(0.50); + led_red = 1; break; case BAT_ERROR: serial.printf("Enter State: BAT_ERROR\n"); mppt.stop(); - led3 = 0; + led_red = 0; break; default: mppt.stop(); - led3 = 0; + led_red = 0; break; } + time_state_changed = time(NULL); state = next_state; } @@ -236,114 +270,73 @@ void enter_state(int next_state) * (MPPT algorithm is running) * CHG_CV Constant voltage (CV) charging at maximum cell voltage * BAT_ERROR An error occured. Charging disabled. - + * */ void state_machine() { + mppt.update(solar_voltage, battery_voltage, dcdc_current); + load_management(); + switch (state) { - case STANDBY: + case STANDBY: { if (solar_voltage > battery_voltage + solar_voltage_offset_start - && battery_voltage < num_cells * v_cell_recharge) { - //enter_state(CHG_BIKE); + && battery_voltage < num_cells * cell_voltage_recharge + && (time(NULL) - time_state_changed) > time_limit_recharge) + { enter_state(CHG_CC); } break; + } - - case CHG_CC: - //serial.printf("CHG_CC state still alive\n"); - if (battery_voltage > num_cells * v_cell_max) { + case CHG_CC: { + if (mppt.last_time_CV() <= time(NULL)) { + serial.printf("Last_time_cv = %d, time(NULL) = %d\n", mppt.last_time_CV(), time(NULL)); enter_state(CHG_CV); } - else if (dcdc_current > max_charge_current) { - // increase PV voltage - mppt.duty_cycle_step(-1); - } - else if (dcdc_current < charge_current_cutoff || - solar_voltage < battery_voltage + solar_voltage_offset_stop) { - //enter_state(BAT_ERROR); + else if (dcdc_current < charge_current_cutoff + || solar_voltage < battery_voltage + solar_voltage_offset_stop) { enter_state(STANDBY); - return; } - else { // start MPPT - - if (solar_power_prev > solar_power) { - pwm_delta = -pwm_delta; - } - mppt.duty_cycle_step(pwm_delta); - } - solar_power_prev = solar_power; break; + } - case CHG_CV: - - if (dcdc_current < charge_current_cutoff || - solar_voltage < battery_voltage + solar_voltage_offset_stop) { - // battery charging finished? - if (battery_voltage > (num_cells * v_cell_max - max_cv_chg_voltage_offset)) { - // TODO: define different current and time cut off (compare to Victron manual) + case CHG_CV: { + // cut-off limit reached because battery full (i.e. CV mode still + // reached by available solar power within last 2s) or CV period long enough? + if ((dcdc_current < current_cutoff_CV && (time(NULL) - mppt.last_time_CV()) < 2) + || (time(NULL) - time_state_changed) > time_limit_CV * 60) + { + if (equalization_enabled) { + // TODO: additional conditions! + enter_state(CHG_EQUALIZATION); + } + else if (trickle_enabled) { enter_state(CHG_TRICKLE); } else { enter_state(STANDBY); } - return; } - else if (dcdc_current > max_charge_current) { - // increase PV voltage - mppt.duty_cycle_step(-1); - } - else if (battery_voltage < (num_cells * v_cell_max - max_cv_chg_voltage_offset)) { - enter_state(CHG_CC); - } - else { - if (battery_voltage > num_cells * v_cell_max) { - // increase input voltage and decrease PV current - mppt.duty_cycle_step(-1); - } - else { - mppt.duty_cycle_step(1); - } - } - break; - - case CHG_TRICKLE: - - if (dcdc_current < charge_current_cutoff || - solar_voltage < battery_voltage + solar_voltage_offset_stop) { - // current cutoff will probably never be reached because of - // leakage current inside battery --> stay in trickle till end of day + else if (dcdc_current < charge_current_cutoff + || solar_voltage < battery_voltage + solar_voltage_offset_stop) { enter_state(STANDBY); - return; } - else if (dcdc_current > max_charge_current) { - // increase PV voltage - mppt.duty_cycle_step(-1); - } - else if (battery_voltage < (num_cells * v_cell_max - max_cv_chg_voltage_offset)) { + break; + } + + case CHG_TRICKLE: { + if (time(NULL) - mppt.last_time_CV() > time_trickle_recharge * 60) { enter_state(CHG_CC); } - else { - if (battery_voltage > num_cells * v_cell_trickle) { - // increase input voltage and decrease PV current - mppt.duty_cycle_step(-1); - } - else { - mppt.duty_cycle_step(1); - } + else if (dcdc_current < charge_current_cutoff + || solar_voltage < battery_voltage + solar_voltage_offset_stop) { + // current cutoff might never be reached because of lakage + // current inside battery --> stay in trickle till end of day + enter_state(STANDBY); } break; - - case BAT_ERROR: - //if (err == 0) { - // enter_state(STANDBY); - //} - break; - - case TEST: - - break; + } default: mppt.stop(); @@ -372,165 +365,232 @@ void energy_counter() nighttime = false; input_Wh_day = 0.0; output_Wh_day = 0.0; + seconds_day = 0; + day_counter++; + } + else { + seconds_day++; } input_Wh_day += solar_power / 1000.0 / 3600.0; output_Wh_day += load_current / 1000.0 * battery_voltage / 1000.0 / 3600.0; input_Wh_total += solar_power / 1000.0 / 3600.0; output_Wh_total += load_current / 1000.0 * battery_voltage / 1000.0 / 3600.0; + + // save values to RTC backup register + rtc_write_backup_reg(0, time(NULL)); + rtc_write_backup_reg(1, (uint32_t)(input_Wh_day * 1000)); // mWh + rtc_write_backup_reg(2, (uint32_t)(output_Wh_day * 1000)); // mWh + rtc_write_backup_reg(3, (uint32_t)(input_Wh_total)); // kWh + rtc_write_backup_reg(4, (uint32_t)(output_Wh_total)); // kWh } -#ifdef DOGLCD_ENABLED -void update_screen(void) +void log_data() { - char str[20]; + //FILE *fp = fopen("/sd/solar_log.csv", "a"); + FILE *fp = fopen("/serial_uext", "a"); - lcd.clear_screen(); + if(fp == NULL) { + serial.printf("Could not open file for write\n"); + fclose(fp); + return; + } - sprintf(str, "V Sol: %.2fV", solar_voltage / 1000.0); - lcd.string(0,0,font_6x8, str); - - sprintf(str, "V Bat: %.2fV", battery_voltage / 1000.0); - lcd.string(0,1,font_6x8, str); - - sprintf(str, "I DCDC %.2fA", dcdc_current / 1000.0); - lcd.string(0,2,font_6x8, str); - - sprintf(str, "I Load: %.2fA", load_current / 1000.0); - lcd.string(0,3,font_6x8, str); - - sprintf(str, "Power: %.2f W", solar_power / 1000.0); - lcd.string(0,4,font_6x8, str); - - sprintf(str, "PWM duty: %.1f %%", mppt.get_duty_cycle() * 100.0); - lcd.string(0,5,font_6x8, str); - - lcd.string(0,6,font_6x8,"State:"); + fprintf(fp, "%d;", (int)time(NULL)); + fprintf(fp, "%d;", day_counter); + fprintf(fp, "%.2f;%.1f;", solar_voltage / 1000.0, solar_power / 1000.0); + fprintf(fp, "%.2f;%.1f;", battery_voltage / 1000.0, dcdc_current / 1000.0); + fprintf(fp, "%.1f;%.2f;", (load_current < 0 ? 0 : load_current) / 1000.0 * battery_voltage / 1000.0, + (load_current < 0 ? 0 : load_current) / 1000.0); + fprintf(fp, "%d;", !load_disable); + fprintf(fp, "%.0f;%.0f;", input_Wh_day, fabs(output_Wh_day)); + fprintf(fp, "%.1f;%.1f;", input_Wh_total / 1000.0, fabs(output_Wh_total / 1000.0)); switch (state) { case STANDBY: - lcd.string(7*6,6,font_6x8,"Standby"); + fprintf(fp, "Standby;"); break; case CHG_CC: - lcd.string(7*6,6,font_6x8,"CC charge"); + fprintf(fp, "CC charge;"); break; case CHG_CV: - lcd.string(7*6,6,font_6x8,"CV charge"); + fprintf(fp, "CV charge;"); break; case CHG_TRICKLE: - lcd.string(7*6,6,font_6x8,"Trickle chg"); + fprintf(fp, "Trickle chg;"); break; case CHG_BIKE: - lcd.string(7*6,6,font_6x8,"Bike gen"); - break; - case TEST: - lcd.string(7*6,6,font_6x8,"Test mode"); - break; - } - - //sprintf(str, "Temp: %.1f C", temperature); - //lcd.string(0,7,font_6x8, str); -} -#endif // DISPLAY_ENABLED - -#ifdef NOKIALCD_ENABLED -void update_screen(void) -{ - char str[20]; - - lcd.cls(); - - sprintf(str, "S %.2fV %.1fW ", solar_voltage / 1000.0, solar_power / 1000.0); - lcd.writeString(0, 1, str, NORMAL); - - sprintf(str, "B %.2fV %.1fA ", battery_voltage / 1000.0, dcdc_current / 1000.0); - lcd.writeString(0, 2, str, NORMAL); - - if (load_disable) { - sprintf(str, "L cut off"); - lcd.writeString(0, 3, str, NORMAL); - } - else { - sprintf(str, "L %.1fW %.2fA ", load_current / 1000.0 * battery_voltage / 1000.0, load_current / 1000.0); - lcd.writeString(0, 3, str, NORMAL); - } - - sprintf(str, "D %.0fWh -%.0fWh ", input_Wh_day, output_Wh_day); - lcd.writeString(0, 4, str, NORMAL); - - sprintf(str, "T %.1fk -%.1fk ", input_Wh_total / 1000.0, output_Wh_total / 1000.0); - lcd.writeString(0, 5, str, NORMAL); - - switch (state) { - case STANDBY: - sprintf(str, "Standby"); - break; - case CHG_CC: - sprintf(str, "CC charge"); - break; - case CHG_CV: - sprintf(str, "CV charge"); - break; - case CHG_TRICKLE: - sprintf(str, "Trickle chg"); - break; - case CHG_BIKE: - sprintf(str, "Bike gen"); + fprintf(fp, "Bike gen;"); break; default: - sprintf(str, "Error"); + fprintf(fp, "Error;"); break; } - lcd.writeString(0, 6, str, NORMAL); + fprintf(fp, "\n"); + fclose(fp); } -#endif // NOKIALCD +float adc_read_avg (AnalogIn& input) { + // use read_u16 for faster calculation + unsigned int sum_adc_readings = 0; + for (int i = 0; i < ADC_AVG_SAMPLES; i++) { + sum_adc_readings += input.read_u16(); // normalized to 0xFFFF + } + return (float) (sum_adc_readings / ADC_AVG_SAMPLES) / (float)0xFFFF; +} //---------------------------------------------------------------------------- -void update_measurements(bool init) +void update_measurements() { - const int vcc = 3300; // mV - int temp = 0; + int vcc = 3300; + float tmp = 0.0; // low pass filter of measurement values: // y(n) = (1-c)*x(n) + c*y(n-1) with c=exp(-Ta/RC) - float c = 0.6; // filter constant + // https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter + float c = 0.2; // filter constant - // otherwise load will be directly disconnected - if (init == true) { + // first call of function + if (battery_voltage == 0) { c = 0.0; } +#ifdef MPPT_12A // solar voltage divider o-- 150k --o-- 6.8k --o - unsigned long sum_adc_readings = 0; - for (int i = 0; i < ADC_AVG_SAMPLES; i++) { - sum_adc_readings += v_solar.read_u16(); - } - temp = (sum_adc_readings / ADC_AVG_SAMPLES) * 1568 / 68 * vcc / 0xFFFF; - - solar_voltage = (1.0 - c) * temp + c * solar_voltage; + tmp = adc_read(v_solar) * vcc * 1568 / 68; + solar_voltage = (100 - c) * tmp + c * solar_voltage; // battery voltage divider o-- 120k --o-- 10k --o - sum_adc_readings = 0; - for (int i = 0; i < ADC_AVG_SAMPLES; i++) { - sum_adc_readings += v_bat.read_u16(); - } - temp = (sum_adc_readings / ADC_AVG_SAMPLES) * 130 / 10 * vcc / 0xFFFF; - battery_voltage = (1.0 - c) * temp + c * battery_voltage; + tmp = adc_read(v_bat) * vcc * 130 / 10; + battery_voltage = (100 - c) * tmp + c * battery_voltage; - sum_adc_readings = 0; - for (int i = 0; i < ADC_AVG_SAMPLES; i++) { - sum_adc_readings += i_dcdc.read_u16(); - } - temp = (sum_adc_readings / ADC_AVG_SAMPLES) * 1000 / 5 / 50 * vcc / 0xFFFF; - dcdc_current = (1.0 - c) * temp + c * dcdc_current; + tmp = adc_read(i_dcdc) * vcc * 1000 / 5 / 50; + dcdc_current = (1.0 - c) * (tmp + dcdc_current_offset) + c * dcdc_current; - sum_adc_readings = 0; - for (int i = 0; i < ADC_AVG_SAMPLES; i++) { - sum_adc_readings += i_load.read_u16(); - } - temp = (sum_adc_readings / ADC_AVG_SAMPLES) * 1000 / 5 / 50 * vcc / 0xFFFF; - load_current = (1.0 - c) * temp + c * load_current; + tmp = adc_read(i_load) * vcc * 1000 / 5 / 50; + load_current = (1.0 - c) * (tmp + load_current_offset) + c * load_current; solar_power = battery_voltage * dcdc_current / 1000; +#endif + +#ifdef MPPT_20A + // reference voltage (should be 2.5V) + vcc = 2500 / adc_read_avg(v_ref); + + // solar voltage divider o-- 100k --o-- 5.6k --o + tmp = adc_read_avg(v_solar) * vcc * 1056 / 56; + solar_voltage = (1.0 - c) * tmp + c * solar_voltage; + + // battery voltage divider o-- 100k --o-- 10k --o + tmp = adc_read_avg(v_bat) * vcc * 110 / 10; + battery_voltage = (1.0 - c) * tmp + c * battery_voltage; + + // dcdc current (op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm) + tmp = adc_read_avg(i_dcdc) * vcc * 1000 / 2 / (1500 / 22); + dcdc_current = (1.0 - c) * (tmp + dcdc_current_offset) + c * dcdc_current; + + // load current (op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm) + tmp = adc_read_avg(i_load) * vcc * 1000 / 2 / (1500 / 22); + load_current = (1.0 - c) * (tmp + load_current_offset) + c * load_current; + + solar_power = battery_voltage * dcdc_current / 1000; + + // temperature sensed by NTC + tmp = adc_read_avg(v_temp) * vcc; // voltage read by ADC + unsigned long rts = 10000.0 * tmp / (3300.0 - tmp); // Ohm + // Temperature calculation using Beta equation for 10k thermistor + // (25°C reference temperature for Beta equation assumed) + float temp_K = 1.0/(1.0/(273.15+25) + 1.0/thermistorBetaValue*log(rts/10000.0)); // K + temperature = (1.0 - c) * (temp_K - 273.15) + c * temperature; +#endif + } + +uint32_t rtc_read_backup_reg(uint32_t BackupRegister) { + RTC_HandleTypeDef RtcHandle; + RtcHandle.Instance = RTC; + return HAL_RTCEx_BKUPRead(&RtcHandle, BackupRegister); +} + +void rtc_write_backup_reg(uint32_t BackupRegister, uint32_t data) { + RTC_HandleTypeDef RtcHandle; + RtcHandle.Instance = RTC; + HAL_PWR_EnableBkUpAccess(); + HAL_RTCEx_BKUPWrite(&RtcHandle, BackupRegister, data); + HAL_PWR_DisableBkUpAccess(); +} + + +#ifdef OLED_ENABLED +void update_screen(void) +{ + oled.clearDisplay(); + oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1); + oled.drawBitmap(104, 0, bmp_load, 16, 16, 1); + + if (state == STANDBY) { + oled.drawBitmap(27, 3, bmp_disconnected, 32, 8, 1); + } + else { + oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1); + } + + if (load_disable == 1) { + oled.drawBitmap(81, 3, bmp_disconnected, 17, 7, 1); + } + else { + oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1); + } + + oled.drawRect(52, 2, 18, 9, 1); // battery shape + oled.drawRect(69, 3, 3, 7, 1); // battery terminal + oled.drawRect(54, 4, 2, 5, 1); // bar 1 + oled.drawRect(57, 4, 2, 5, 1); // bar 2 + oled.drawRect(60, 4, 2, 5, 1); // bar 3 + oled.drawRect(63, 4, 2, 5, 1); // bar 4 + oled.drawRect(66, 4, 2, 5, 1); // bar 5 + + oled.setTextCursor(0, 18); + oled.printf("%4.1fV", solar_voltage / 1000.0); + oled.setTextCursor(0, 26); + oled.printf("%4.0fW", abs(solar_power) / 1000.0); + + oled.setTextCursor(42, 18); + oled.printf("%5.2fV", battery_voltage / 1000.0); + oled.setTextCursor(42, 26); + oled.printf("%5.2fA", abs(dcdc_current) / 1000.0); + + oled.setTextCursor(90, 18); + oled.printf("%5.2fA\n", abs(load_current) / 1000.0); + oled.setTextCursor(90, 26); + oled.printf("%5.1fW", abs(load_current) / 1000.0 * battery_voltage / 1000.0); + + oled.setTextCursor(0, 36); + oled.printf("Day +%5.0fWh -%5.0fWh", input_Wh_day, fabs(output_Wh_day)); + oled.printf("Tot +%4.1fkWh -%4.1fkWh", input_Wh_total / 1000.0, fabs(output_Wh_total) / 1000.0); + + oled.setTextCursor(0, 56); + oled.printf("PWM %.1f%% ", mppt.get_duty_cycle() * 100.0); + switch (state) { + case STANDBY: + oled.printf("Standby\n"); + break; + case CHG_CC: + oled.printf("CC charge\n"); + break; + case CHG_CV: + oled.printf("CV charge\n"); + break; + case CHG_TRICKLE: + oled.printf("Trickle chg\n"); + break; + case CHG_BIKE: + oled.printf("Bike gen\n"); + break; + default: + oled.printf("Error\n"); + break; + } + oled.display(); +} +#endif // OLED