stevafernandes commited on
Commit
7ce905d
·
verified ·
1 Parent(s): 0b3900a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ q = 0
4
+ q_bar = 1
5
+ history = []
6
+
7
+
8
+ def sr_latch(s, r):
9
+ global q, q_bar, history
10
+
11
+ s = int(s)
12
+ r = int(r)
13
+
14
+ if s == 1 and r == 1:
15
+ state = "INVALID"
16
+ elif s == 1 and r == 0:
17
+ q, q_bar = 1, 0
18
+ state = "SET"
19
+ elif s == 0 and r == 1:
20
+ q, q_bar = 0, 1
21
+ state = "RESET"
22
+ else:
23
+ state = "HOLD"
24
+
25
+ q_display = "X" if state == "INVALID" else str(q)
26
+ q_bar_display = "X" if state == "INVALID" else str(q_bar)
27
+
28
+ history.append(f"S={s}, R={r} => Q={q_display}, Q'={q_bar_display} [{state}]")
29
+
30
+ log = "\n".join(history)
31
+
32
+ return q_display, q_bar_display, state, log
33
+
34
+
35
+ def reset_latch():
36
+ global q, q_bar, history
37
+ q = 0
38
+ q_bar = 1
39
+ history = []
40
+ return "0", "1", "HOLD", ""
41
+
42
+
43
+ with gr.Blocks(title="SR Latch Simulator") as demo:
44
+ gr.Markdown("## SR Latch Simulator")
45
+ gr.Markdown(
46
+ "Toggle **S** (set) and **R** (reset) inputs to see how the latch responds. "
47
+ "The latch remembers its previous state when both inputs are 0."
48
+ )
49
+
50
+ with gr.Row():
51
+ s_input = gr.Radio(choices=["0", "1"], value="0", label="S (Set)")
52
+ r_input = gr.Radio(choices=["0", "1"], value="0", label="R (Reset)")
53
+
54
+ apply_btn = gr.Button("Apply", variant="primary")
55
+
56
+ with gr.Row():
57
+ q_output = gr.Textbox(label="Q", value="0", interactive=False)
58
+ q_bar_output = gr.Textbox(label="Q'", value="1", interactive=False)
59
+ state_output = gr.Textbox(label="State", value="HOLD", interactive=False)
60
+
61
+ gr.Markdown("### Truth table")
62
+ gr.Dataframe(
63
+ value=[
64
+ ["0", "0", "Prev", "Prev", "Hold"],
65
+ ["1", "0", "1", "0", "Set"],
66
+ ["0", "1", "0", "1", "Reset"],
67
+ ["1", "1", "X", "X", "Invalid"],
68
+ ],
69
+ headers=["S", "R", "Q", "Q'", "State"],
70
+ interactive=False,
71
+ )
72
+
73
+ gr.Markdown("### History")
74
+ history_box = gr.Textbox(label="Log", lines=8, interactive=False)
75
+
76
+ reset_btn = gr.Button("Reset latch")
77
+
78
+ apply_btn.click(
79
+ fn=sr_latch,
80
+ inputs=[s_input, r_input],
81
+ outputs=[q_output, q_bar_output, state_output, history_box],
82
+ )
83
+
84
+ reset_btn.click(
85
+ fn=reset_latch,
86
+ inputs=[],
87
+ outputs=[q_output, q_bar_output, state_output, history_box],
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()