git ssb

0+

dinoworm ๐Ÿ› / metronome



Tree: fca343292fb72c697c29294488b5b36397ac1dc5

Files: fca343292fb72c697c29294488b5b36397ac1dc5 / src / interface.rs

4566 bytesRaw
1extern crate ncurses;
2
3use ncurses::{WchResult};
4use std::sync::mpsc::{channel, Sender, Receiver};
5use std::thread::{sleep, spawn};
6
7use clock;
8use control;
9
10// https://unicode.org/charts/PDF/U0000.pdf
11static CHAR_SPACE: u32 = 0x0020;
12static CHAR_RETURN: u32 = 0x000D;
13static CHAR_NEWLINE: u32 = 0x000A;
14
15#[derive(Debug)]
16pub struct TerminalInterface {}
17
18impl TerminalInterface {
19 pub fn start (control_tx: Sender<control::ControlMessage>) -> Sender<InterfaceMessage> {
20 let (tx, rx) = channel();
21
22 let mut signature = clock::ClockSignature::default();
23
24 spawn(move|| {
25 /* Setup ncurses. */
26 ncurses::initscr();
27
28 let locale_conf = ncurses::LcCategory::all;
29 ncurses::setlocale(locale_conf, "en_US.UTF-8");
30
31 /* Enable mouse events. */
32 ncurses::mousemask(ncurses::ALL_MOUSE_EVENTS as ncurses::mmask_t, None);
33
34 /* Allow for extended keyboard (like F1). */
35 ncurses::keypad(ncurses::stdscr(), true);
36 ncurses::noecho();
37
38 loop {
39 let ch = ncurses::wget_wch(ncurses::stdscr());
40
41 match ch {
42 Some(WchResult::KeyCode(ncurses::KEY_MOUSE)) => {
43 }
44
45 Some(WchResult::KeyCode(ncurses::KEY_UP)) => { // why is up down?
46 control_tx.send(control::ControlMessage::NudgeTempo(-1_f64)).unwrap();
47 }
48
49 Some(WchResult::KeyCode(ncurses::KEY_DOWN)) => { // why is down up?
50 control_tx.send(control::ControlMessage::NudgeTempo(1_f64)).unwrap();
51 }
52
53 // https://github.com/jeaye/ncurses-rs/blob/master/src/constants.rs
54 Some(WchResult::KeyCode(_)) => {
55 }
56
57 // Some(WchResult::KeyCode(KEY_ENTER)) => beat(),
58 Some(WchResult::Char(ch)) => {
59 if (ch == CHAR_SPACE) {
60 control_tx.send(control::ControlMessage::TapTempo).unwrap();
61 }
62
63 if (ch == CHAR_NEWLINE) {
64 control_tx.send(control::ControlMessage::Reset).unwrap();
65 }
66 }
67
68 None => {}
69 }
70
71 ncurses::refresh();
72 }
73
74 ncurses::endwin();
75 });
76
77 spawn(move|| {
78 for interface_message in rx {
79 match interface_message {
80 InterfaceMessage::Time(time) => {
81 ncurses::clear();
82 ncurses::mv(0, 0);
83 print_beat(time);
84 print_bar(time);
85 print_time(time);
86 print_signature(signature);
87 },
88 InterfaceMessage::Signature(next_signature) => {
89 signature = next_signature;
90 }
91 }
92
93 ncurses::refresh();
94 }
95 });
96
97 tx
98 }
99}
100
101pub fn print_beat (time: clock::ClockTime) {
102 if time.ticks == 0 {
103 if time.beats == 0 {
104 ncurses::printw("SUPER ");
105 }
106 ncurses::printw("BEAT");
107 }
108 ncurses::printw("\n");
109}
110
111pub fn print_bar (time: clock::ClockTime) {
112 if time.bars == 0 {
113 ncurses::printw("YAY YAY YAY");
114 }
115 ncurses::printw("\n");
116}
117
118pub fn print_time (time: clock::ClockTime) {
119 ncurses::printw("nanos: ");
120 ncurses::printw(format!("{}\n", time.nanos).as_ref());
121 ncurses::printw("ticks: ");
122 ncurses::printw(format!("{}\n", time.ticks + 1).as_ref());
123 ncurses::printw("beats: ");
124 ncurses::printw(format!("{}\n", time.beats + 1).as_ref());
125 ncurses::printw("bars: ");
126 ncurses::printw(format!("{}\n", time.bars + 1).as_ref());
127}
128
129pub fn print_signature (signature: clock::ClockSignature) {
130 ncurses::printw("beats per minute: ");
131 ncurses::printw(format!("{}\n", signature.to_beats_per_minute()).as_ref());
132 ncurses::printw("ticks per beat: ");
133 ncurses::printw(format!("{}\n", signature.ticks_per_beat).as_ref());
134 ncurses::printw("beats per bar: ");
135 ncurses::printw(format!("{}\n", signature.beats_per_bar).as_ref());
136 ncurses::printw("bars per loop: ");
137 ncurses::printw(format!("{}\n", signature.bars_per_loop).as_ref());
138}
139
140#[derive(Clone, Copy, Debug, Hash)]
141pub enum InterfaceMessage {
142 Time(clock::ClockTime),
143 Signature(clock::ClockSignature),
144}
145

Built with git-ssb-web