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

More int type fixes and tests for drivers #80

Merged
merged 6 commits into from
Jul 8, 2014
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ rust_tests :platformtree_test, {
produce: 'platformtree_test'.in_build,
}

rust_tests :zinc_test, {
source: 'main.rs'.in_source,
deps: [:core_crate],
produce: 'zinc_test'.in_build,
recompile_on: [:platform, :features],
build_for: :host,
}

# macros
compile_rust :macro_platformtree, {
source: 'macro/platformtree.rs'.in_root,
Expand Down
122 changes: 118 additions & 4 deletions src/drivers/chario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::option::{Some, None};
use core::str::{Str, StrSlice};
use core::slice::{Vector, ImmutableVector};
use core::collections::Collection;
use core::iter::Iterator;
use core::iter::{Iterator, range};

use core::mem::zeroed;

Expand All @@ -36,11 +36,9 @@ pub trait CharIO {
/// Outputs a string.
fn puts(&self, s: &str) {
let chars : &[u8] = s.as_slice().as_bytes();
let mut i = 0;
while i < s.len() {
for i in range(0, s.len()) {
let c : char = chars[i] as char;
self.putc(c);
i += 1;
}
}

Expand Down Expand Up @@ -68,3 +66,119 @@ pub trait CharIO {
self.putint(i, 16);
}
}

#[cfg(test)]
pub mod test {
use core::cell::RefCell;

use drivers::chario::CharIO;
Copy link
Member

Choose a reason for hiding this comment

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

One empty line between system and local use


pub struct TestCharIOData {
last_char: char,
putc_calls: uint,
}

pub struct TestCharIO {
data: RefCell<TestCharIOData>
}

impl CharIO for TestCharIO {
fn putc(&self, value: char) {
let mut data = self.data.borrow_mut();
data.putc_calls += 1;
data.last_char = value;
}
}

impl TestCharIO {
pub fn new() -> TestCharIO {
TestCharIO {
data: RefCell::new(TestCharIOData {
last_char: '\0',
putc_calls: 0,
}),
}
}

fn get_last_char(&self) -> char {
self.data.borrow().last_char
}

fn get_and_reset_putc_calls(&self) -> uint {
let current = self.data.borrow().putc_calls;
self.data.borrow_mut().putc_calls = 0;
current
}
}

#[test]
fn putc_should_store_a_char() {
let io = TestCharIO::new();
io.putc('a');
assert!(io.get_last_char() == 'a');
io.putc('z');
assert!(io.get_last_char() == 'z');
}

#[test]
fn puti_should_store_a_number_as_char() {
let io = TestCharIO::new();
io.puti(3);
assert!(io.get_last_char() == '3');
io.puti(9);
assert!(io.get_last_char() == '9');
io.puti(10);
assert!(io.get_last_char() == '0');
io.puti(11);
assert!(io.get_last_char() == '1');
}

#[test]
fn puth_should_store_a_number_as_char() {
let io = TestCharIO::new();
io.puth(3);
assert!(io.get_last_char() == '3');
io.puth(9);
assert!(io.get_last_char() == '9');
io.puth(10);
assert!(io.get_last_char() == 'a');
io.puth(11);
assert!(io.get_last_char() == 'b');
io.puth(16);
assert!(io.get_last_char() == '0');
io.puth(17);
assert!(io.get_last_char() == '1');
}

#[test]
fn putint_should_work_with_different_bases() {
let io = TestCharIO::new();
io.putint(0, 2);
assert!(io.get_last_char() == '0');
io.putint(1, 2);
assert!(io.get_last_char() == '1');
io.putint(2, 2);
assert!(io.get_last_char() == '0');
io.putint(3, 2);
assert!(io.get_last_char() == '1');
io.putint(7, 7);
assert!(io.get_last_char() == '0');
io.putint(8, 7);
assert!(io.get_last_char() == '1');
io.putint(12, 7);
assert!(io.get_last_char() == '5');
io.putint(14, 7);
assert!(io.get_last_char() == '0');
}

#[test]
fn puts_should_leave_us_with_just_the_last_char() {
let io = TestCharIO::new();
io.puts("fu!");
assert!(io.get_last_char() == '!');
assert!(io.get_and_reset_putc_calls() == 3);
io.puts("\n\t");
assert!(io.get_last_char() == '\t');
assert!(io.get_and_reset_putc_calls() == 2);
}
}
24 changes: 12 additions & 12 deletions src/drivers/lcd/c12332.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ impl<'a, S: SPI, T: Timer, P: GPIO> C12332<'a, S, T, P> {
self.cs.set_high();
}

pub fn set_pixel(&self, x: i32, y: i32, color: u16) {
if x > 127 || y > 31 || x < 0 || y < 0 {
pub fn set_pixel(&self, x: u32, y: u32, color: u16) {
if x > 127 || y > 31 {
return
}

let index = x + (y/8) * 128;
let index = (x + (y/8) * 128) as uint;
if color == 0 {
self.videobuf[index as uint].set(
self.videobuf[index as uint].get() & !(1i32 << (y%8i32) as uint) as u8);
self.videobuf[index].set(
self.videobuf[index].get() & !(1u8 << (y%8u32) as uint) as u8);
} else {
self.videobuf[index as uint].set(
self.videobuf[index as uint].get() | (1 << ((y%8) as uint)));
self.videobuf[index].set(
self.videobuf[index].get() | (1 << ((y%8) as uint)));
}
}

pub fn character(&self, x: i32, y: i32, c: u8) {
pub fn character(&self, x: u32, y: u32, c: u8) {
let hor: u8;
let vert: u8;
let offset: u8;
Expand Down Expand Up @@ -176,9 +176,9 @@ impl<'a, S: SPI, T: Timer, P: GPIO> C12332<'a, S, T, P> {
z = zeichen[(bpl * i + ((j & 0xF8) >> 3)+1) as uint];
b = 1 << ((j & 0x07) as uint);
if ( z & b ) == 0x00 {
self.set_pixel(x+i as i32, y+j as i32, 0);
self.set_pixel(x+i as u32, y+j as u32, 0);
} else {
self.set_pixel(x+i as i32, y+j as i32, 1);
self.set_pixel(x+i as u32, y+j as u32, 1);
}
i += 1;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<'a, S: SPI, T: Timer, P: GPIO> LCD for C12332<'a, S, T, P> {
}
}

fn pixel(&self, x: i32, y: i32, color: u16) {
fn pixel(&self, x: u32, y: u32, color: u16) {
self.set_pixel(x, y, color);
}
}
Expand All @@ -260,7 +260,7 @@ impl<'a, S: SPI, T: Timer, P: GPIO> CharIO for C12332<'a, S, T, P> {
self.char_y.set(0);
}
} else {
self.character(self.char_x.get() as i32, self.char_y.get() as i32, value as u8);
self.character(self.char_x.get(), self.char_y.get(), value as u8);
}
}
}
4 changes: 2 additions & 2 deletions src/drivers/lcd/ili9341.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ impl<'a> lcd::LCD for ILI9341<'a> {
self.do_clear();
}
fn flush(&self) {}
fn pixel(&self, x: i32, y: i32, color: u16) {
self.do_pixel(x as u32, y as u32, color);
fn pixel(&self, x: u32, y: u32, color: u16) {
self.do_pixel(x, y, color);
}
}

Expand Down
Loading