binary segment representation
This commit is contained in:
174
src/display.rs
174
src/display.rs
@@ -82,169 +82,133 @@ impl SegmentPins {
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Segment {
|
||||
a: bool,
|
||||
b: bool,
|
||||
c: bool,
|
||||
d: bool,
|
||||
e: bool,
|
||||
f: bool,
|
||||
g: bool,
|
||||
dp: bool,
|
||||
}
|
||||
pub struct Segment(u8);
|
||||
|
||||
impl Segment {
|
||||
pub fn new() -> Segment {
|
||||
Segment {
|
||||
a: false,
|
||||
b: false,
|
||||
c: false,
|
||||
d: false,
|
||||
e: false,
|
||||
f: false,
|
||||
g: false,
|
||||
dp: false,
|
||||
Segment(0)
|
||||
}
|
||||
|
||||
pub fn off(&mut self) -> &mut Self {
|
||||
self.0 = 0;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn a(&mut self) -> &mut Self {
|
||||
self.0 |= 0b1000_0000;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn b(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0100_0000;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn c(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0010_0000;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn d(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0001_0000;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn e(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0000_1000;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn f(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0000_0100;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn g(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0000_0010;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn dp(&mut self) -> &mut Self {
|
||||
self.0 |= 0b0000_0001;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn apply(&self, seg: &mut SegmentPins) {
|
||||
seg.set_off();
|
||||
if self.a {
|
||||
if self.0 & 0b1000_0000 != 0 {
|
||||
seg.set_a();
|
||||
}
|
||||
if self.b {
|
||||
if self.0 & 0b0100_0000 != 0 {
|
||||
seg.set_b();
|
||||
}
|
||||
if self.c {
|
||||
if self.0 & 0b0010_0000 != 0 {
|
||||
seg.set_c();
|
||||
}
|
||||
if self.d {
|
||||
if self.0 & 0b0001_0000 != 0 {
|
||||
seg.set_d();
|
||||
}
|
||||
if self.e {
|
||||
if self.0 & 0b0000_1000 != 0 {
|
||||
seg.set_e();
|
||||
}
|
||||
if self.f {
|
||||
if self.0 & 0b0000_0100 != 0 {
|
||||
seg.set_f();
|
||||
}
|
||||
if self.g {
|
||||
if self.0 & 0b0000_0010 != 0 {
|
||||
seg.set_g();
|
||||
}
|
||||
if self.dp {
|
||||
if self.0 & 0b0000_0001 != 0 {
|
||||
seg.set_dp();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn off(&mut self) -> &mut Self {
|
||||
self.a = false;
|
||||
self.b = false;
|
||||
self.c = false;
|
||||
self.d = false;
|
||||
self.e = false;
|
||||
self.f = false;
|
||||
self.g = false;
|
||||
self.dp = false;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn num(&mut self, no: u8) -> &mut Self {
|
||||
match no {
|
||||
0 => {
|
||||
self.a = true;
|
||||
self.b = true;
|
||||
self.c = true;
|
||||
self.d = true;
|
||||
self.e = true;
|
||||
self.f = true;
|
||||
self.a().b().c().d().e().f();
|
||||
}
|
||||
1 => {
|
||||
self.b = true;
|
||||
self.c = true;
|
||||
self.b().c();
|
||||
}
|
||||
2 => {
|
||||
self.a = true;
|
||||
self.b = true;
|
||||
self.g = true;
|
||||
self.e = true;
|
||||
self.d = true;
|
||||
self.a().b().g().e().d();
|
||||
}
|
||||
3 => {
|
||||
self.a = true;
|
||||
self.b = true;
|
||||
self.g = true;
|
||||
self.c = true;
|
||||
self.d = true;
|
||||
self.a().b().g().c().d();
|
||||
}
|
||||
4 => {
|
||||
self.f = true;
|
||||
self.g = true;
|
||||
self.b = true;
|
||||
self.c = true;
|
||||
self.f().g().b().c();
|
||||
}
|
||||
5 => {
|
||||
self.a = true;
|
||||
self.f = true;
|
||||
self.g = true;
|
||||
self.c = true;
|
||||
self.d = true;
|
||||
self.a().f().g().c().d();
|
||||
}
|
||||
6 => {
|
||||
self.a = true;
|
||||
self.f = true;
|
||||
self.g = true;
|
||||
self.c = true;
|
||||
self.d = true;
|
||||
self.e = true;
|
||||
self.a().f().g().c().d().e();
|
||||
}
|
||||
7 => {
|
||||
self.a = true;
|
||||
self.b = true;
|
||||
self.c = true;
|
||||
self.a().b().c();
|
||||
}
|
||||
8 => {
|
||||
self.a = true;
|
||||
self.b = true;
|
||||
self.c = true;
|
||||
self.d = true;
|
||||
self.e = true;
|
||||
self.f = true;
|
||||
self.g = true;
|
||||
self.a().b().c().d().e().f().g();
|
||||
}
|
||||
9 => {
|
||||
self.a = true;
|
||||
self.b = true;
|
||||
self.c = true;
|
||||
self.d = true;
|
||||
self.f = true;
|
||||
self.g = true;
|
||||
self.a().b().c().d().f().g();
|
||||
}
|
||||
_ => panic!("Num out of range"),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn dp(&mut self) -> &mut Self {
|
||||
self.dp = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn minus(&mut self) -> &mut Self {
|
||||
self.g = true;
|
||||
self
|
||||
self.g()
|
||||
}
|
||||
|
||||
pub fn prompt(&mut self) -> &mut Self {
|
||||
self.d = true;
|
||||
self
|
||||
self.d()
|
||||
}
|
||||
|
||||
pub fn e(&mut self) -> &mut Self {
|
||||
self.a = true;
|
||||
self.d = true;
|
||||
self.e = true;
|
||||
self.f = true;
|
||||
self.g = true;
|
||||
self
|
||||
pub fn sym_e(&mut self) -> &mut Self {
|
||||
self.a().d().e().f().g()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,7 +257,7 @@ impl DispalyState {
|
||||
self[1].off();
|
||||
self[2].off();
|
||||
self[3].off().minus();
|
||||
self[4].off().e();
|
||||
self[4].off().sym_e();
|
||||
self[5].off().minus();
|
||||
self[6].off();
|
||||
self[7].off();
|
||||
|
||||
Reference in New Issue
Block a user