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

Commit

Permalink
trim sudoku and change color.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsxguo committed Jul 2, 2022
1 parent 8ad98e8 commit 288bbd0
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 121 deletions.
2 changes: 0 additions & 2 deletions example/sudoku/.gitignore

This file was deleted.

9 changes: 4 additions & 5 deletions example/sudoku/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#![no_std]



pub use key::*;
use rkgpu::*;
use rkplat::drivers::virtio::{__GPU_DEIVCE, __INPUT_DEIVCE, InputEvent};
Expand Down Expand Up @@ -119,31 +118,31 @@ pub fn input_handler(input_event: InputEvent) {
KEY_W => {
if SELECT_Y >= 75 {
SELECT_Y -= 75;
draw_select(SELECT_OLD_X, SELECT_OLD_Y, CYAN);
draw_select(SELECT_OLD_X, SELECT_OLD_Y, LIGHT_CYAN);
draw_select(SELECT_X, SELECT_Y, RED);
}
INPUT_NUMBER = 100;
}
KEY_S => {
if SELECT_Y < 600 {
SELECT_Y += 75;
draw_select(SELECT_OLD_X, SELECT_OLD_Y, CYAN);
draw_select(SELECT_OLD_X, SELECT_OLD_Y, LIGHT_CYAN);
draw_select(SELECT_X, SELECT_Y, RED);
}
INPUT_NUMBER = 100;
}
KEY_A => {
if SELECT_X >= 75 {
SELECT_X -= 75;
draw_select(SELECT_OLD_X, SELECT_OLD_Y, CYAN);
draw_select(SELECT_OLD_X, SELECT_OLD_Y, LIGHT_CYAN);
draw_select(SELECT_X, SELECT_Y, RED);
}
INPUT_NUMBER = 100;
}
KEY_D => {
if SELECT_X < 600 {
SELECT_X += 75;
draw_select(SELECT_OLD_X, SELECT_OLD_Y, CYAN);
draw_select(SELECT_OLD_X, SELECT_OLD_Y, LIGHT_CYAN);
draw_select(SELECT_X, SELECT_Y, RED);
}
INPUT_NUMBER = 100;
Expand Down
115 changes: 17 additions & 98 deletions example/sudoku/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.





/*
一个数独程序
库依赖:
[dependencies]
rand = { version = "0.8.5", features = ["small_rng"] }
*/
// extern crate rand;
// use rand::Rng;
#![no_std]
#![no_main]
extern crate rkboot;
Expand All @@ -61,10 +47,12 @@ use rktimeconv::TimePoint;
pub mod key;
pub mod input;
pub mod cursor;
pub mod output;

pub use key::*;
pub use input::*;
pub use cursor::*;
pub use output::*;

static mut mutex: Semaphore = Semaphore::new(0);

Expand All @@ -75,34 +63,16 @@ pub struct Sudoku {
tag: [[usize; 9]; 9],
}

impl Sudoku {
// 打印当前数独信息
pub unsafe fn map_print(&self) {
for i in 0..9 {
for j in 0..9 {
// show_sudoku_number(pos_x: u8, pos_y: u8, number: u8);
if self.tag[i][j] == 0 {
show_sudoku_number(i as u8, j as u8, self.map[i][j] as u8, GRAY);
continue;
} else {
show_sudoku_number(i as u8, j as u8, self.map[i][j] as u8, BLACK);
// print!("{} ", self.map[i][j]);
}
}
// println!("");
}
}
}

// 数独零初始化
// 生成一个 9x9 零矩阵
pub fn sudoku_init_zero() -> Sudoku {
let map_allzero = [[0; 9]; 9];
let sudoku = Sudoku {
map: map_allzero,
tag: map_allzero,
};
sudoku
let map_allzero = [[0; 9]; 9];
let sudoku = Sudoku {
map: map_allzero,
tag: map_allzero,
};
sudoku
}

// 数独行随机填充:
Expand Down Expand Up @@ -353,7 +323,7 @@ pub fn hint(map: &mut [[usize; 9]; 9]) -> bool {

add_num(map, nextrow, nextcol, map_allzero[nextrow][nextcol], false);

unsafe { show_sudoku_number(nextrow as u8, nextcol as u8, map_allzero[nextrow][nextcol] as u8, GOLD); }
unsafe { show_sudoku_number(nextrow as u8, nextcol as u8, map_allzero[nextrow][nextcol] as u8, TAN); }

return true;
}
Expand All @@ -369,24 +339,24 @@ pub fn error_hinter(_null: *mut u8) {
}
}

fn init(sudoku:&mut Sudoku) {
fn init(sudoku: &mut Sudoku) {
unsafe {
sched::create_thread("", rkalloc::get_default().unwrap(),
rksched::thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(),
thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(),
input_tracer, null_mut()).expect("TODO: panic message");

sched::create_thread("", rkalloc::get_default().unwrap(),
rksched::thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(),
thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(),
error_hinter, null_mut()).expect("TODO: panic message");

sched::create_thread("", rkalloc::get_default().unwrap(),
rksched::thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(),
thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(),
show_time, null_mut()).expect("TODO: panic message");
rkgpu::init();
printg("Hello, world!\nHello, OSH-2022!\nHello, Runikraft!\n", 700, 10, RED, 255, 4);
printg("Use W, A, S, and D to move selecting rectangle.\nUse up, left, down, and right to move cursor.\nUse H for hint, use O for solution.", 0, 700, BLACK, 255, 2);
update_cursor(900, 500, true);
draw_select(0, 0, RED);
printg("Use W, A, S, and D to move selecting rectangle.\nUse up, left, down, and right to move cursor.\nUse H for hint, use O for solution.", 0, 700, BLACK, 255, 2);
draw_sudoku_lattices(PURPLE, BLACK);
screen_flush();
row_random(&mut sudoku.map, 0);
Expand All @@ -398,9 +368,9 @@ fn init(sudoku:&mut Sudoku) {

#[no_mangle]
fn main() {
let mut sudoku: Sudoku = sudoku_init_zero();
let mut sudoku: Sudoku = sudoku_init_zero();
init(&mut sudoku);
unsafe{
unsafe {
loop {
this_thread::sleep_for(Duration::from_millis(1));

Expand All @@ -425,56 +395,5 @@ fn main() {
}
INPUT_NUMBER = 200;
}
// sudoku_solve(& mut sudoku.map, & mut sudoku.answer, 0, 0);
// unsafe { sudoku.map_print(); }
}
}

use rkplat::drivers::virtio::__GPU_DEIVCE;
use rkgpu::{draw_font, DIRECTION, draw_line};

unsafe fn draw_sudoku_lattices(color0: Color, color1: Color) -> u8 {
let (width, height) = __GPU_DEIVCE.as_mut().unwrap().resolution();
if width >= 750 && height >= 750 {
for x in 0..10 {
if x % 3 == 0 {
draw_line(DIRECTION::Vertical, x * 75, 0, 675, color0, 255, 4);
} else {
draw_line(DIRECTION::Vertical, x * 75, 0, 675, color1, 255, 1);
}
}
for y in 0..10 {
if y % 3 == 0 {
draw_line(DIRECTION::Horizontal, 0, y * 75, 675, color0, 255, 4);
} else {
draw_line(DIRECTION::Horizontal, 0, y * 75, 675, color1, 255, 1);
}
}
1
} else { 0 }
}

unsafe fn show_sudoku_number(pos_x: u8, pos_y: u8, number: u8, color: Color) -> u8 {
if pos_x <= 8 && pos_y <= 8 {
let start_x: u32 = 75 * pos_x as u32 + 20;
let start_y: u32 = 75 * pos_y as u32 + 6;
if number == 0 {
draw_font(start_x, start_y, CYAN, 255, ' ', 4);
} else {
draw_font(start_x, start_y, color, 255, (number + 48).into(), 4);
}
screen_flush();
0
} else { 1 }
}

fn show_time(_null: *mut u8) {
loop {
let timepoint_from_unix: Duration = wall_clock();
let timepoint: TimePoint = TimePoint::from_unix_time(timepoint_from_unix);
let time = alloc::format!("Time: {:04}-{:02}-{:02} {:02}:{:02}:{:02}", timepoint.year(), timepoint.month() + 1,
timepoint.day(), timepoint.hour(), timepoint.min(), timepoint.second());
printg(time.as_str(), 700, 300, ORANGE, 255, 2);
this_thread::sleep_for(Duration::from_secs(1));
}
}
}
Empty file removed example/sudoku/src/mod.rs
Empty file.
115 changes: 115 additions & 0 deletions example/sudoku/src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: BSD-3-Clause
// sudoku/main.rs

// Authors: 蓝俊玮 <[email protected]>
// Authors: 郭耸霄 <[email protected]>

// Copyright (C) 2022 吴骏东, 张子辰, 蓝俊玮, 郭耸霄 and 陈建绿.

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

use core::time::Duration;
use rkplat::drivers::virtio::__GPU_DEIVCE;
use rkgpu::*;
use rkplat::time::wall_clock;
use rktimeconv::*;
use crate::{mutex, Sudoku};

pub unsafe fn draw_sudoku_lattices(color0: Color, color1: Color) -> u8 {
let (width, height) = __GPU_DEIVCE.as_mut().unwrap().resolution();
if width >= 750 && height >= 750 {
for x in 0..10 {
if x % 3 == 0 {
draw_line(DIRECTION::Vertical, x * 75, 0, 675, color0, 255, 4);
} else {
draw_line(DIRECTION::Vertical, x * 75, 0, 675, color1, 255, 1);
}
}
for y in 0..10 {
if y % 3 == 0 {
draw_line(DIRECTION::Horizontal, 0, y * 75, 675, color0, 255, 4);
} else {
draw_line(DIRECTION::Horizontal, 0, y * 75, 675, color1, 255, 1);
}
}
screen_flush();
1
} else { 0 }
}

pub unsafe fn show_sudoku_number(pos_x: u8, pos_y: u8, number: u8, color: Color) -> u8 {
if pos_x <= 8 && pos_y <= 8 {
let start_x: u32 = 75 * pos_x as u32 + 20;
let start_y: u32 = 75 * pos_y as u32 + 6;
if number == 0 {
draw_font(start_x, start_y, LIGHT_CYAN, 255, ' ', 4);
} else {
draw_font(start_x, start_y, color, 255, (number + 48).into(), 4);
}
screen_flush();
0
} else { 1 }
}

pub fn show_time(_null: *mut u8) {
loop {
let timepoint_from_unix: Duration = wall_clock();
let timepoint: TimePoint = TimePoint::from_unix_time(timepoint_from_unix);
let time = alloc::format!("Time: {:04}-{:02}-{:02} {:02}:{:02}:{:02}", timepoint.year(), timepoint.month() + 1,
timepoint.day(), timepoint.hour(), timepoint.min(), timepoint.second());
printg(time.as_str(), 700, 300, BLUE, 255, 2);
rksched::this_thread::sleep_for(Duration::from_secs(1));
}
}

pub fn error_hinter(_null: *mut u8) {
unsafe {
loop {
mutex.wait();
printg("You can't write this number HERE!", 700, 500, RED, 255, 2);
rksched::this_thread::sleep_for(Duration::from_secs(1));
printg(" ", 700, 500, RED, 255, 2);
}
}
}


impl Sudoku {
// 打印当前数独信息
pub unsafe fn map_print(&self) {
for i in 0..9 {
for j in 0..9 {
// show_sudoku_number(pos_x: u8, pos_y: u8, number: u8);
if self.tag[i][j] == 0 {
show_sudoku_number(i as u8, j as u8, self.map[i][j] as u8, GRAY);
continue;
} else {
show_sudoku_number(i as u8, j as u8, self.map[i][j] as u8, BLACK);
// print!("{} ", self.map[i][j]);
}
}
// println!("");
}
}
}
14 changes: 1 addition & 13 deletions lib/rkgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,9 @@ static DIC: [u128; 127] = include!("dic.txt");
pub static mut FB: &mut [u8] = unsafe { &mut _EMPTY };
pub static mut FB_CURSOR: &mut [u32] = &mut [0; 1000];

//static CURSOR: [u8; 16 * 16 * 4] = include!("cursor.txt");

pub unsafe fn init() {
// static mut CURSOR_NEW: [u8; 64 * 64 * 4] = [0; 64 * 64 * 4];
// for i in 0..16 {
// for j in 0..16 {
// CURSOR_NEW[(i * 64 + j) * 4 + 0] = CURSOR[(i * 16 + j) * 4 + 0];
// CURSOR_NEW[(i * 64 + j) * 4 + 1] = CURSOR[(i * 16 + j) * 4 + 1];
// CURSOR_NEW[(i * 64 + j) * 4 + 2] = CURSOR[(i * 16 + j) * 4 + 2];
// CURSOR_NEW[(i * 64 + j) * 4 + 3] = CURSOR[(i * 16 + j) * 4 + 3];
// }
// }
FB = __GPU_DEIVCE.as_mut().unwrap().setup_framebuffer().expect("failed to get FB");
let (width, height) = __GPU_DEIVCE.as_mut().unwrap().resolution();
draw_clear(CYAN);
draw_clear(LIGHT_CYAN);
}

pub fn resolution() -> (u32, u32) {
Expand Down
6 changes: 3 additions & 3 deletions lib/rkgpu/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ pub fn draw_font(start_x: u32, start_y: u32, color: Color, alpha: u8, ch: char,
FB[idx + 0] = color.blue;
FB[idx + 3] = alpha;
} else {
FB[idx + 2] = CYAN.red;
FB[idx + 1] = CYAN.green;
FB[idx + 0] = CYAN.blue;
FB[idx + 2] = LIGHT_CYAN.red;
FB[idx + 1] = LIGHT_CYAN.green;
FB[idx + 0] = LIGHT_CYAN.blue;
FB[idx + 3] = 255;
}
}
Expand Down

0 comments on commit 288bbd0

Please sign in to comment.