Skip to content

Commit 23bd9b0

Browse files
authored
Fix display rotation for different screen sizes (#145)
1 parent fe1ffab commit 23bd9b0

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
## [Unreleased] - ReleaseDate
88

9+
- [#145](https://github.com/jamwaffles/ssd1306/pull/145) Fixed rotation for 96x16 and 72x40 displays.
10+
911
## [0.5.1] - 2021-01-09
1012

1113
## [0.5.0] - 2020-12-21

src/displaysize.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ pub trait DisplaySize {
1818
/// Height in pixels
1919
const HEIGHT: u8;
2020

21+
/// Maximum width supported by the display driver
22+
const DRIVER_COLS: u8 = 128;
23+
24+
/// Maximum height supported by the display driver
25+
const DRIVER_ROWS: u8 = 64;
26+
2127
/// Horizontal offset in pixels
2228
const OFFSETX: u8 = 0;
2329

src/mode/graphics.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,20 @@ where
146146
self.max_y = 0;
147147

148148
// Tell the display to update only the part that has changed
149+
let offset_x = match self.properties.get_rotation() {
150+
DisplayRotation::Rotate0 | DisplayRotation::Rotate270 => DSIZE::OFFSETX,
151+
DisplayRotation::Rotate180 | DisplayRotation::Rotate90 => {
152+
// If segment remapping is flipped, we need to calculate
153+
// the offset from the other edge of the display.
154+
DSIZE::DRIVER_COLS - DSIZE::WIDTH - DSIZE::OFFSETX
155+
}
156+
};
149157
match self.properties.get_rotation() {
150158
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
151159
self.properties.set_draw_area(
152-
(disp_min_x + DSIZE::OFFSETX, disp_min_y + DSIZE::OFFSETY),
153-
(disp_max_x + DSIZE::OFFSETX, disp_max_y + DSIZE::OFFSETY),
160+
(disp_min_x + offset_x, disp_min_y + DSIZE::OFFSETY),
161+
(disp_max_x + offset_x, disp_max_y + DSIZE::OFFSETY),
154162
)?;
155-
156163
self.properties.bounded_draw(
157164
&self.buffer,
158165
width as usize,
@@ -162,10 +169,9 @@ where
162169
}
163170
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
164171
self.properties.set_draw_area(
165-
(disp_min_y + DSIZE::OFFSETY, disp_min_x + DSIZE::OFFSETX),
166-
(disp_max_y + DSIZE::OFFSETY, disp_max_x + DSIZE::OFFSETX),
172+
(disp_min_y + offset_x, disp_min_x + DSIZE::OFFSETY),
173+
(disp_max_y + offset_x, disp_max_x + DSIZE::OFFSETY),
167174
)?;
168-
169175
self.properties.bounded_draw(
170176
&self.buffer,
171177
height as usize,

0 commit comments

Comments
 (0)