binary segment representation

This commit is contained in:
2025-07-18 20:31:23 +01:00
parent 46cb2886f2
commit d69c3eb56d

View File

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