Skip to content

Commit 7fe936f

Browse files
authored
Fix display rotation in terminal mode (#147)
1 parent 23bd9b0 commit 7fe936f

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## [Unreleased] - ReleaseDate
88

99
- [#145](https://github.com/jamwaffles/ssd1306/pull/145) Fixed rotation for 96x16 and 72x40 displays.
10+
- [#147](https://github.com/jamwaffles/ssd1306/pull/147) Fixed display rotation in terminal mode.
1011

1112
## [0.5.1] - 2021-01-09
1213

src/mode/terminal.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,18 @@ where
199199
self.properties
200200
.change_mode(AddrMode::Horizontal)
201201
.terminal_err()?;
202+
let offset_x = match self.properties.get_rotation() {
203+
DisplayRotation::Rotate0 | DisplayRotation::Rotate270 => DSIZE::OFFSETX,
204+
DisplayRotation::Rotate180 | DisplayRotation::Rotate90 => {
205+
// If segment remapping is flipped, we need to calculate
206+
// the offset from the other edge of the display.
207+
DSIZE::DRIVER_COLS - DSIZE::WIDTH - DSIZE::OFFSETX
208+
}
209+
};
202210
self.properties
203211
.set_draw_area(
204-
(DSIZE::OFFSETX, DSIZE::OFFSETY),
205-
(
206-
DSIZE::WIDTH + DSIZE::OFFSETX,
207-
DSIZE::HEIGHT + DSIZE::OFFSETY,
208-
),
212+
(offset_x, DSIZE::OFFSETY),
213+
(DSIZE::WIDTH + offset_x, DSIZE::HEIGHT + DSIZE::OFFSETY),
209214
)
210215
.terminal_err()?;
211216

@@ -308,18 +313,26 @@ where
308313
if column >= width || row >= height {
309314
Err(OutOfBounds)
310315
} else {
316+
let offset_x = match self.properties.get_rotation() {
317+
DisplayRotation::Rotate0 | DisplayRotation::Rotate270 => DSIZE::OFFSETX,
318+
DisplayRotation::Rotate180 | DisplayRotation::Rotate90 => {
319+
// If segment remapping is flipped, we need to calculate
320+
// the offset from the other edge of the display.
321+
DSIZE::DRIVER_COLS - DSIZE::WIDTH - DSIZE::OFFSETX
322+
}
323+
};
311324
match self.properties.get_rotation() {
312325
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
313326
self.properties
314-
.set_column(DSIZE::OFFSETX + column * 8)
327+
.set_column(offset_x + column * 8)
315328
.terminal_err()?;
316329
self.properties
317330
.set_row(DSIZE::OFFSETY + row * 8)
318331
.terminal_err()?;
319332
}
320333
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
321334
self.properties
322-
.set_column(DSIZE::OFFSETX + row * 8)
335+
.set_column(offset_x + row * 8)
323336
.terminal_err()?;
324337
self.properties
325338
.set_row(DSIZE::OFFSETY + column * 8)
@@ -334,7 +347,11 @@ where
334347
/// Reset the draw area and move pointer to the top left corner
335348
fn reset_pos(&mut self) -> Result<(), TerminalModeError> {
336349
// Initialise the counter when we know it's valid
337-
self.cursor = Some(Cursor::new(DSIZE::WIDTH, DSIZE::HEIGHT));
350+
let (w, h) = match self.properties.get_rotation() {
351+
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => (DSIZE::WIDTH, DSIZE::HEIGHT),
352+
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => (DSIZE::HEIGHT, DSIZE::WIDTH),
353+
};
354+
self.cursor = Some(Cursor::new(w, h));
338355

339356
// Reset cursor position
340357
self.set_position(0, 0)?;

0 commit comments

Comments
 (0)