add circle drawing to graphics

This commit is contained in:
bunnie 2020-11-23 00:40:40 +08:00
parent f61ae8ee2a
commit 16e9f43015
6 changed files with 771 additions and 65 deletions

View file

@ -1,23 +1,58 @@
#!/bin/sh
#!/bin/bash
# argument 1 is the target for copy
set -e # exit on any error
if [ -z "$1" ]
then
echo "Usage: $0 ssh-target [privatekey]"
echo "Missing ssh-target argument."
echo "Assumes betrusted-scripts repo is cloned on ssh-target at ~/code/betrused-scripts/"
exit 0
fi
UPDATE_FPGA=1
UPDATE_KERNEL=1
USE_USB=1
FPGA_IMAGE=../betrusted-soc/build/gateware/encrypted.bin
KERNEL_IMAGE=target/riscv32imac-unknown-none-elf/release/xous.img
CSR_CSV=../betrusted-soc/build/csr.csv.1 # always use the previous version for the current update!
USE_IDENITY=0
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-k|--kernel-skip)
UPDATE_KERNEL=0
shift
;;
-f|--fpga-skip)
UPDATE_FPGA=0
shift
;;
-c|--copy-to)
DEST_HOST=$2
USE_USB=0
shift
shift
;;
-i|--identity)
USE_IDENTITY=1
IDENTITY=$2
shift
shift
;;
-h|--help)
echo "$0 provisions betrusted. --kernel-skip skips the kernel, --fpga-skip skips the FPGA."
echo "Alternatively, using --copy-to <hostname> copies the files to a remote host and skips provisioning."
echo " --identity <id_file> supplies an identity file to the scp command."
echo " Copying assumes betrusted-scripts repo is cloned on ssh-target at ~/code/betrused-scripts/"
exit 0
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
DESTDIR=code/precursors
# notes:
# xous-stage1.bin written to 0x2050_0000 (64k erase block size)
# xous-kernel.bin written to 0x2051_0000 => passed as a0 arg to xous-stage1.bin
# This is handled in part by betrusted-scripts, with provision-xous.sh
# stage1 and kernel are merged into xous.img by this script.
cargo xtask hw-image ../betrusted-soc/build/software/soc.svd
if [ $? -ne 0 ]
@ -26,19 +61,29 @@ then
exit 1
fi
echo "Copying to target..."
if [ $# -gt 0 ]
# case of no private key specified
if [ $USE_USB -eq 1 ]
then
if [ -z "$2" ]
if [ $UPDATE_FPGA -eq 1 ]
then
scp target/riscv32imac-unknown-none-elf/release/xous.img $1:$DESTDIR/
scp ../betrusted-soc/build/gateware/encrypted.bin $1:$DESTDIR/
scp ../betrusted-soc/build/csr.csv $1:$DESTDIR/soc-csr.csv
else
scp -i $2 target/riscv32imac-unknown-none-elf/release/xous.img $1:$DESTDIR/
scp -i $2 ../betrusted-soc/build/gateware/encrypted.bin $1:$DESTDIR/
scp -i $2 ../betrusted-soc/build/csr.csv $1:$DESTDIR/soc-csr.csv
echo "Burning FPGA image"
sudo wishbone-tool --csr-csv $CSR_CSV --load-name $FPGA_IMAGE --load-address 0x0 --load-flash
echo "*** Manual power cycle required to reload SoC FPGA configuration ***"
echo " -> Either issue a power cycle command, or insert paper clip in the hole on the right hand side!"
fi
if [ $UPDATE_KERNEL -eq 1 ]
then
echo "Burning firmware"
sudo wishbone-tool --csr-csv $CSR_CSV --load-name $KERNEL_IMAGE --load-address 0x500000 --load-flash
fi
else
echo "Copy to target with $0 <user@host> <ssh-id>"
if [ $USE_IDENTITY -eq 1 ]
then
# there is a private key
scp -i $IDENTITY $KERNEL_IMAGE $FPGA_IMAGE $DEST_HOST:$DESTDIR/
scp -i $IDENTITY $CSR_CSV $DEST_HOST:$DESTDIR/soc-csr.csv
else
scp $KERNEL_IMAGE $FPGA_IMAGE $CSR_CSV $DEST_HOST:$DESTDIR/
scp $CSR_CSV $DEST_HOST:$DESTDIR/soc-csr.csv
fi
fi

View file

@ -1,15 +1,497 @@
use crate::op::PixelColor;
use xous::{Message, ScalarMessage};
use core::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign};
#[derive(Copy, Clone, Debug)]
/// 2D size.
///
/// `Size` is used to define the width and height of an object.
///
/// [Nalgebra] support can be enabled with the `nalgebra_support` feature. This implements
/// `From<Vector2<N>>` and `From<&Vector2<N>>` where `N` is `Scalar + Into<u16>`. This allows use
/// of Nalgebra's [`Vector2`] with embedded-graphics where `u16`, `u16` or `u8` is used for value
/// storage.
///
/// # Examples
///
/// ## Create a `Size` from two integers
///
///
/// ```rust
/// use embedded_graphics::geometry::Size;
///
/// // Create a size using the `new` constructor method
/// let s = Size::new(10, 20);
/// ```
///
/// ## Create a `Size` from a Nalgebra `Vector2`
///
/// _Be sure to enable the `nalgebra_support` feature to get [Nalgebra] integration._
///
/// Any `Vector2<N>` can be used where `N: Into<u16> + nalgebra::Scalar`. This includes the primitive types `u16`, `u16` and `u8`.
///
/// ```rust
/// # #[cfg(feature = "nalgebra_support")] {
/// use nalgebra::Vector2;
/// use embedded_graphics::geometry::Size;
///
/// assert_eq!(Size::from(Vector2::new(10u16, 20)), Size::new(10u16, 20));
/// assert_eq!(Size::from(Vector2::new(10u16, 20)), Size::new(10u16, 20));
/// assert_eq!(Size::from(Vector2::new(10u8, 20)), Size::new(10u16, 20));
/// # }
/// ```
///
/// `.into()` can also be used, but may require more type annotations:
///
/// ```rust
/// # #[cfg(feature = "nalgebra_support")] {
/// use nalgebra::Vector2;
/// use embedded_graphics::geometry::Size;
///
/// let c: Size = Vector2::new(10u16, 20).into();
///
/// assert_eq!(c, Size::new(10u16, 20));
/// # }
/// ```
///
/// [`Drawable`]: ../drawable/trait.Drawable.html
/// [`Point`]: struct.Point.html
/// [`Vector2<N>`]: https://docs.rs/nalgebra/0.18.0/nalgebra/base/type.Vector2.html
/// [`Vector2`]: https://docs.rs/nalgebra/0.18.0/nalgebra/base/type.Vector2.html
/// [Nalgebra]: https://docs.rs/nalgebra
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Size {
/// The width.
pub width: u16,
/// The height.
pub height: u16,
}
impl Size {
/// Creates a size from a width and a height.
pub const fn new(width: u16, height: u16) -> Self {
Size { width, height }
}
/// Creates a size with width and height equal to zero.
pub const fn zero() -> Self {
Size {
width: 0,
height: 0,
}
}
/// Creates a size from two corner points of a bounding box.
pub(crate) fn from_bounding_box(corner_1: Point, corner_2: Point) -> Self {
let width = (corner_1.x - corner_2.x).abs() as u16;
let height = (corner_1.y - corner_2.y).abs() as u16;
Self { width, height }
}
}
impl Add for Size {
type Output = Size;
fn add(self, other: Size) -> Size {
Size::new(self.width + other.width, self.height + other.height)
}
}
impl AddAssign for Size {
fn add_assign(&mut self, other: Size) {
self.width += other.width;
self.height += other.height;
}
}
impl Sub for Size {
type Output = Size;
fn sub(self, other: Size) -> Size {
Size::new(self.width - other.width, self.height - other.height)
}
}
impl SubAssign for Size {
fn sub_assign(&mut self, other: Size) {
self.width -= other.width;
self.height -= other.height;
}
}
impl Index<usize> for Size {
type Output = u16;
fn index(&self, idx: usize) -> &u16 {
match idx {
0 => &self.width,
1 => &self.height,
_ => panic!("index out of bounds: the len is 2 but the index is {}", idx),
}
}
}
impl From<(u16, u16)> for Size {
fn from(other: (u16, u16)) -> Self {
Size::new(other.0, other.1)
}
}
impl From<[u16; 2]> for Size {
fn from(other: [u16; 2]) -> Self {
Size::new(other[0], other[1])
}
}
impl From<&[u16; 2]> for Size {
fn from(other: &[u16; 2]) -> Self {
Size::new(other[0], other[1])
}
}
impl From<Size> for (u16, u16) {
fn from(other: Size) -> (u16, u16) {
(other.width, other.height)
}
}
impl From<&Size> for (u16, u16) {
fn from(other: &Size) -> (u16, u16) {
(other.width, other.height)
}
}
#[cfg(feature = "nalgebra_support")]
use nalgebra::{base::Scalar, Vector2};
#[cfg(feature = "nalgebra_support")]
impl<N> From<Vector2<N>> for Size
where
N: Into<u16> + Scalar,
{
fn from(other: Vector2<N>) -> Self {
Self::new(other[0].into(), other[1].into())
}
}
#[cfg(feature = "nalgebra_support")]
impl<N> From<&Vector2<N>> for Size
where
N: Into<u16> + Scalar,
{
fn from(other: &Vector2<N>) -> Self {
Self::new(other[0].into(), other[1].into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sizes_can_be_added() {
let left = Size::new(10, 20);
let right = Size::new(30, 40);
assert_eq!(left + right, Size::new(40, 60));
}
#[test]
fn sizes_can_be_subtracted() {
let left = Size::new(30, 40);
let right = Size::new(10, 20);
assert_eq!(left - right, Size::new(20, 20));
}
#[test]
fn from_tuple() {
assert_eq!(Size::from((20, 30)), Size::new(20, 30));
}
#[test]
fn from_array() {
assert_eq!(Size::from([20, 30]), Size::new(20, 30));
}
#[test]
fn from_array_ref() {
assert_eq!(Size::from(&[20, 30]), Size::new(20, 30));
}
#[test]
fn index() {
let size = Size::new(1, 2);
assert_eq!(size.width, size[0]);
assert_eq!(size.height, size[1]);
}
#[test]
#[should_panic]
fn index_out_of_bounds() {
let size = Size::new(1, 2);
let _ = size[2];
}
#[test]
#[cfg(feature = "nalgebra_support")]
fn nalgebra_support() {
let left = nalgebra::Vector2::new(30u16, 40);
let right = nalgebra::Vector2::new(10, 20);
assert_eq!(Size::from(left - right), Size::new(20, 20));
}
}
/// Style properties for an object
#[derive(Debug, Copy, Clone)]
pub struct Style {
/// Fill colour of the object
///
/// For fonts, this is the background colour of the text
pub fill_color: Option<PixelColor>,
/// Stroke (border/line) color of the object
///
/// For fonts, this is the foreground colour of the text
pub stroke_color: Option<PixelColor>,
/// Stroke width
///
/// Set the stroke width for an object. Has no effect on fonts.
pub stroke_width: i16,
}
impl Style
{
/// Create a new style with a given stroke value and defaults for everything else
pub fn stroke_color(stroke_color: PixelColor) -> Self {
Self {
stroke_color: Some(stroke_color),
..Style::default()
}
}
/// Returns the stroke width as an `i16`.
///
/// If the stroke width is too large to fit into an `i16` the maximum value
/// for an `i16` is returned instead.
pub(crate) fn stroke_width_i16(&self) -> i16 {
self.stroke_width
}
}
impl Default for Style
{
fn default() -> Self {
Self {
fill_color: None,
stroke_color: None,
stroke_width: 1,
}
}
}
/// Add a style to an object
pub trait WithStyle
{
/// Add a complete style to the object
fn style(self, style: Style) -> Self;
/// Set the stroke colour for the object
///
/// This can be a noop
fn stroke_color(self, color: Option<PixelColor>) -> Self;
/// Set the stroke width for the object
///
/// A stroke with a width of zero will not be rendered
fn stroke_width(self, width: u16) -> Self;
/// Set the fill property of the object's style
///
/// This can be a noop
fn fill_color(self, color: Option<PixelColor>) -> Self;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Point {
pub x: u16,
pub y: u16,
pub x: i16,
pub y: i16,
}
impl Point {
pub fn new(x: u16, y: u16) -> Point {
pub fn new(x: i16, y: i16) -> Point {
Point { x, y }
}
/// Creates a point with X and Y equal to zero.
pub const fn zero() -> Self {
Point { x: 0, y: 0 }
}
}
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point::new(self.x + other.x, self.y + other.y)
}
}
impl Add<Size> for Point {
type Output = Point;
/// Offsets a point by adding a size.
///
/// # Panics
///
/// This function will panic if `width` or `height` are too large to be represented as an `i16`
/// and debug assertions are enabled.
fn add(self, other: Size) -> Point {
let width = other.width as i16;
let height = other.height as i16;
debug_assert!(width >= 0, "width is too large");
debug_assert!(height >= 0, "height is too large");
Point::new(self.x + width, self.y + height)
}
}
impl AddAssign for Point {
fn add_assign(&mut self, other: Point) {
self.x += other.x;
self.y += other.y;
}
}
impl AddAssign<Size> for Point {
/// Offsets a point by adding a size.
///
/// # Panics
///
/// This function will panic if `width` or `height` are too large to be represented as an `i16`
/// and debug assertions are enabled.
fn add_assign(&mut self, other: Size) {
let width = other.width as i16;
let height = other.height as i16;
debug_assert!(width >= 0, "width is too large");
debug_assert!(height >= 0, "height is too large");
self.x += width;
self.y += height;
}
}
impl Sub for Point {
type Output = Point;
fn sub(self, other: Point) -> Point {
Point::new(self.x - other.x, self.y - other.y)
}
}
impl Sub<Size> for Point {
type Output = Point;
/// Offsets a point by subtracting a size.
///
/// # Panics
///
/// This function will panic if `width` or `height` are too large to be represented as an `i16`
/// and debug assertions are enabled.
fn sub(self, other: Size) -> Point {
let width = other.width as i16;
let height = other.height as i16;
debug_assert!(width >= 0, "width is too large");
debug_assert!(height >= 0, "height is too large");
Point::new(self.x - width, self.y - height)
}
}
impl SubAssign for Point {
fn sub_assign(&mut self, other: Point) {
self.x -= other.x;
self.y -= other.y;
}
}
impl SubAssign<Size> for Point {
/// Offsets a point by subtracting a size.
///
/// # Panics
///
/// This function will panic if `width` or `height` are too large to be represented as an `i16`
/// and debug assertions are enabled.
fn sub_assign(&mut self, other: Size) {
let width = other.width as i16;
let height = other.height as i16;
debug_assert!(width >= 0, "width is too large");
debug_assert!(height >= 0, "height is too large");
self.x -= width;
self.y -= height;
}
}
impl Index<usize> for Point {
type Output = i16;
fn index(&self, idx: usize) -> &i16 {
match idx {
0 => &self.x,
1 => &self.y,
_ => panic!("index out of bounds: the len is 2 but the index is {}", idx),
}
}
}
impl Neg for Point {
type Output = Point;
fn neg(self) -> Self::Output {
Point::new(-self.x, -self.y)
}
}
impl From<(i16, i16)> for Point {
fn from(other: (i16, i16)) -> Self {
Point::new(other.0, other.1)
}
}
impl From<[i16; 2]> for Point {
fn from(other: [i16; 2]) -> Self {
Point::new(other[0], other[1])
}
}
impl From<&[i16; 2]> for Point {
fn from(other: &[i16; 2]) -> Self {
Point::new(other[0], other[1])
}
}
impl From<Point> for (i16, i16) {
fn from(other: Point) -> (i16, i16) {
(other.x, other.y)
}
}
impl From<&Point> for (i16, i16) {
fn from(other: &Point) -> (i16, i16) {
(other.x, other.y)
}
}
impl Into<usize> for Point {
@ -18,16 +500,20 @@ impl Into<usize> for Point {
}
}
/// A single pixel
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Pixel(pub Point, pub PixelColor);
#[derive(Copy, Clone, Debug)]
pub struct Rect {
pub x0: u16,
pub y0: u16,
pub x1: u16,
pub y1: u16,
pub x0: i16,
pub y0: i16,
pub x1: i16,
pub y1: i16,
}
impl Rect {
pub fn new(x0: u16, y0: u16, x1: u16, y1: u16) -> Self {
pub fn new(x0: i16, y0: i16, x1: i16, y1: i16) -> Self {
Self { x0, y0, x1, y1 }
}
}
@ -49,7 +535,7 @@ impl From<usize> for Point {
#[derive(Copy, Clone, Debug)]
pub struct Color {
pub color: u32,
pub color: u16,
}
impl From<usize> for Color {
@ -58,6 +544,18 @@ impl From<usize> for Color {
}
}
#[derive(Debug, Clone, Copy)]
pub struct Rectangle {
/// Top left point of the rect
pub top_left: Point,
/// Bottom right point of the rect
pub bottom_right: Point,
/// Object style
pub style: PixelColor,
}
#[derive(Debug)]
pub enum Opcode<'a> {
/// Flush the buffer to the screen
@ -73,11 +571,11 @@ pub enum Opcode<'a> {
Rectangle(Point /* upper-left */, Point /* lower-right */),
/// Draw a circle with a specified radius
Circle(Point, u32 /* radius */),
Circle(Point, u16 /* radius */),
/// Change the style of the current pen
Style(
u32, /* stroke width */
u16, /* stroke width */
Color, /* stroke color */
Color, /* fill color */
),

View file

@ -1,8 +1,11 @@
#![cfg_attr(target_os = "none", no_std)]
// pub mod size;
pub mod api;
pub use api::{Point, Color, Rect};
use xous::String;
pub mod op;
pub mod fonts;
use xous::{send_message, CID};
@ -10,7 +13,7 @@ pub fn draw_line(cid: CID, start: Point, end: Point) -> Result<(), xous::Error>
send_message(cid, api::Opcode::Line(start, end).into()).map(|_| ())
}
pub fn draw_circle(cid: CID, center: Point, radius: u32) -> Result<(), xous::Error> {
pub fn draw_circle(cid: CID, center: Point, radius: u16) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Circle(center, radius).into()).map(|_| ())
}
@ -18,7 +21,7 @@ pub fn draw_rectangle(cid: CID, start: Point, end: Point) -> Result<(), xous::Er
send_message(cid, api::Opcode::Rectangle(start, end).into()).map(|_| ())
}
pub fn set_style(cid: CID, width: u32, stroke: Color, fill: Color) -> Result<(), xous::Error> {
pub fn set_style(cid: CID, width: u16, stroke: Color, fill: Color) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Style(width, stroke, fill).into()).map(|_| ())
}

View file

@ -56,7 +56,6 @@ fn xmain() -> ! {
Opcode::Line(start, end) => {
info!("GFX: Drawing line from {:?} to {:?}", start, end);
op::line(display.native_buffer(), start.x as _, start.y as _, end.x as _, end.y as _, if current_color.color == 0 { op::PixelColor::Off } else {op::PixelColor::On });
// todo!();
}
Opcode::Rectangle(start, end) => {
todo!();
@ -66,11 +65,8 @@ fn xmain() -> ! {
// .ok();
}
Opcode::Circle(mid, radius) => {
todo!();
// Circle::new(mid.into(), radius)
// .into_styled(current_style)
// .draw(&mut display)
// .unwrap();
info!("GFX: Drawing cicrle at {:?} radius {:?}", mid, radius);
op::circle(display.native_buffer(), mid.x as _, mid.y as _, radius as _, 1, op::PixelColor::On);
}
Opcode::Style(stroke_width, stroke_color, fill_color) => {
current_color = stroke_color;

View file

@ -1,5 +1,6 @@
use super::fonts;
use super::fonts::{Font, GlyphHeader};
use crate::api::{Point, Style, Pixel};
/// LCD Frame buffer bounds
pub const LCD_WORDS_PER_LINE: usize = 11;
@ -10,7 +11,7 @@ pub const LCD_FRAME_BUF_SIZE: usize = LCD_WORDS_PER_LINE * LCD_LINES;
const WIDTH: usize = 336;
const HEIGHT: usize = 536;
#[derive(Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PixelColor {
On,
Off,
@ -338,6 +339,86 @@ pub fn line(fb: &mut LcdFB, x0: usize, y0: usize, x1: usize, y1: usize, color: P
}
}
/// Pixel iterator for each pixel in the circle border
#[derive(Debug, Copy, Clone)]
pub struct CircleIterator {
center: Point,
radius: u16,
style: Style,
p: Point,
}
impl Iterator for CircleIterator
{
type Item = Pixel;
// https://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles
fn next(&mut self) -> Option<Self::Item> {
// If border or stroke colour is `None`, treat entire object as transparent and exit early
if self.style.stroke_color.is_none() && self.style.fill_color.is_none() {
return None;
}
let radius = self.radius as i16 - self.style.stroke_width_i16() + 1;
let outer_radius = self.radius as i16;
let radius_sq = radius * radius;
let outer_radius_sq = outer_radius * outer_radius;
loop {
let t = self.p;
let len = t.x * t.x + t.y * t.y;
let is_border = len > radius_sq - radius && len < outer_radius_sq + radius;
let is_fill = len <= outer_radius_sq + 1;
let item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined"),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined"),
))
} else {
None
};
self.p.x += 1;
if self.p.x > self.radius as i16 {
self.p.x = -(self.radius as i16);
self.p.y += 1;
}
if self.p.y > self.radius as i16 {
break None;
}
if item.is_some() {
break item;
}
}
}
}
pub fn circle(fb: &mut LcdFB, x: usize, y: usize, r: usize, stroke_width: usize, color: PixelColor) {
let c = CircleIterator {
center: Point{x: x as i16, y: y as i16},
radius: r as u16,
style: Style{ fill_color: Some(color), stroke_color: Some(color), stroke_width: stroke_width as i16},
p: Point::zero(),
};
for pixel in c {
put_pixel(fb, pixel.0.x as usize, pixel.0.y as usize, pixel.1);
}
}
#[cfg(test)]
mod tests {
use super::fonts;

View file

@ -10,6 +10,7 @@ mod timer;
use core::fmt::Write;
use log::{error, info};
use xous::String;
use graphics_server::Point;
// fn print_and_yield(index: *mut usize) -> ! {
// let num = index as usize;
@ -19,6 +20,29 @@ use xous::String;
// }
// }
#[derive(Debug, Clone, Copy)]
pub struct Rectangle {
/// Top left point of the rect
pub top_left: Point,
/// Bottom right point of the rect
pub bottom_right: Point,
}
impl Rectangle {
pub fn top_left(&self) -> Point {
self.top_left
}
pub fn bottom_right(&self) -> Point {
self.bottom_right
}
pub fn new(top_left: Point, bottom_right: Point) -> Self {
Rectangle {
top_left,
bottom_right,
}
}
}
#[cfg(baremetal)]
use utralib::generated::*;
@ -38,6 +62,68 @@ fn ensure_connection(server: xous::SID) -> xous::CID {
}
}
pub struct Bounce {
vector: Point,
radius: u16,
bounds: Rectangle,
loc: Point,
lfsr: u32,
}
impl Bounce {
pub fn new(radius: u16, bounds: Rectangle) -> Bounce {
Bounce {
vector: Point::new(2,3),
radius: radius,
bounds: bounds,
loc: Point::new((bounds.bottom_right.x - bounds.top_left.x)/2, (bounds.bottom_right.y - bounds.top_left.y)/2),
lfsr: 0xace1u32,
}
}
pub fn next_rand(&mut self) -> i16 {
let ret = move_lfsr(self.lfsr);
self.lfsr = ret;
(ret % 8) as i16
}
pub fn update(&mut self) -> &mut Self {
let mut x: i16;
let mut y: i16;
// update the new ball location
x = self.loc.x + self.vector.x; y = self.loc.y + self.vector.y;
let r: i16 = self.radius as i16;
if (x >= (self.bounds.bottom_right().x - r)) ||
(x <= (self.bounds.top_left().x + r)) ||
(y >= (self.bounds.bottom_right().y - r)) ||
(y <= (self.bounds.top_left().y + r)) {
if x >= (self.bounds.bottom_right().x - r - 1) {
self.vector.x = -self.next_rand();
x = self.bounds.bottom_right().x - r;
}
if x <= self.bounds.top_left().x + r + 1 {
self.vector.x = self.next_rand();
x = self.bounds.top_left().x + r;
}
if y >= (self.bounds.bottom_right().y - r - 1) {
self.vector.y = -self.next_rand();
y = self.bounds.bottom_right().y - r;
}
if y <= (self.bounds.top_left().y + r + 1) {
self.vector.y = self.next_rand();
y = self.bounds.top_left().y + r;
}
}
self.loc.x = x;
self.loc.y = y;
self
}
}
#[xous::xous_main]
fn shell_main() -> ! {
timer::init();
@ -71,9 +157,11 @@ fn shell_main() -> ! {
// let mut counter: usize = 0;
let mut ls = logstr::LogStr::new();
let mut lfsr = 0xace1u32;
let dark = graphics_server::Color::from(0);
let light = graphics_server::Color::from(!0);
let mut bouncyball = Bounce::new(14,
Rectangle::new(Point::new(0, 18 * 21),
Point::new(336, 536-1))); // TODO: make API call to get screen size
#[cfg(baremetal)]
{
@ -88,6 +176,14 @@ fn shell_main() -> ! {
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 2);
}
graphics_server::set_style(
graphics_conn,
1,
dark,
dark,
)
.expect("unable to draw to screen: {:?}");
let mut last_time: u64 = 0;
let mut string_buffer = String::new(4096);
loop {
@ -123,24 +219,10 @@ fn shell_main() -> ! {
error!("error requesting ticktimer!")
}
graphics_server::set_style(
graphics_server::draw_circle(
graphics_conn,
5,
if lfsr & 1 == 0 { dark } else { light },
if lfsr & 1 == 0 { dark } else { light },
)
.expect("unable to draw to screen: {:?}");
let x1 = move_lfsr(lfsr);
let y1 = move_lfsr(x1);
let x2 = move_lfsr(y1);
let y2 = move_lfsr(x2);
lfsr = y2;
graphics_server::draw_line(
graphics_conn,
graphics_server::Point::new((x1 % 336) as _, (y1 % 536) as _),
graphics_server::Point::new((x2 % 336) as _, (y2 % 536) as _),
bouncyball.loc,
bouncyball.radius as u16,
)
.expect("unable to draw to screen");
@ -154,5 +236,6 @@ fn shell_main() -> ! {
ticktimer_server::sleep_ms(ticktimer_conn, 2000 + loops).expect("couldn't sleep");
loops += 1;
bouncyball.update();
}
}