Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Commit

Permalink
Refactor LCD even further -
Browse files Browse the repository at this point in the history
- fix hanging test by fixing signed/unsigned types
- get rid of C copypastaz and not needed mut vars
- more refactoring of while loops
  • Loading branch information
errordeveloper committed Jul 8, 2014
1 parent 01181d9 commit 3b083b9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 102 deletions.
40 changes: 13 additions & 27 deletions src/drivers/lcd/c12332.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ might be an issue for any other peripheral sharing the same SPI bus.
use core::cell;
use core::slice::ImmutableVector;
use core::mem::zeroed;
use core::option::{Some, None};
use core::iter::{Iterator, range};

use super::font_small_7;
use super::LCD;
Expand Down Expand Up @@ -129,16 +131,6 @@ impl<'a, S: SPI, T: Timer, P: GPIO> C12332<'a, S, T, P> {
}

pub fn character(&self, x: u32, y: u32, c: u8) {
let hor: u8;
let vert: u8;
let offset: u8;
let bpl: u8;
let mut j: u8;
let mut i: u8;
let mut b: u8;
let zeichen: &[u8];
let mut z: u8;
let w: u8;
let width: u32 = 128;
let height: u32 = 32;

Expand All @@ -147,10 +139,10 @@ impl<'a, S: SPI, T: Timer, P: GPIO> C12332<'a, S, T, P> {
}

// read font parameter from start of array
offset = self.font[0]; // bytes / char
hor = self.font[1]; // get hor size of font
vert = self.font[2]; // get vert size of font
bpl = self.font[3]; // bytes per line
let offset = self.font[0]; // bytes / char
let hor = self.font[1]; // get hor size of font
let vert = self.font[2]; // get vert size of font
let bpl = self.font[3]; // bytes per line

let mut char_x: u32 = self.char_x.get();
let mut char_y: u32 = self.char_y.get();
Expand All @@ -165,24 +157,20 @@ impl<'a, S: SPI, T: Timer, P: GPIO> C12332<'a, S, T, P> {

let start: uint = ((c - 32) as uint * offset as uint) + 4;
let end: uint = start + offset as uint;
zeichen = self.font.slice(start, end);
let zeichen = self.font.slice(start, end);
// zeichen = &self.font[]; // start of char bitmap
w = zeichen[0]; // width of actual char
let w = zeichen[0]; // width of actual char
// construct the char into the buffer
j = 0;
while j < vert {
i = 0;
while i < hor {
z = zeichen[(bpl * i + ((j & 0xF8) >> 3)+1) as uint];
b = 1 << ((j & 0x07) as uint);
for j in range(0, vert) {
for i in range(0, hor) {
let z: u8 = zeichen[(bpl * i + ((j & 0xF8) >> 3)+1) as uint];
let b: u8 = 1 << ((j & 0x07) as uint);
if ( z & b ) == 0x00 {
self.set_pixel(x+i as u32, y+j as u32, 0);
} else {
self.set_pixel(x+i as u32, y+j as u32, 1);
}
i += 1;
}
j += 1;
}

char_x += w as u32;
Expand Down Expand Up @@ -238,10 +226,8 @@ impl<'a, S: SPI, T: Timer, P: GPIO> LCD for C12332<'a, S, T, P> {
}

fn clear(&self) {
let mut i = 0;
while i < 512 {
for i in range(0u, 512) {
self.videobuf[i].set(0);
i += 1;
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/drivers/lcd/ili9341.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use core::option::{Some, None};
use core::iter::{Iterator, range};

use interfaces::lcd;
use interfaces::chario;
use hal::timer;
Expand Down Expand Up @@ -71,20 +74,17 @@ impl<'a> ILI9341<'a> {
}

fn verify_id(&self) -> bool {
let mut i: uint = 0;
let mut data: [u8, ..3] = [0, 0, 0];
let id: [u8, ..3] = [0x00, 0x93, 0x41];
let mut id_matched = true;

while i < 3 {
for i in range(0, 3) {
data[i] = self.read_register(0xd3, (i+1) as u8);
if data[i] != id[i] {
id_matched = false;
return false;
}
i += 1;
}

id_matched
true
}

fn set_power_control_a(&self) {
Expand Down Expand Up @@ -272,13 +272,11 @@ impl<'a> ILI9341<'a> {

self.dc.set_high();
self.cs.set_low();
let mut i = 0;
while i < 38400 {
for i in range(0, 38400) {
self.spi.transfer(0);
self.spi.transfer(0);
self.spi.transfer(0);
self.spi.transfer(0);
i += 1;
}
self.cs.set_high();
}
Expand Down
114 changes: 48 additions & 66 deletions src/drivers/lcd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! Drivers for TFT LCDs.

use core::option::{Some, None};
use core::iter::{Iterator, range};
use core::iter::{Iterator, range, range_inclusive};

use drivers::chario::CharIO;

Expand All @@ -43,60 +43,51 @@ pub trait LCD : CharIO {

/// Draws a line from xy0 to xy1.
fn line(&self, x0_b: u32, y0_b: u32, x1: u32, y1: u32, color: u16) {
let mut x0: u32 = x0_b;
let mut y0: u32 = y0_b;
let mut dx: u32;
let mut dy: u32;
let mut dx_sym: u32;
let mut dy_sym: u32;
let mut dx_x2: u32;
let mut dy_x2: u32;
let mut di: i32;

dx = x1-x0;
dy = y1-y0;

if dx > 0 {
dx_sym = 1;
} else {
dx_sym = -1;
}
let (mut x0, mut y0) = (x0_b, y0_b);

if dy > 0 {
dy_sym = 1;
} else {
dy_sym = -1;
}
let (dx, dy) = ((x1-x0) as i32, (y1-y0) as i32);

let dx_sym: i32 =
if dx > 0 {
1
} else {
-1
};

dx = dx_sym*dx;
dy = dy_sym*dy;
let dy_sym: i32 =
if dy > 0 {
1
} else {
-1
};

dx_x2 = dx*2;
dy_x2 = dy*2;
let (dx, dy) = (dx_sym*dx, dy_sym*dy);

let (dx_x2, dy_x2) = (dx*2, dy*2);

if dx >= dy {
di = (dy_x2 - dx) as i32;
let mut di = dy_x2 - dx;
while x0 != x1 {
self.pixel(x0, y0, color);
x0 += dx_sym;
x0 += dx_sym as u32;
if di < 0 {
di += dy_x2 as i32;
di += dy_x2;
} else {
di += (dy_x2 - dx_x2) as i32;
y0 += dy_sym;
di += dy_x2 - dx_x2;
y0 += dy_sym as u32;
}
}
self.pixel(x0, y0, color);
} else {
di = (dx_x2 - dy) as i32;
let mut di = dx_x2 - dy;
while y0 != y1 {
self.pixel(x0, y0, color);
y0 += dy_sym;
y0 += dy_sym as u32;
if di < 0 {
di += dx_x2 as i32;
di += dx_x2;
} else {
di += (dx_x2 - dy_x2) as i32;
x0 += dx_sym;
di += dx_x2 - dy_x2;
x0 += dx_sym as u32;
}
}
self.pixel(x0, y0, color);
Expand Down Expand Up @@ -132,33 +123,25 @@ pub trait LCD : CharIO {

/// Draws a filled rectangle.
fn fillrect(&self, x0_b: u32, y0_b: u32, x1_b: u32, y1_b: u32, color: u16) {
let mut l: u32;
let mut c: u32;
let mut i: u32;
let mut x0: u32 = x0_b;
let mut y0: u32 = y0_b;
let mut x1: u32 = x1_b;
let mut y1: u32 = y1_b;
if x0 > x1 {
i = x0;
x0 = x1;
x1 = i;
}

if y0 > y1 {
i = y0;
y0 = y1;
y1 = i;
}

l = x0;
while l <= x1 {
c = y0;
while c <= y1 {
self.pixel(l, c, color);
c += 1;
let (x0, x1) =
if x0_b > x1_b {
(x1_b, x0_b)
} else {
(x0_b, x1_b)
};

let (y0, y1) =
if y0_b > y1_b {
(y1_b, y0_b)
} else {
(y0_b, y1_b)
};

for l in range_inclusive(x0, x1) {
for c in range_inclusive(y0, y1) {
self.pixel(l as u32, c as u32, color);
}
l += 1;
}
}

Expand Down Expand Up @@ -298,8 +281,7 @@ mod test {

io.line(0, 0, 15, 15, 1);

// TODO(errordeveloper): investigate why this hangs the test run in `__psynch_cvwait()`
// io.line(15, 15, 0, 0, 1);
io.line(15, 15, 0, 0, 1);

io.for_each(|(x, y), v| {
assert!(v == diagonal[y as uint][x as uint]);
Expand Down Expand Up @@ -367,7 +349,7 @@ mod test {
assert!(overlapping[10][10] == 7);

io.rect(4, 2, 12, 7, 6);
io.rect(7, 4, 10, 10, 7);
io.rect(10, 10, 7, 4, 7);

io.for_each(|(x, y), v| {
assert!(v == overlapping[x as uint][y as uint]);
Expand Down

5 comments on commit 3b083b9

@hacknbot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from farcaller
at errordeveloper@3b083b9

@hacknbot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging errordeveloper/zinc/more_int_fixes = 3b083b9 into auto

@hacknbot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errordeveloper/zinc/more_int_fixes = 3b083b9 merged ok, testing candidate = c008f64

@hacknbot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hacknbot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c008f64

Please sign in to comment.