Skip to content

Commit 43382ba

Browse files
committed
Inky 7.3: Add ShiftRegister driver.
1 parent 93979cb commit 43382ba

File tree

9 files changed

+127
-48
lines changed

9 files changed

+127
-48
lines changed

drivers/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ add_subdirectory(pms5003)
4141
add_subdirectory(sh1107)
4242
add_subdirectory(st7567)
4343
add_subdirectory(psram_display)
44-
add_subdirectory(inky73)
44+
add_subdirectory(inky73)
45+
add_subdirectory(shiftregister)

drivers/inky73/inky73.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ target_sources(${DRIVER_NAME} INTERFACE
77
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
88

99
# Pull in pico libraries that we need
10-
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_spi)
10+
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_spi shiftregister)

drivers/inky73/inky73.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,10 @@ namespace pimoroni {
4444
};
4545

4646
bool UC8159Inky7::is_busy() {
47-
if(BUSY == PIN_UNUSED) {
48-
if(absolute_time_diff_us(get_absolute_time(), timeout) > 0) {
49-
return true;
50-
} else {
51-
return false;
52-
}
53-
}
54-
return !gpio_get(BUSY);
47+
return !(sr.read() & 128);
5548
}
5649

57-
void UC8159Inky7::busy_wait(uint minimum_wait_ms) {
58-
timeout = make_timeout_time_ms(minimum_wait_ms);
50+
void UC8159Inky7::busy_wait() {
5951
while(is_busy()) {
6052
tight_loop_contents();
6153
}
@@ -82,10 +74,6 @@ namespace pimoroni {
8274
gpio_set_dir(RESET, GPIO_OUT);
8375
gpio_put(RESET, 1);
8476

85-
gpio_set_function(BUSY, GPIO_FUNC_SIO);
86-
gpio_set_dir(BUSY, GPIO_IN);
87-
gpio_set_pulls(BUSY, true, false);
88-
8977
gpio_set_function(SCK, GPIO_FUNC_SPI);
9078
gpio_set_function(MOSI, GPIO_FUNC_SPI);
9179
};
@@ -189,18 +177,20 @@ namespace pimoroni {
189177
busy_wait();
190178

191179
command(PON, {0}); // turn on
192-
busy_wait(200);
180+
busy_wait();
193181

194182
command(DRF, {0}); // start display refresh
195-
busy_wait(200);
183+
busy_wait();
196184

197185
if(blocking) {
198-
busy_wait(32 * 1000);
186+
busy_wait();
199187

200188
command(POF); // turn off
201-
} else {
202-
timeout = make_timeout_time_ms(32 * 1000);
203189
}
204190
}
205191

192+
bool UC8159Inky7::is_pressed(Button button) {
193+
return sr.read() & button;
194+
}
195+
206196
}

drivers/inky73/inky73.hpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "common/pimoroni_common.hpp"
99
#include "common/pimoroni_bus.hpp"
1010
#include "libraries/pico_graphics/pico_graphics.hpp"
11+
#include "drivers/shiftregister/shiftregister.hpp"
1112

1213
namespace pimoroni {
1314

@@ -23,14 +24,25 @@ namespace pimoroni {
2324
uint DC = 28; // 27;
2425
uint SCK = SPI_DEFAULT_SCK;
2526
uint MOSI = SPI_DEFAULT_MOSI;
26-
uint BUSY = PIN_UNUSED;
2727
uint RESET = 27; //25;
2828

29-
absolute_time_t timeout;
29+
uint SR_CLOCK = 8;
30+
uint SR_LATCH = 9;
31+
uint SR_DATA = 10;
3032

3133
bool blocking = false;
3234

35+
ShiftRegister<uint8_t> sr = ShiftRegister<uint8_t>(SR_CLOCK, SR_LATCH, SR_DATA);
36+
3337
public:
38+
enum Button : uint8_t {
39+
BUTTON_A = 1,
40+
BUTTON_B = 2,
41+
BUTTON_C = 4,
42+
BUTTON_D = 8,
43+
BUTTON_E = 16
44+
};
45+
3446
enum colour : uint8_t {
3547
BLACK = 0,
3648
WHITE = 1,
@@ -44,12 +56,12 @@ namespace pimoroni {
4456

4557
UC8159Inky7(uint16_t width, uint16_t height) : UC8159Inky7(width, height, ROTATE_0, {PIMORONI_SPI_DEFAULT_INSTANCE, SPI_BG_FRONT_CS, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, 28, PIN_UNUSED}) {};
4658

47-
UC8159Inky7(uint16_t width, uint16_t height, SPIPins pins, uint busy=PIN_UNUSED, uint reset=27) : UC8159Inky7(width, height, ROTATE_0, pins, busy, reset) {};
59+
UC8159Inky7(uint16_t width, uint16_t height, SPIPins pins, uint reset=27) : UC8159Inky7(width, height, ROTATE_0, pins, reset) {};
4860

49-
UC8159Inky7(uint16_t width, uint16_t height, Rotation rotation, SPIPins pins, uint busy=PIN_UNUSED, uint reset=27) :
61+
UC8159Inky7(uint16_t width, uint16_t height, Rotation rotation, SPIPins pins, uint reset=27) :
5062
DisplayDriver(width, height, rotation),
5163
spi(pins.spi),
52-
CS(pins.cs), DC(pins.dc), SCK(pins.sck), MOSI(pins.mosi), BUSY(busy), RESET(reset) {
64+
CS(pins.cs), DC(pins.dc), SCK(pins.sck), MOSI(pins.mosi), RESET(reset) {
5365
init();
5466
}
5567

@@ -58,7 +70,7 @@ namespace pimoroni {
5870
// Methods
5971
//--------------------------------------------------
6072
public:
61-
void busy_wait(uint minimum_wait_ms=0);
73+
void busy_wait();
6274
void reset();
6375
void power_off();
6476

@@ -67,6 +79,8 @@ namespace pimoroni {
6779

6880
void set_blocking(bool blocking);
6981

82+
bool is_pressed(Button button);
83+
7084
private:
7185
void init();
7286
void setup();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include(shiftregister.cmake)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set(DRIVER_NAME shiftregister)
2+
add_library(${DRIVER_NAME} INTERFACE)
3+
4+
target_sources(${DRIVER_NAME} INTERFACE
5+
${CMAKE_CURRENT_LIST_DIR}/${DRIVER_NAME}.cpp)
6+
7+
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
8+
9+
# Pull in pico libraries that we need
10+
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "shiftregister.hpp"
2+
3+
namespace pimoroni {
4+
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include "pico/stdlib.h"
4+
#include "hardware/spi.h"
5+
#include "hardware/gpio.h"
6+
7+
namespace pimoroni {
8+
template<typename T> class ShiftRegister {
9+
private:
10+
uint CLOCK = 0;
11+
uint LATCH = 0;
12+
uint DATA = 0;
13+
14+
public:
15+
ShiftRegister(uint clock, uint latch, uint data) :
16+
CLOCK(clock),
17+
LATCH(latch),
18+
DATA(data) {
19+
gpio_init(CLOCK);
20+
gpio_set_function(CLOCK, GPIO_FUNC_SIO);
21+
gpio_set_dir(CLOCK, GPIO_OUT);
22+
23+
gpio_init(LATCH);
24+
gpio_set_function(LATCH, GPIO_FUNC_SIO);
25+
gpio_set_dir(LATCH, GPIO_OUT);
26+
27+
gpio_init(DATA);
28+
gpio_set_function(DATA, GPIO_FUNC_SIO);
29+
gpio_set_dir(DATA, GPIO_IN);
30+
}
31+
T read() {
32+
gpio_put(LATCH, 0);
33+
__asm("NOP;");
34+
gpio_put(LATCH, 1);
35+
__asm("NOP;");
36+
T out = 0;
37+
for (auto i = 0u; i < sizeof(T) * 8; i++) {
38+
out <<= 1;
39+
out |= gpio_get(DATA);
40+
gpio_put(CLOCK, 1);
41+
__asm("NOP;");
42+
gpio_put(CLOCK, 0);
43+
__asm("NOP;");
44+
}
45+
return out;
46+
}
47+
};
48+
}

examples/inky_frame/inky_frame7_test.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,43 @@
77

88
using namespace pimoroni;
99

10+
uint LED_PIN = 8;
11+
1012
int main() {
1113
stdio_init_all();
1214

15+
gpio_init(LED_PIN);
16+
gpio_set_function(LED_PIN, GPIO_FUNC_SIO);
17+
gpio_set_dir(LED_PIN, GPIO_OUT);
18+
1319
PSRamDisplay ramDisplay(800, 480);
1420
PicoGraphics_PenInky7 graphics(800, 480, ramDisplay);
15-
UC8159Inky7 uc8159(800,400);
16-
17-
graphics.set_pen(1);
18-
graphics.clear();
19-
20-
for(int i =0 ; i < 100 ; i++)
21-
{
22-
uint size = 25 + (rand() % 50);
23-
uint x = rand() % graphics.bounds.w;
24-
uint y = rand() % graphics.bounds.h;
25-
26-
graphics.set_pen(0);
27-
graphics.circle(Point(x, y), size);
28-
29-
graphics.set_pen(2+(i%5));
30-
graphics.circle(Point(x, y), size-2);
31-
}
21+
UC8159Inky7 inky7(800,400);
22+
23+
while (true) {
24+
while(!inky7.is_pressed(UC8159Inky7::BUTTON_A)) {
25+
sleep_ms(10);
26+
}
27+
graphics.set_pen(1);
28+
graphics.clear();
29+
30+
for(int i =0 ; i < 100 ; i++)
31+
{
32+
uint size = 25 + (rand() % 50);
33+
uint x = rand() % graphics.bounds.w;
34+
uint y = rand() % graphics.bounds.h;
35+
36+
graphics.set_pen(0);
37+
graphics.circle(Point(x, y), size);
38+
39+
graphics.set_pen(2+(i%5));
40+
graphics.circle(Point(x, y), size-2);
41+
}
3242

33-
uc8159.update(&graphics);
34-
35-
while(true)
36-
sleep_ms(1000);
43+
gpio_put(LED_PIN, 1);
44+
inky7.update(&graphics);
45+
gpio_put(LED_PIN, 0);
46+
}
3747

3848
return 0;
3949
}

0 commit comments

Comments
 (0)