Skip to content

Commit 1a23ee7

Browse files
committed
2 parents 539fe96 + 58bd3f3 commit 1a23ee7

File tree

5 files changed

+111
-27
lines changed

5 files changed

+111
-27
lines changed

src/Inkplate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Inkplate : public System, public Graphics
6868
// void writeRow(uint8_t data);
6969
uint32_t partialUpdate(bool _forced = false, bool leaveOn = false);
7070

71-
#if defined(ARDUINO_INKPLATECOLOR)
71+
#if defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATE4)
7272
void clean();
7373

7474
// These 4 functions need to refactored because conflicting functionalities
@@ -78,7 +78,7 @@ class Inkplate : public System, public Graphics
7878
bool getPanelDeepSleepState();
7979

8080
void setIOExpanderForLowPower();
81-
#elif defined(ARDUINO_INKPLATE2) || defined(ARDUINO_INKPLATE4)
81+
#elif defined(ARDUINO_INKPLATE2)
8282
void clean();
8383

8484
// These 4 functions need to refactored because conflicting functionalities

src/boards/Inkplate4.cpp

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#ifdef ARDUINO_INKPLATE4
2424

25-
SPISettings epdSpiSettings(1000000UL, MSBFIRST, SPI_MODE0);
25+
SPISettings epdSpiSettings(4000000UL, MSBFIRST, SPI_MODE0);
2626

2727
/**
2828
* @brief begin function initialize Inkplate object with predefined
@@ -54,12 +54,15 @@ bool Inkplate::begin()
5454
return false;
5555
}
5656

57+
// Init TwoWire and GPIO expander
58+
setIOExpanderForLowPower();
59+
60+
// Set the default text color
61+
setTextColor(INKPLATE_BLACK);
62+
5763
// Clear frame buffer
5864
clearDisplay();
5965

60-
// Set default rotation
61-
setRotation(0);
62-
6366
_beginDone = 1;
6467
}
6568

@@ -150,7 +153,7 @@ void Inkplate::display(bool leaveOn) // Leave on does nothing
150153
sendCommand(0x20); // Activate Display Update Sequence
151154

152155
delayMicroseconds(500); // Wait at least 200 uS
153-
waitForEpd(60000);
156+
waitForEpd(24000);
154157
}
155158

156159
/**
@@ -165,15 +168,42 @@ void Inkplate::setPanelDeepSleep(bool _state)
165168

166169
if (_panelState)
167170
{
171+
// Set SPI pins
172+
SPI.begin(EPAPER_CLK, -1, EPAPER_DIN, -1);
173+
174+
// Set up EPD communication pins
175+
pinMode(EPAPER_CS_PIN, OUTPUT);
176+
pinMode(EPAPER_DC_PIN, OUTPUT);
177+
pinMode(EPAPER_RST_PIN, OUTPUT);
178+
pinMode(EPAPER_BUSY_PIN, INPUT_PULLUP);
179+
180+
delay(10);
181+
168182
// Send commands to power up panel. According to the datasheet, it can be
169183
// powered up from deep sleep only by reseting it and doing reinit.
170184
begin();
171185
}
172186
else
173187
{
174-
sendCommand(0x10);
175-
sendData(0x01);
176-
delay(100);
188+
sendCommand(0X50); // VCOM and data interval setting
189+
sendData(0xf7);
190+
191+
sendCommand(0X02); // Power EPD off
192+
waitForEpd(BUSY_TIMEOUT_MS);
193+
sendCommand(0X07); // Put EPD in deep sleep
194+
sendData(0xA5);
195+
delay(1);
196+
197+
// Disable SPI
198+
SPI.end();
199+
200+
// To reduce power consumption, set SPI pins as outputs
201+
pinMode(EPAPER_RST_PIN, INPUT);
202+
pinMode(EPAPER_DC_PIN, INPUT);
203+
pinMode(EPAPER_CS_PIN, INPUT);
204+
pinMode(EPAPER_BUSY_PIN, INPUT);
205+
pinMode(EPAPER_CLK, INPUT);
206+
pinMode(EPAPER_DIN, INPUT);
177207
}
178208
}
179209

@@ -295,19 +325,22 @@ void Graphics::writePixel(int16_t x0, int16_t y0, uint16_t _color)
295325
{
296326
if (x0 > width() - 1 || y0 > height() - 1 || x0 < 0 || y0 < 0)
297327
return;
328+
298329
if (_color > 2)
299330
return;
300331

301332
switch (rotation)
302333
{
303334
case 0:
304335
x0 = width() - 1 - x0;
336+
y0 = y0 + 1;
305337
break;
306338
case 1:
307339
_swap_int16_t(x0, y0);
308340
break;
309341
case 2:
310342
y0 = height() - y0 - 1;
343+
x0 = x0 + 1;
311344
break;
312345
case 3:
313346
_swap_int16_t(x0, y0);
@@ -323,14 +356,64 @@ void Graphics::writePixel(int16_t x0, int16_t y0, uint16_t _color)
323356
// half is for red pixels only
324357
if (_color < 2)
325358
{
359+
// Draw the Black/White pixel and clear the red pixel
326360
*(DMemory4Bit + E_INK_WIDTH / 8 * y0 + _x) |= (pixelMaskLUT[7 - _x_sub]);
327361
*(DMemory4Bit + E_INK_WIDTH / 8 * y0 + _x) &= ~(_color << (7 - _x_sub));
328362
*(DMemory4Bit + (E_INK_WIDTH * E_INK_HEIGHT / 8) + E_INK_WIDTH / 8 * y0 + _x) &= ~(pixelMaskLUT[7 - _x_sub]);
329363
}
330364
else
331365
{
366+
// Draw a red pixel in the set coordinates
332367
*(DMemory4Bit + (E_INK_WIDTH * E_INK_HEIGHT / 8) + E_INK_WIDTH / 8 * y0 + _x) |= (pixelMaskLUT[7 - _x_sub]);
333368
}
334369
}
335370

371+
/**
372+
* @brief setIOExpanderForLowPower initiates I/O Expander pins for low power, and puts
373+
* them in OUTPUT LOW because they are using least amount of current in deep
374+
* sleep that way
375+
*/
376+
void Inkplate::setIOExpanderForLowPower()
377+
{
378+
Wire.begin();
379+
memset(ioRegsInt, 0, 22);
380+
ioBegin(IO_INT_ADDR, ioRegsInt);
381+
382+
// TOUCHPAD PINS
383+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B2, INPUT);
384+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B3, INPUT);
385+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B4, INPUT);
386+
387+
// Battery voltage Switch MOSFET
388+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B1, OUTPUT);
389+
390+
// Rest of pins go to OUTPUT LOW state because in deepSleep mode they are
391+
// using least amount of power
392+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A0, OUTPUT);
393+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A1, OUTPUT);
394+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A2, OUTPUT);
395+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A3, OUTPUT);
396+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A4, OUTPUT);
397+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A5, OUTPUT);
398+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A6, OUTPUT);
399+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A7, OUTPUT);
400+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B0, OUTPUT);
401+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B5, OUTPUT);
402+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B6, OUTPUT);
403+
pinModeInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B7, OUTPUT);
404+
405+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A0, LOW);
406+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A1, LOW);
407+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A2, LOW);
408+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A3, LOW);
409+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A4, LOW);
410+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A5, LOW);
411+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A6, LOW);
412+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_A7, LOW);
413+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B0, LOW);
414+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B5, LOW);
415+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B6, LOW);
416+
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, IO_PIN_B7, LOW);
417+
}
418+
336419
#endif

src/boards/Inkplate4.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#ifndef INKPLATE4_H
2020
#define INKPLATE4_H
2121

22-
// So compiler knows we need Inkplate 2 features
23-
// If there is a good board def this isn't required
2422
#define EPAPER_RST_PIN 19
2523
#define EPAPER_DC_PIN 33
2624
#define EPAPER_CS_PIN 27
@@ -30,16 +28,18 @@
3028

3129
#define BUSY_TIMEOUT_MS 1000
3230

33-
#define E_INK_HEIGHT 300
34-
#define E_INK_WIDTH 400
31+
// ePaper specific defines
32+
#define E_INK_HEIGHT 300
33+
#define E_INK_WIDTH 400
34+
#define INKPLATE_WHITE 0b00000000
35+
#define INKPLATE_BLACK 0b00000001
36+
#define INKPLATE_RED 0b00000010
3537

36-
#define IO_INT_ADDR 0x20
37-
#define IO_EXT_ADDR 0x21
38+
// Pin on the internal io expander which controls MOSFET for turning on and off the SD card
39+
#define SD_PMOS_PIN IO_PIN_B2 // 10
3840

39-
// in defines.h
40-
// #define WHITE 0
41-
// #define BLACK 1
42-
#define RED 2
41+
#define IO_INT_ADDR 0x20
42+
#define IO_EXT_ADDR 0x20
4343

4444
#ifndef _swap_int16_t
4545
#define _swap_int16_t(a, b) \

src/include/Pcal.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
***************************************************/
1818

1919
#if defined(ARDUINO_INKPLATE10V2) || defined(ARDUINO_INKPLATE6V2) || defined(ARDUINO_INKPLATE6PLUSV2) || \
20-
defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATECOOL) || defined(ARDUINO_INKPLATE5)
20+
defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATECOOL) || defined(ARDUINO_INKPLATE5) || \
21+
defined(ARDUINO_INKPLATE4)
2122

2223
#include "Pcal.h"
2324

@@ -189,7 +190,7 @@ void Expander::updatePCALRegister(uint8_t _addr, uint8_t _regIndex, uint8_t *k,
189190
*/
190191
void Expander::pinModeIO(uint8_t _pin, uint8_t _mode, uint8_t _ioID)
191192
{
192-
#ifndef ARDUINO_INKPLATECOLOR
193+
#if !defined(ARDUINO_INKPLATECOLOR) && !defined(ARDUINO_INKPLATE4)
193194
if ((_ioID == IO_INT_ADDR) && (_pin < 9))
194195
return;
195196
#endif
@@ -210,7 +211,7 @@ void Expander::pinModeIO(uint8_t _pin, uint8_t _mode, uint8_t _ioID)
210211
*/
211212
void Expander::digitalWriteIO(uint8_t _pin, uint8_t _state, uint8_t _ioID)
212213
{
213-
#ifndef ARDUINO_INKPLATECOLOR
214+
#if !defined(ARDUINO_INKPLATECOLOR) && !defined(ARDUINO_INKPLATE4)
214215
if ((_ioID == IO_INT_ADDR) && (_pin < 9))
215216
return;
216217
#endif
@@ -229,7 +230,7 @@ void Expander::digitalWriteIO(uint8_t _pin, uint8_t _state, uint8_t _ioID)
229230
*/
230231
uint8_t Expander::digitalReadIO(uint8_t _pin, uint8_t _ioID)
231232
{
232-
#ifndef ARDUINO_INKPLATECOLOR
233+
#if !defined(ARDUINO_INKPLATECOLOR) && !defined(ARDUINO_INKPLATE4)
233234
if ((_ioID == IO_INT_ADDR) && (_pin < 9))
234235
return 0;
235236
#endif
@@ -293,7 +294,7 @@ void Expander::setPorts(uint16_t _d, uint8_t _ioID)
293294
{
294295
// Can't use this function on internal IO Expander except on a Inkplate Color!
295296
// Using this function can damage Inkplate!
296-
#ifndef ARDUINO_INKPLATECOLOR
297+
#if !defined(ARDUINO_INKPLATECOLOR) && !defined(ARDUINO_INKPLATE4)
297298
if ((_ioID == IO_INT_ADDR))
298299
return;
299300
#endif

src/include/System.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ int16_t System::sdCardInit()
152152
{
153153
// New Soldered Inkplate boards use P-MOS to disable supply to the uSD card to reduce power in deep sleep.
154154
#if defined(ARDUINO_INKPLATE6V2) || defined(ARDUINO_INKPLATE10V2) || defined(ARDUINO_INKPLATE6PLUSV2) || \
155-
defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATE5)
155+
defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATE5) || defined(ARDUINO_INKPLATE4)
156156
pinModeInternal(IO_INT_ADDR, ioRegsInt, SD_PMOS_PIN, OUTPUT);
157157
digitalWriteInternal(IO_INT_ADDR, ioRegsInt, SD_PMOS_PIN, LOW);
158158
delay(50);
@@ -168,7 +168,7 @@ int16_t System::sdCardInit()
168168
void System::sdCardSleep()
169169
{
170170
#if defined(ARDUINO_INKPLATE6V2) || defined(ARDUINO_INKPLATE10V2) || defined(ARDUINO_INKPLATE6PLUSV2) || \
171-
defined(ARDUINO_INKPLATECOLOR)
171+
defined(ARDUINO_INKPLATECOLOR) || defined(ARDUINO_INKPLATE4)
172172
// Set SPI pins to input to reduce power consumption in deep sleep
173173
pinMode(12, INPUT);
174174
pinMode(13, INPUT);

0 commit comments

Comments
 (0)