Skip to content

Commit

Permalink
chore(display): Add patches files for MCUFRIEND_kbv (#11)
Browse files Browse the repository at this point in the history
Support auto patch in PIO
  • Loading branch information
XavierBrassoud committed May 3, 2024
1 parent 98b524b commit f95207f
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ The following are some guidelines to observe when creating issues or PRs:

- Ensure your code are properly formed with `clang-format` and passes quality gates with `pio checks`

- Using [Visual Studio Code](https://code.visualstudio.com/) with the [PlatformIO](https://platformio.org/) [extension](https://platformio.org/install/ide?install=vscode) is highly recommended
- Using [Visual Studio Code](https://code.visualstudio.com/) with the [PlatformIO](https://platformio.org/) [extension](https://platformio.org/install/ide?install=vscode) is highly recommended. The environment will be automatically configured, select an example environment and PIO will build the project for you.
55 changes: 32 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,38 @@ The control panel embed a GIANTPLUS ILI9163C screen driven by 8-bit parallel int
This library is not a GFX library. You can rely on [Adafruit GFX](https://github.com/adafruit/Adafruit-GFX-Library) library derivatives,
such as the excellent [MCUFRIEND_kbv](https://github.com/prenticedavid/MCUFRIEND_kbv) library.
Here is an adaptation using [MCUFRIEND_kbv](https://github.com/prenticedavid/MCUFRIEND_kbv) library:
1. Locate your [MCUFRIEND_kbv v3](https://github.com/prenticedavid/MCUFRIEND_kbv) library:
1. [PlatformIO](https://platformio.org/) users: directly edit in `.pio/libdeps/<your_project>/MCUFRIEND_kbv` **[⚠️ Non-persistent]** — or — [Override package files](https://docs.platformio.org/en/stable/scripting/examples/override_package_files.html).
2. Other users: download [MCUFRIEND_kbv v3](https://github.com/prenticedavid/MCUFRIEND_kbv)
2. Edit *MCUFRIEND_kbv/utility/mcufriend_shield.h*:
1. Uncomment `#define USE_SPECIAL`
3. Edit *MCUFRIEND_kbv/MCUFRIEND_kbv.cpp*:
1. Uncomment `#define SUPPORT_9163`
2. Go to `#ifdef SUPPORT_9163` section
3. Replace `*p16 = 160;` by `*p16 = 128;`
4. Edit *MCUFRIEND_kbv/utility/mcufriend_special.h*:
1. Write to the top of mcufriend_special.h `#define USE_EPSON_PNL_CE02`
2. Copy content from *extras/mcufriend_specials/<ARDUINO_TYPE>.h* file
3. Paste to the SPECIAL definitions of *mcufriend_special.h*, somewhere between `#if` and `#else`
5. Your code requires:
1. Definition for `Epson_PNL_CE02 controlPanel(...)`
2. Turn display ON before INIT:
``` c++
controlPanel.extenderWrite(ExtenderPin::LCD_BACKLIGHT, HIGH);
tft.begin(0x9163);
```

`examples/display` and `examples/full` depend on the adaptation above.
Bellow an adaptation using [MCUFRIEND_kbv](https://github.com/prenticedavid/MCUFRIEND_kbv) library, `examples/display` and `examples/full` are based on.
* [PlatformIO](https://platformio.org/) users:
1. Require `patch` tool installed in your system.
2. Copy `pio-tools/patches.py` and `patches/` folder to the root of your project.
3. Adding this line to your `platformio.ini`:
``` ini
lib_deps =
prenticedavid/MCUFRIEND_kbv @ ^3
adafruit/Adafruit GFX Library @ ^1
adafruit/Adafruit BusIO @ ^1
Wire
SPI
extra_scripts = pre:pio-tools/patches.py
```

* Other users:
1. Download [MCUFRIEND_kbv v3](https://github.com/prenticedavid/MCUFRIEND_kbv)
2. Apply `patch` command with diff files provided in `patches/` folder:
``` sh
patch MCUFRIEND_kbv/MCUFRIEND_kbv.cpp patches/MCUFRIEND_kbv/MCUFRIEND_kbv.cpp
patch MCUFRIEND_kbv/utility/mcufriend_special.h patches/MCUFRIEND_kbv/utility/mcufriend_special.h
patch MCUFRIEND_kbv/utility/mcufriend_shield.h patches/MCUFRIEND_kbv/utility/mcufriend_shield.h
```

Nice! Now your code requires this final steps:
1. Definition for `Epson_PNL_CE02 controlPanel(...)`
2. Turn display ON before INIT:
``` c++
controlPanel.extenderWrite(ExtenderPin::LCD_BACKLIGHT, HIGH);
tft.begin(0x9163);
```

[examples/display]([examples/display/display.ino]):
``` c++
Expand Down
8 changes: 8 additions & 0 deletions patches/MCUFRIEND_kbv/MCUFRIEND_kbv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
18c18
< //#define SUPPORT_9163 //
---
> #define SUPPORT_9163 //
2227c2227
< *p16 = 160;
---
> *p16 = 128;
4 changes: 4 additions & 0 deletions patches/MCUFRIEND_kbv/utility/mcufriend_shield.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1c1
< //#define USE_SPECIAL //check for custom drivers
---
> #define USE_SPECIAL //check for custom drivers
94 changes: 94 additions & 0 deletions patches/MCUFRIEND_kbv/utility/mcufriend_special.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
2a3
> #define USE_EPSON_PNL_CE02 // 6.39sec
47a49,139
> #elif defined(__AVR_ATmega2560__) && defined(USE_EPSON_PNL_CE02)
> // LCD pins |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 | |RD |WR |RST |RS |CS |
> // EPSON_PNL_CE02 | Parallel multiplexer 9 & 10 | |VCC |WR |RST | Register 9, 10 & 11 |
> // ATmega2560 | PG2 (SCK) | PG1 (SER IN) | |- |PL0 |PL2 |PB1 |PB2 |PL1 |
> // MEGA2560pin | 51 | 52 | |- |49 |47 | 52 |51 |48 |
>
> #include <Arduino.h>
> #include <Epson_PNL_CE02.h>
> extern Epson_PNL_CE02 controlPanel;
>
> #warning USE_MEGA_8BIT_SHIELD for EPSON_PNL_CE02
>
> #define WR_PORT PORTL
> #define WR_PIN 0 // D49 CTE
> #define RESET_PORT PORTL
> #define RESET_PIN 2 // D25 CTE
>
> #define VIRTUAL_PORT _MMIO_BYTE(0XFF) // Set Virtual Port for PIN_ methods
> #define CD_PORT VIRTUAL_PORT
> #define CD_PIN static_cast<int>(ExtenderPin::LCD_DC) // D4 VHC595
> #define CS_PORT VIRTUAL_PORT
> #define CS_PIN static_cast<int>(ExtenderPin::LCD_CS) // D3 VHC595
>
> #define UNUSED_PORT _MMIO_BYTE(0XFE) // Disable READ methods as the control panel wired the display in write-only mode
> #define RD_PORT UNUSED_PORT
> #define RD_PIN -1
>
> #define write_8(x) ({ controlPanel.displayWrite(x); })
>
> #define read_8() (PINC)
> #define setWriteDir() ({ DDRC = 0xFF; })
> #define setReadDir() ({ DDRC = 0x00; })
> #define write8(x) \
> ({ \
> write_8(x); \
> WR_ACTIVE; \
> WR_STROBE; \
> })
> #define write16(x) \
> ({ \
> uint8_t h = (x) >> 8, l = x; \
> write8(h); \
> write8(l); \
> })
> #define READ_8(dst) \
> ({ \
> RD_STROBE; \
> dst = read_8(); \
> RD_IDLE; \
> })
> #define READ_16(dst) \
> ({ \
> RD_STROBE; \
> dst = read_8(); \
> RD_IDLE; \
> RD_STROBE; \
> dst = (dst << 8) | read_8(); \
> RD_IDLE; \
> })
>
> #define PIN_LOW(p, b) \
> ({ \
> if (&p != &VIRTUAL_PORT) \
> { \
> (p) &= ~(1 << (b)); \
> } \
> else \
> { \
> controlPanel.extenderWrite((ExtenderPin)b, LOW); \
> } \
> })
>
> #define PIN_HIGH(p, b) \
> ({ \
> if (&p != &VIRTUAL_PORT) \
> { \
> (p) |= (1 << (b)); \
> } \
> else \
> { \
> controlPanel.extenderWrite((ExtenderPin)b, HIGH); \
> } \
> })
>
> #define PIN_OUTPUT(p, b) \
> ({ \
> if (&p != &VIRTUAL_PORT) \
> { \
> *(&p - 1) |= (1 << (b)); \
> } \
> })
30 changes: 30 additions & 0 deletions pio-tools/patches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Script to override package files.
# Apply patches from "patches" folder into ".pio/lib_deps/<env>/"
# The "patches" folder must match ".pio/lib_deps/<env>/" structure

from os import walk
from os.path import join, isfile, relpath

Import("env")

LIB_DEPS_DIR = join(env.subst("$PROJECT_LIBDEPS_DIR"), env.subst("$PIOENV"))
PATCHES_DIR = "patches"

for dir_, _, files in walk(PATCHES_DIR):
for file_name in files:
rel_dir = relpath(dir_, PATCHES_DIR)
rel_file = join(rel_dir, file_name)

original_file = join(LIB_DEPS_DIR, rel_file)
patch_file = join(PATCHES_DIR, rel_file)

patched = isfile(f"{original_file}.patched")

# patch file only if we didn't do it before
if not patched:

assert isfile(original_file) and isfile(patch_file)

print("patch %s %s" % (original_file, patch_file))
env.Execute("patch %s %s" % (original_file, patch_file))
env.Execute("touch " + f"{original_file}.patched")
7 changes: 6 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ custom_example_dir = examples/blink
custom_example_dir = examples/buttons

[env:example_display]
extra_scripts =
${env.extra_scripts}
pre:pio-tools/patches.py ; Apply patches needed by EPSON_PNL_CE02 on MCUFRIEND_kbv to use display
custom_example_dir = examples/display
lib_deps =
prenticedavid/MCUFRIEND_kbv @ ^3
Expand All @@ -41,6 +44,9 @@ lib_deps =
SPI

[env:example_full]
extra_scripts =
${env.extra_scripts}
pre:pio-tools/patches.py ; Apply patches needed by EPSON_PNL_CE02 on MCUFRIEND_kbv to use display
custom_example_dir = examples/full
lib_deps =
mathertel/OneButton @ ^2.5.0
Expand All @@ -49,4 +55,3 @@ lib_deps =
adafruit/Adafruit BusIO @ ^1
Wire
SPI

0 comments on commit f95207f

Please sign in to comment.