| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # (C) Paul Evans, 2015-2026 -- leonerd@leonerd.org.uk | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 5 | ▶ | use v5.26; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 5 | ▶ | use Object::Pad 0.800 ':experimental(adjust_params)'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | package Device::Chip::SSD1306 0.16; | ||||||||||||||
| 11 | class Device::Chip::SSD1306 | ||||||||||||||
| 12 | :isa(Device::Chip) | ||||||||||||||
| 13 | 3 | ▶ | :strict(params); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 5 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 5 | require XSLoader; | |||||||||||||
| 19 | 5 | XSLoader::load( __PACKAGE__, our $VERSION ); | |||||||||||||
| 20 | |||||||||||||||
| 21 | =encoding UTF-8 | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 NAME | ||||||||||||||
| 24 | |||||||||||||||
| 25 | C<Device::Chip::SSD1306> - chip driver for monochrome OLED modules | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||
| 28 | |||||||||||||||
| 29 | =for highlighter language=perl | ||||||||||||||
| 30 | |||||||||||||||
| 31 | This abstract L<Device::Chip> subclass provides communication to an | ||||||||||||||
| 32 | F<SSD1306>, F<SSD1309> or F<SH1106> chip attached by an adapter. To actually | ||||||||||||||
| 33 | use it, you should use one of the subclasses for the various interface types. | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =over 4 | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =item * | ||||||||||||||
| 38 | |||||||||||||||
| 39 | L<Device::Chip::SSD1306::I2C> - I²C | ||||||||||||||
| 40 | |||||||||||||||
| 41 | =item * | ||||||||||||||
| 42 | |||||||||||||||
| 43 | L<Device::Chip::SSD1306::SPI4> - 4-wire SPI | ||||||||||||||
| 44 | |||||||||||||||
| 45 | =back | ||||||||||||||
| 46 | |||||||||||||||
| 47 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||||
| 48 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||||
| 49 | concepts or features, only the use of this module to access them. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =cut | ||||||||||||||
| 52 | |||||||||||||||
| 53 | =head1 DEVICE MODELS | ||||||||||||||
| 54 | |||||||||||||||
| 55 | This module supports a variety of actual chip modules with different display | ||||||||||||||
| 56 | sizes. The specific display module is selected by the C<model> argument to the | ||||||||||||||
| 57 | constructor, which should take one of the following values: | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =over 4 | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =item C<SSD1306-128x64> | ||||||||||||||
| 62 | |||||||||||||||
| 63 | An F<SSD1306> driving a display with 128 columns and 64 rows. The most common | ||||||||||||||
| 64 | of the display modules, often found with a 0.96 inch display. | ||||||||||||||
| 65 | |||||||||||||||
| 66 | This setting also drives the F<SSD1309> chip found in the larger 1.6 or 2.4 | ||||||||||||||
| 67 | inch displays. | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =item C<SSD1306-128x32> | ||||||||||||||
| 70 | |||||||||||||||
| 71 | An F<SSD1306> driving a display with 128 columns and 32 rows. This is the | ||||||||||||||
| 72 | usual "half-height" module, often found with a 0.91 inch display. This uses | ||||||||||||||
| 73 | only even-numbered rows. | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =item C<SSD1306-64x32> | ||||||||||||||
| 76 | |||||||||||||||
| 77 | An F<SSD1306> driving a display with 64 columns and 32 rows. This is the usual | ||||||||||||||
| 78 | "quarter" size module, often found with a 0.49 inch display. This uses the | ||||||||||||||
| 79 | only the middle 64 columns. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =item C<SH1106-128x64> | ||||||||||||||
| 82 | |||||||||||||||
| 83 | An F<SH1106> driving a display with 128 columns and 64 rows. This is the chip | ||||||||||||||
| 84 | that's usually found in the larger display modules, such as 1.3 and 1.6 inch. | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =back | ||||||||||||||
| 87 | |||||||||||||||
| 88 | The overall API shape of this module is similar to that of | ||||||||||||||
| 89 | L<Device::Chip::MAX7219Panel>, supporting the same set of drawing methods. A | ||||||||||||||
| 90 | future version of both of these modules may extend the concept into providing | ||||||||||||||
| 91 | access via an interface helper instance, if some standard API shape is defined | ||||||||||||||
| 92 | for driving these kinds of 1bpp pixel displays. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =cut | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 5 | my %MODELS = ( | |||||||||||||
| 97 | "SSD1306-128x64" => { | ||||||||||||||
| 98 | columns => 128, | ||||||||||||||
| 99 | rows => 64, | ||||||||||||||
| 100 | |||||||||||||||
| 101 | set_com_pins_arg => 0x12, | ||||||||||||||
| 102 | column_offset => 0, | ||||||||||||||
| 103 | }, | ||||||||||||||
| 104 | "SSD1306-128x32" => { | ||||||||||||||
| 105 | columns => 128, | ||||||||||||||
| 106 | rows => 32, | ||||||||||||||
| 107 | |||||||||||||||
| 108 | set_com_pins_arg => 0x02, | ||||||||||||||
| 109 | column_offset => 0, | ||||||||||||||
| 110 | }, | ||||||||||||||
| 111 | "SSD1306-64x32" => { | ||||||||||||||
| 112 | columns => 64, | ||||||||||||||
| 113 | rows => 32, | ||||||||||||||
| 114 | |||||||||||||||
| 115 | set_com_pins_arg => 0x12, | ||||||||||||||
| 116 | # This module seems to use the middle 64 column pins | ||||||||||||||
| 117 | column_offset => 32, | ||||||||||||||
| 118 | }, | ||||||||||||||
| 119 | "SH1106-128x64" => { | ||||||||||||||
| 120 | columns => 128, | ||||||||||||||
| 121 | rows => 64, | ||||||||||||||
| 122 | |||||||||||||||
| 123 | set_com_pins_arg => 0x12, | ||||||||||||||
| 124 | # SH1106 has a 128 column physical display but its VRAM is 132 columns | ||||||||||||||
| 125 | # wide; with two blank columns either side of the display. It actually | ||||||||||||||
| 126 | # starts outputting from column 2; | ||||||||||||||
| 127 | column_offset => 2, | ||||||||||||||
| 128 | }, | ||||||||||||||
| 129 | ); | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 CONSTRUCTOR | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =cut | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head2 new | ||||||||||||||
| 136 | |||||||||||||||
| 137 | $chip = Device::Chip::SSD1306->new( | ||||||||||||||
| 138 | model => $model, | ||||||||||||||
| 139 | ... | ||||||||||||||
| 140 | ); | ||||||||||||||
| 141 | |||||||||||||||
| 142 | Returns a new C<Device::Chip::SSD1306> driver instance for the given model | ||||||||||||||
| 143 | name, which must be one of the models listed in L</DEVICE MODELS>. If no model | ||||||||||||||
| 144 | option is chosen, the default of C<SSD1306-128x64> will apply. | ||||||||||||||
| 145 | |||||||||||||||
| 146 | In addition to C<model>, the following named options may also be passed: | ||||||||||||||
| 147 | |||||||||||||||
| 148 | =over 4 | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =item xflip => BOOL | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =item yflip => BOOL | ||||||||||||||
| 153 | |||||||||||||||
| 154 | If true, the order of columns or rows respectively will be reversed by the | ||||||||||||||
| 155 | hardware. In particular, if both are true, this inverts the orientation of | ||||||||||||||
| 156 | the display, if it is mounted upside-down. | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =item contrast => INT | ||||||||||||||
| 159 | |||||||||||||||
| 160 | Normally set to 0x9F, but you may wish to set a lower value to avoid display | ||||||||||||||
| 161 | burn-in on rarely-updated static displays. | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =item vcomh => INT | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Normally set to 4, but you may wish to set a lower value to avoid display | ||||||||||||||
| 166 | burn-in on rarely-updated static displays. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =back | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =cut | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 5 | ▶ | field $_rows :reader; | ||||||||||||
|
Subroutine
rows: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 173 | 4 | ▶ | field $_columns :reader; | ||||||||||||
|
Subroutine
columns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 174 | |||||||||||||||
| 175 | 5 | field $_column_offset; | |||||||||||||
| 176 | 5 | field $_set_com_pins_arg; | |||||||||||||
| 177 | field $_xflip :param = 0; | ||||||||||||||
| 178 | 5 | field $_yflip :param = 0; | |||||||||||||
| 179 | |||||||||||||||
| 180 | 5 | field $_contrast :param = 0x9F; | |||||||||||||
| 181 | 5 | field $_vcomh :param = 4; | |||||||||||||
| 182 | |||||||||||||||
| 183 | 5 | ADJUST :params ( :$model = "SSD1306-128x64" ) | |||||||||||||
| 184 | { | ||||||||||||||
| 185 | my $modelargs = $MODELS{$model} or croak "Unrecognised model $model"; | ||||||||||||||
| 186 | |||||||||||||||
| 187 | ( $_rows, $_columns, $_column_offset, $_set_com_pins_arg ) = | ||||||||||||||
| 188 | @{$modelargs}{qw( rows columns column_offset set_com_pins_arg )}; | ||||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 5 | =head1 METHODS | |||||||||||||
| 192 | |||||||||||||||
| 193 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||||
| 194 | instances. | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =cut | ||||||||||||||
| 197 | |||||||||||||||
| 198 | =head2 rows | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =head2 columns | ||||||||||||||
| 201 | |||||||||||||||
| 202 | $n = $chip->rows; | ||||||||||||||
| 203 | |||||||||||||||
| 204 | $n = $chip->columns; | ||||||||||||||
| 205 | |||||||||||||||
| 206 | Simple accessors that return the number of rows or columns present on the | ||||||||||||||
| 207 | physical display. | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =cut | ||||||||||||||
| 210 | |||||||||||||||
| 211 | 5 | ▶ | use constant { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 212 | 5 | CMD_SET_CONTRAST => 0x81, # , contrast | |||||||||||||
| 213 | CMD_DISPLAY_LAMPTEST => 0xA4, # + all on | ||||||||||||||
| 214 | CMD_DISPLAY_INVERT => 0xA6, # + invert | ||||||||||||||
| 215 | CMD_DISPLAY_OFF => 0xAE, # | ||||||||||||||
| 216 | CMD_DISPLAY_ON => 0xAF, # | ||||||||||||||
| 217 | |||||||||||||||
| 218 | CMD_SCROLL_RIGHT => 0x26, # , 0, start page, time, end page, 0, 0xff | ||||||||||||||
| 219 | CMD_SCROLL_LEFT => 0x27, # , 0, start page, time, end page, 0, 0xff | ||||||||||||||
| 220 | CMD_SCROLL_VERT_RIGHT => 0x29, # , 0, start page, time, end page, vertical | ||||||||||||||
| 221 | CMD_SCROLL_VERT_LEFT => 0x2A, # , 0, start page, time, end page, vertical | ||||||||||||||
| 222 | CMD_SCROLL_DEACTIVATE => 0x2E, # | ||||||||||||||
| 223 | CMD_SCROLL_ACTIVATE => 0x2F, # | ||||||||||||||
| 224 | CMD_SET_SCROLL_AREA => 0xA3, # , start row, scroll rows | ||||||||||||||
| 225 | |||||||||||||||
| 226 | CMD_SET_LOW_COLUMN => 0x00, # + column | ||||||||||||||
| 227 | CMD_SET_HIGH_COLUMN => 0x10, # + column | ||||||||||||||
| 228 | CMD_SET_ADDR_MODE => 0x20, # , mode | ||||||||||||||
| 229 | MODE_HORIZONTAL => 0, | ||||||||||||||
| 230 | MODE_VERTICAL => 1, | ||||||||||||||
| 231 | MODE_PAGE => 2, | ||||||||||||||
| 232 | CMD_SET_COLUMN_ADDR => 0x21, # , start column, end column | ||||||||||||||
| 233 | CMD_SET_PAGE_ADDR => 0x22, # , start page, end page | ||||||||||||||
| 234 | CMD_SET_PAGE_START => 0xB0, # + page | ||||||||||||||
| 235 | |||||||||||||||
| 236 | CMD_SET_DISPLAY_START => 0x40, # + line | ||||||||||||||
| 237 | CMD_SET_SEGMENT_REMAP => 0xA0, # + remap | ||||||||||||||
| 238 | CMD_SET_MUX_RATIO => 0xA8, # , mux | ||||||||||||||
| 239 | CMD_SET_COM_SCAN_DIR => 0xC0, # + direction(8) | ||||||||||||||
| 240 | CMD_SET_DISPLAY_OFFS => 0xD3, # , line offset | ||||||||||||||
| 241 | CMD_SET_COM_PINS => 0xDA, # , (config << 4) | 0x02 | ||||||||||||||
| 242 | |||||||||||||||
| 243 | CMD_SET_CLOCKDIV => 0xD5, # , (freq << 4) | (divratio) | ||||||||||||||
| 244 | CMD_SET_PRECHARGE => 0xD9, # , (ph1 << 4) | (ph2) | ||||||||||||||
| 245 | CMD_SET_VCOMH_LEVEL => 0xDB, # , (level << 4) | ||||||||||||||
| 246 | CMD_SET_CHARGEPUMP => 0x8D, # , 0x10 | (enable << 2) | ||||||||||||||
| 247 | |||||||||||||||
| 248 | CMD_NOP => 0xE3, | ||||||||||||||
| 249 | 5 | }; | |||||||||||||
| 250 | |||||||||||||||
| 251 | =head2 init | ||||||||||||||
| 252 | |||||||||||||||
| 253 | await $chip->init; | ||||||||||||||
| 254 | |||||||||||||||
| 255 | Initialise the display after reset to some sensible defaults. | ||||||||||||||
| 256 | |||||||||||||||
| 257 | =cut | ||||||||||||||
| 258 | |||||||||||||||
| 259 | # This initialisation sequence is inspired by the Adafruit driver | ||||||||||||||
| 260 | # https://github.com/adafruit/Adafruit_SSD1306 | ||||||||||||||
| 261 | |||||||||||||||
| 262 | 0 | ▶ | async method init () | ||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 263 | 0 | { | |||||||||||||
| 264 | 0 | await $self->display( 0 ); | |||||||||||||
| 265 | 0 | await $self->send_cmd( CMD_SET_CLOCKDIV, ( 8 << 4 ) | 0x80 ); | |||||||||||||
| 266 | 0 | await $self->send_cmd( CMD_SET_MUX_RATIO, $_rows - 1 ); | |||||||||||||
| 267 | 0 | await $self->send_cmd( CMD_SET_DISPLAY_OFFS, 0 ); | |||||||||||||
| 268 | 0 | await $self->send_cmd( CMD_SET_DISPLAY_START | 0 ); | |||||||||||||
| 269 | 0 | await $self->send_cmd( CMD_SET_CHARGEPUMP, 0x14 ); | |||||||||||||
| 270 | 0 | await $self->send_cmd( CMD_SET_ADDR_MODE, MODE_HORIZONTAL ); | |||||||||||||
| 271 | 0 | ▶ | await $self->send_cmd( CMD_SET_SEGMENT_REMAP | ( $_xflip ? 1 : 0 ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 272 | 0 | ▶ | await $self->send_cmd( CMD_SET_COM_SCAN_DIR | ( $_yflip ? 1<<3 : 0 ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 273 | 0 | await $self->send_cmd( CMD_SET_COM_PINS, $_set_com_pins_arg ); | |||||||||||||
| 274 | 0 | await $self->send_cmd( CMD_SET_CONTRAST, $_contrast ); | |||||||||||||
| 275 | 0 | await $self->send_cmd( CMD_SET_PRECHARGE, ( 0x0f << 4 ) | ( 1 ) ); | |||||||||||||
| 276 | 0 | await $self->send_cmd( CMD_SET_VCOMH_LEVEL, ( $_vcomh << 4 ) ); | |||||||||||||
| 277 | 5 | } | |||||||||||||
| 278 | |||||||||||||||
| 279 | =head2 display | ||||||||||||||
| 280 | |||||||||||||||
| 281 | await $chip->display( $on ); | ||||||||||||||
| 282 | |||||||||||||||
| 283 | Turn on or off the display. | ||||||||||||||
| 284 | |||||||||||||||
| 285 | =cut | ||||||||||||||
| 286 | |||||||||||||||
| 287 | 0 | ▶ | async method display ( $on ) | ||||||||||||
|
Subroutine
display: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 288 | 0 | { | |||||||||||||
| 289 | 0 | ▶ | await $self->send_cmd( $on ? CMD_DISPLAY_ON : CMD_DISPLAY_OFF ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 290 | 5 | } | |||||||||||||
| 291 | |||||||||||||||
| 292 | =head2 display_lamptest | ||||||||||||||
| 293 | |||||||||||||||
| 294 | await $chip->display_lamptest( $enable ); | ||||||||||||||
| 295 | |||||||||||||||
| 296 | Turn on or off the all-pixels-lit lamptest mode. | ||||||||||||||
| 297 | |||||||||||||||
| 298 | =cut | ||||||||||||||
| 299 | |||||||||||||||
| 300 | 0 | ▶ | async method display_lamptest ( $enable ) | ||||||||||||
|
Subroutine
display_lamptest: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 301 | 0 | { | |||||||||||||
| 302 | 0 | await $self->send_cmd( CMD_DISPLAY_LAMPTEST + !!$enable ); | |||||||||||||
| 303 | 5 | } | |||||||||||||
| 304 | |||||||||||||||
| 305 | =head2 display_invert | ||||||||||||||
| 306 | |||||||||||||||
| 307 | await $chip->display_invert( $enable ); | ||||||||||||||
| 308 | |||||||||||||||
| 309 | Turn on or off the inverted output mode. | ||||||||||||||
| 310 | |||||||||||||||
| 311 | =cut | ||||||||||||||
| 312 | |||||||||||||||
| 313 | 0 | ▶ | async method display_invert ( $enable ) | ||||||||||||
|
Subroutine
display_invert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 314 | 0 | { | |||||||||||||
| 315 | 0 | await $self->send_cmd( CMD_DISPLAY_INVERT + !!$enable ); | |||||||||||||
| 316 | 5 | } | |||||||||||||
| 317 | |||||||||||||||
| 318 | =head2 send_display | ||||||||||||||
| 319 | |||||||||||||||
| 320 | await $chip->send_display( $pixels ); | ||||||||||||||
| 321 | |||||||||||||||
| 322 | Sends an entire screen-worth of pixel data. The C<$pixels> should be in a | ||||||||||||||
| 323 | packed binary string containing one byte per 8 pixels. | ||||||||||||||
| 324 | |||||||||||||||
| 325 | =cut | ||||||||||||||
| 326 | |||||||||||||||
| 327 | 4 | ▶ | async method send_display ( $pixels ) | ||||||||||||
|
Subroutine
send_display: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 328 | 4 | { | |||||||||||||
| 329 | # This output method isn't quite what most of the SSD1306 drivers use, but | ||||||||||||||
| 330 | # it happens to work on both the SSD1306 and the SH1106, whereas other code | ||||||||||||||
| 331 | # based on SET_COLUMN_ADDR + SET_PAGE_ADDR do not | ||||||||||||||
| 332 | |||||||||||||||
| 333 | 4 | my $pagewidth = $_columns; | |||||||||||||
| 334 | |||||||||||||||
| 335 | 4 | my $column = $_column_offset; | |||||||||||||
| 336 | |||||||||||||||
| 337 | 4 | foreach my $page ( 0 .. ( $_rows / 8 ) - 1 ) { | |||||||||||||
| 338 | 24 | await $self->send_cmd( CMD_SET_PAGE_START + $page ); | |||||||||||||
| 339 | 24 | await $self->send_cmd( CMD_SET_LOW_COLUMN | $column & 0x0f ); | |||||||||||||
| 340 | 24 | await $self->send_cmd( CMD_SET_HIGH_COLUMN | $column >> 4 ); | |||||||||||||
| 341 | 24 | await $self->send_data( substr $pixels, $page * $pagewidth, $pagewidth ); | |||||||||||||
| 342 | } | ||||||||||||||
| 343 | 5 | } | |||||||||||||
| 344 | |||||||||||||||
| 345 | =head1 DRAWING METHODS | ||||||||||||||
| 346 | |||||||||||||||
| 347 | The following methods operate on an internal framebuffer stored by the | ||||||||||||||
| 348 | instance. The user must invoke the L</refresh> method to update the actual | ||||||||||||||
| 349 | chip after calling them. | ||||||||||||||
| 350 | |||||||||||||||
| 351 | =cut | ||||||||||||||
| 352 | |||||||||||||||
| 353 | # The internal framebuffer is now implemented by XS code, we just need an SV | ||||||||||||||
| 354 | # to attach it to | ||||||||||||||
| 355 | 5 | field $_framebuffer; | |||||||||||||
| 356 | method DESTROY | ||||||||||||||
| 357 | 8 | ▶ | { | ||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||
| 358 | 8 | ▶ | $_framebuffer and _framebuffer_free( $_framebuffer ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 359 | 5 | } | |||||||||||||
| 360 | |||||||||||||||
| 361 | =head2 clear | ||||||||||||||
| 362 | |||||||||||||||
| 363 | $chip->clear; | ||||||||||||||
| 364 | |||||||||||||||
| 365 | Resets the stored framebuffer to blank. | ||||||||||||||
| 366 | |||||||||||||||
| 367 | =cut | ||||||||||||||
| 368 | |||||||||||||||
| 369 | 2 | ▶ | method clear () | ||||||||||||
|
Subroutine
clear: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 370 | { | ||||||||||||||
| 371 | 2 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 372 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 373 | |||||||||||||||
| 374 | 2 | _framebuffer_clear( $_framebuffer ); | |||||||||||||
| 375 | 5 | } | |||||||||||||
| 376 | |||||||||||||||
| 377 | =head2 draw_pixel | ||||||||||||||
| 378 | |||||||||||||||
| 379 | $chip->draw_pixel( $x, $y, $val = 1 ); | ||||||||||||||
| 380 | |||||||||||||||
| 381 | Draw the given pixel. If the third argument is false, the pixel will be | ||||||||||||||
| 382 | cleared instead of set. | ||||||||||||||
| 383 | |||||||||||||||
| 384 | =cut | ||||||||||||||
| 385 | |||||||||||||||
| 386 | 1 | ▶ | method draw_pixel ( $x, $y, $val = 1 ) | ||||||||||||
|
Subroutine
draw_pixel: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 387 | { | ||||||||||||||
| 388 | 1 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 389 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 390 | |||||||||||||||
| 391 | 1 | _framebuffer_draw_pixel( $_framebuffer, $x, $y, $val ); | |||||||||||||
| 392 | 5 | } | |||||||||||||
| 393 | |||||||||||||||
| 394 | =head2 draw_hline | ||||||||||||||
| 395 | |||||||||||||||
| 396 | $chip->draw_hline( $x1, $x2, $y, $val = 1 ); | ||||||||||||||
| 397 | |||||||||||||||
| 398 | Draw a horizontal line in the given I<$y> row, between the columns I<$x1> and | ||||||||||||||
| 399 | I<$x2> (inclusive). If the fourth argument is false, the pixels will be | ||||||||||||||
| 400 | cleared instead of set. | ||||||||||||||
| 401 | |||||||||||||||
| 402 | =cut | ||||||||||||||
| 403 | |||||||||||||||
| 404 | 1 | ▶ | method draw_hline ( $x1, $x2, $y, $val = 1 ) | ||||||||||||
|
Subroutine
draw_hline: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 405 | { | ||||||||||||||
| 406 | 1 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 407 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 408 | |||||||||||||||
| 409 | 1 | _framebuffer_draw_hline( $_framebuffer, $x1, $x2, $y, $val ); | |||||||||||||
| 410 | 5 | } | |||||||||||||
| 411 | |||||||||||||||
| 412 | =head2 draw_vline | ||||||||||||||
| 413 | |||||||||||||||
| 414 | $chip->draw_vline( $x, $y1, $y2, $val = 1 ); | ||||||||||||||
| 415 | |||||||||||||||
| 416 | Draw a vertical line in the given I<$x> column, between the rows I<$y1> and | ||||||||||||||
| 417 | I<$y2> (inclusive). If the fourth argument is false, the pixels will be | ||||||||||||||
| 418 | cleared instead of set. | ||||||||||||||
| 419 | |||||||||||||||
| 420 | =cut | ||||||||||||||
| 421 | |||||||||||||||
| 422 | 2 | ▶ | method draw_vline ( $x, $y1, $y2, $val = 1 ) | ||||||||||||
|
Subroutine
draw_vline: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 423 | { | ||||||||||||||
| 424 | 2 | ▶ | $val //= 1; | ||||||||||||
|
Condition50%1/2
$val //= 1
MC/DC0%0/1
$val //= 1
$val
Truth table50%1/2
A
A= $val
| |||||||||||||||
| 425 | |||||||||||||||
| 426 | 2 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 427 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 428 | |||||||||||||||
| 429 | 2 | _framebuffer_draw_vline( $_framebuffer, $x, $y1, $y2, $val ); | |||||||||||||
| 430 | 5 | } | |||||||||||||
| 431 | |||||||||||||||
| 432 | =head2 draw_rect | ||||||||||||||
| 433 | |||||||||||||||
| 434 | $chip->draw_rect( $x1, $y1, $x2, $y2, $val = 1 ); | ||||||||||||||
| 435 | |||||||||||||||
| 436 | I<Since version 0.15.> | ||||||||||||||
| 437 | |||||||||||||||
| 438 | Draw a solid rectangle in between the given I<$x1> to I<$2> columns and rows | ||||||||||||||
| 439 | I<$y1> to I<$y2> (all inclusive). If the final argument is false, the pixels | ||||||||||||||
| 440 | will be cleared instead of set. | ||||||||||||||
| 441 | |||||||||||||||
| 442 | =cut | ||||||||||||||
| 443 | |||||||||||||||
| 444 | 1 | ▶ | method draw_rect ( $x1, $y1, $x2, $y2, $val = 1 ) | ||||||||||||
|
Subroutine
draw_rect: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 445 | { | ||||||||||||||
| 446 | 1 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 447 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 448 | |||||||||||||||
| 449 | 1 | _framebuffer_draw_rect( $_framebuffer, $x1, $y1, $x2, $y2, $val ); | |||||||||||||
| 450 | 5 | } | |||||||||||||
| 451 | |||||||||||||||
| 452 | =head2 draw_blit | ||||||||||||||
| 453 | |||||||||||||||
| 454 | $chip->draw_blit( $x, $y, @lines ); | ||||||||||||||
| 455 | |||||||||||||||
| 456 | Draws a bitmap pattern by copying the data given in lines, starting at the | ||||||||||||||
| 457 | given position. | ||||||||||||||
| 458 | |||||||||||||||
| 459 | Each value in C<@lines> should be a string giving a horizontal line of bitmap | ||||||||||||||
| 460 | data, each character corresponding to a single pixel of the display. Pixels | ||||||||||||||
| 461 | corresponding to a spaces will be left alone, a hyphen will be cleared, and | ||||||||||||||
| 462 | any other character (for example a C<#>) will be set. | ||||||||||||||
| 463 | |||||||||||||||
| 464 | For example, to draw an rightward-pointing arrow: | ||||||||||||||
| 465 | |||||||||||||||
| 466 | $chip->draw_blit( 20, 40, | ||||||||||||||
| 467 | " # ", | ||||||||||||||
| 468 | " ## ", | ||||||||||||||
| 469 | "######", | ||||||||||||||
| 470 | "######", | ||||||||||||||
| 471 | " ## ", | ||||||||||||||
| 472 | " # " ); | ||||||||||||||
| 473 | |||||||||||||||
| 474 | =cut | ||||||||||||||
| 475 | |||||||||||||||
| 476 | 1 | ▶ | method draw_blit ( $x0, $y, @lines ) | ||||||||||||
|
Subroutine
draw_blit: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||
| 477 | { | ||||||||||||||
| 478 | 1 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 479 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 480 | |||||||||||||||
| 481 | # TODO: more efficient in XS, somehow...? | ||||||||||||||
| 482 | 1 | ▶ | for( ; @lines; $y++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 483 | 8 | my @pixels = split m//, shift @lines; | |||||||||||||
| 484 | 8 | ▶ | @pixels or next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 485 | |||||||||||||||
| 486 | 8 | my $x = $x0; | |||||||||||||
| 487 | 8 | ▶ | for( ; @pixels; $x++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 488 | 32 | my $p = shift @pixels; | |||||||||||||
| 489 | |||||||||||||||
| 490 | 32 | ▶ | $p eq " " ? next : | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 491 | $p eq "-" ? _framebuffer_draw_pixel( $_framebuffer, $x, $y, 0 ) : | ||||||||||||||
| 492 | _framebuffer_draw_pixel( $_framebuffer, $x, $y, 1 ); | ||||||||||||||
| 493 | } | ||||||||||||||
| 494 | } | ||||||||||||||
| 495 | 5 | } | |||||||||||||
| 496 | |||||||||||||||
| 497 | =head2 draw_glyph | ||||||||||||||
| 498 | |||||||||||||||
| 499 | $chip->draw_glyph( $x, $y, $glyph, $val = 1, $bg = undef ); | ||||||||||||||
| 500 | |||||||||||||||
| 501 | I<Since version 0.15.> | ||||||||||||||
| 502 | |||||||||||||||
| 503 | Draws a glyph by copying the bitmap data out of the C<$glyph> argument, as if | ||||||||||||||
| 504 | it were a single glyph structure obtained from L<Font::PCF>. The bitmap is | ||||||||||||||
| 505 | copied starting at the given position, and extends rightward and downward for | ||||||||||||||
| 506 | whatever is the defined size of the glyph given by its data. For each pixel | ||||||||||||||
| 507 | set in the glyph's bitmap, the corresponding pixel of the display is set to | ||||||||||||||
| 508 | C<$val>; the other pixels are set to C<$bg>. Either of these last two values | ||||||||||||||
| 509 | may be set to C<undef> to not modify those corresponding pixels. | ||||||||||||||
| 510 | |||||||||||||||
| 511 | Note that neither this module nor its tests actually depend on C<Font::PCF> | ||||||||||||||
| 512 | itself; it merely invokes methods on the object consistent with that API. | ||||||||||||||
| 513 | Specifically, it expects the following methods: | ||||||||||||||
| 514 | |||||||||||||||
| 515 | =over 4 | ||||||||||||||
| 516 | |||||||||||||||
| 517 | =item width | ||||||||||||||
| 518 | |||||||||||||||
| 519 | $cols = $glyph->width; | ||||||||||||||
| 520 | |||||||||||||||
| 521 | Returns the number of columns wide that contain pixels of glyph data. | ||||||||||||||
| 522 | |||||||||||||||
| 523 | =item bitmap | ||||||||||||||
| 524 | |||||||||||||||
| 525 | [ $row0, $row1, ... ] = $glyph->bitmap; | ||||||||||||||
| 526 | |||||||||||||||
| 527 | Returns an C<ARRAY> reference of rows of pixel data. Each row is represented | ||||||||||||||
| 528 | by a big-endian 32bit integer. The topmost bit corresponds to the leftmost | ||||||||||||||
| 529 | pixel. | ||||||||||||||
| 530 | |||||||||||||||
| 531 | =back | ||||||||||||||
| 532 | |||||||||||||||
| 533 | =cut | ||||||||||||||
| 534 | |||||||||||||||
| 535 | 4 | ▶ | method draw_glyph ( $x0, $y0, $glyph, $val = 1, $bg = undef ) | ||||||||||||
|
Subroutine
draw_glyph: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 536 | { | ||||||||||||||
| 537 | 4 | my $bitmap = $glyph->bitmap; | |||||||||||||
| 538 | 4 | my $width_1 = $glyph->width - 1; | |||||||||||||
| 539 | |||||||||||||||
| 540 | 4 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 541 | _framebuffer_new( $_framebuffer, $_rows, $_columns ); | ||||||||||||||
| 542 | |||||||||||||||
| 543 | # TODO: more efficient in XS, somehow...? | ||||||||||||||
| 544 | 4 | foreach my $row ( 0 .. $#$bitmap ) { | |||||||||||||
| 545 | 32 | my $pixels = $bitmap->[$row]; | |||||||||||||
| 546 | 32 | my $y = $y0 + $row; | |||||||||||||
| 547 | |||||||||||||||
| 548 | 32 | foreach my $col ( 0 .. $width_1 ) { | |||||||||||||
| 549 | # $pixels is 32bit big-endian | ||||||||||||||
| 550 | 256 | ▶ | my $update = ( $pixels & (1<<(31-$col)) ) ? $val : $bg; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 551 | 256 | ▶ | _framebuffer_draw_pixel( $_framebuffer, $x0+$col, $y, $update ) if defined $update; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 552 | } | ||||||||||||||
| 553 | } | ||||||||||||||
| 554 | 5 | } | |||||||||||||
| 555 | |||||||||||||||
| 556 | =head2 refresh | ||||||||||||||
| 557 | |||||||||||||||
| 558 | await $chip->refresh; | ||||||||||||||
| 559 | |||||||||||||||
| 560 | Sends the framebuffer to the display chip. | ||||||||||||||
| 561 | |||||||||||||||
| 562 | =cut | ||||||||||||||
| 563 | |||||||||||||||
| 564 | 4 | ▶ | async method refresh () | ||||||||||||
|
Subroutine
refresh: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 565 | 4 | { | |||||||||||||
| 566 | 4 | ▶ | $_framebuffer or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 567 | return; # nothing to do | ||||||||||||||
| 568 | |||||||||||||||
| 569 | 4 | my $maxcol = $_columns - 1; | |||||||||||||
| 570 | 4 | my ( $dirty_xlo, $dirty_xhi ) = _framebuffer_dirty_xlohi( $_framebuffer ); | |||||||||||||
| 571 | 4 | my $column = $_column_offset + $dirty_xlo; | |||||||||||||
| 572 | |||||||||||||||
| 573 | 4 | foreach my $page ( 0 .. ( $_rows / 8 ) - 1 ) { | |||||||||||||
| 574 | 32 | ▶ | next unless _framebuffer_is_dirty_page( $_framebuffer, $page ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 575 | |||||||||||||||
| 576 | 24 | my $data = _framebuffer_pagedata( $_framebuffer, $page, $dirty_xlo, $dirty_xhi ); | |||||||||||||
| 577 | |||||||||||||||
| 578 | 24 | await $self->send_cmd( CMD_SET_PAGE_START + $page ); | |||||||||||||
| 579 | 24 | await $self->send_cmd( CMD_SET_LOW_COLUMN | $column & 0x0f ); | |||||||||||||
| 580 | 24 | await $self->send_cmd( CMD_SET_HIGH_COLUMN | $column >> 4 ); | |||||||||||||
| 581 | 24 | await $self->send_data( $data ); | |||||||||||||
| 582 | } | ||||||||||||||
| 583 | |||||||||||||||
| 584 | 4 | _framebuffer_cleaned( $_framebuffer ); | |||||||||||||
| 585 | 5 | } | |||||||||||||
| 586 | |||||||||||||||
| 587 | =head1 TODO | ||||||||||||||
| 588 | |||||||||||||||
| 589 | =over 4 | ||||||||||||||
| 590 | |||||||||||||||
| 591 | =item * | ||||||||||||||
| 592 | |||||||||||||||
| 593 | More interfaces - 3-wire SPI | ||||||||||||||
| 594 | |||||||||||||||
| 595 | =back | ||||||||||||||
| 596 | |||||||||||||||
| 597 | =cut | ||||||||||||||
| 598 | |||||||||||||||
| 599 | =head1 AUTHOR | ||||||||||||||
| 600 | |||||||||||||||
| 601 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||
| 602 | |||||||||||||||
| 603 | =cut | ||||||||||||||
| 604 | |||||||||||||||
| 605 | 5 | 0x55AA; | |||||||||||||