]> git.zerfleddert.de Git - ms2-fixes/blame - MS2Debounce/src/de/rmdir/ms2debounce/MS2Debounce.java
integrate hw debounce into gui
[ms2-fixes] / MS2Debounce / src / de / rmdir / ms2debounce / MS2Debounce.java
CommitLineData
7bcde8d7
MG
1package de.rmdir.ms2debounce;
2
3import android.app.Activity;
fd8f8652
MG
4import android.app.Dialog;
5import android.app.AlertDialog;
7bcde8d7 6import android.os.Bundle;
ae569a50 7import android.content.Intent;
fd8f8652 8import android.content.DialogInterface;
ee6322a1 9import android.widget.TextView;
c51c7009 10import android.widget.EditText;
dea0f4b0
MG
11import android.widget.Button;
12import android.widget.CheckBox;
13import android.view.View;
fd8f8652
MG
14import android.view.Menu;
15import android.view.MenuInflater;
16import android.view.MenuItem;
1559225a
MG
17import android.text.TextWatcher;
18import android.text.Editable;
7bcde8d7
MG
19
20public class MS2Debounce extends Activity
21{
dea0f4b0
MG
22 private DebounceModuleHelper module;
23
e75481cd
MG
24 // Calling these is expensive, so cache the result...
25 private boolean loaded;
26 private boolean safe_to_load;
27 private int debounce_delay;
381027a8
MG
28 private int settle_time;
29 private int poll_time;
2bb83a0e
MG
30 private boolean hw_debounce_en;
31 private int hw_debounce_time;
e75481cd 32
dea0f4b0
MG
33 public MS2Debounce()
34 {
35 super();
36 module = new DebounceModuleHelper(this);
37 }
38
226a7d4d
MG
39 @Override
40 public void onCreate(Bundle savedInstanceState)
41 {
42 super.onCreate(savedInstanceState);
ae569a50 43
dea0f4b0 44 setContentView(R.layout.main);
1559225a
MG
45
46 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
47 textDelay.addTextChangedListener(new TextWatcher() {
48 @Override
49 public void afterTextChanged(Editable delay) {
50 if (delay.toString().length() > 0) {
51 module.setSavedDelay(Integer.parseInt(delay.toString()));
381027a8
MG
52 }
53 }
1559225a 54
381027a8
MG
55 @Override
56 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
57 }
58
59 @Override
60 public void onTextChanged(CharSequence s, int start, int before, int count) {
61 }
62 });
63
64 EditText textSettle = (EditText)findViewById(R.id.settle_time);
65 textSettle.addTextChangedListener(new TextWatcher() {
66 @Override
67 public void afterTextChanged(Editable settle_time) {
68 if (settle_time.toString().length() > 0) {
69 module.setSavedSettle(Integer.parseInt(settle_time.toString()));
70 }
71 }
72
73 @Override
74 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
75 }
76
77 @Override
78 public void onTextChanged(CharSequence s, int start, int before, int count) {
79 }
80 });
81
82 EditText textPoll = (EditText)findViewById(R.id.poll_time);
83 textPoll.addTextChangedListener(new TextWatcher() {
84 @Override
85 public void afterTextChanged(Editable poll_time) {
86 if (poll_time.toString().length() > 0) {
87 module.setSavedPoll(Integer.parseInt(poll_time.toString()));
1559225a
MG
88 }
89 }
90
91 @Override
92 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
93 }
94
95 @Override
96 public void onTextChanged(CharSequence s, int start, int before, int count) {
97 }
98 });
99
2bb83a0e
MG
100 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
101 textHwDebounceTime.addTextChangedListener(new TextWatcher() {
102 @Override
103 public void afterTextChanged(Editable hw_debounce_time) {
104 if (hw_debounce_time.toString().length() > 0) {
105 module.setSavedHwDebounceTime(Integer.parseInt(hw_debounce_time.toString()));
106 }
107 }
108
109 @Override
110 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
111 }
112
113 @Override
114 public void onTextChanged(CharSequence s, int start, int before, int count) {
115 }
116 });
117
dea0f4b0
MG
118 updateUI();
119 }
120
121 private void updateUI() {
dea0f4b0
MG
122 disableUI();
123
01b288b5 124 // Calling these is expensive, so cache the result...
e75481cd
MG
125 loaded = module.isLoaded();
126 safe_to_load = module.is_safe_to_load();
127 debounce_delay = module.getDelay();
381027a8
MG
128 settle_time = module.getSettle();
129 poll_time = module.getPoll();
2bb83a0e
MG
130 hw_debounce_en = module.getHwDebounce();
131 hw_debounce_time = module.getHwDebounceTime();
dea0f4b0 132
c51c7009 133 TextView text = (TextView)findViewById(R.id.text);
882feaf4 134 text.setText("Module loaded: " + loaded + "\n" +
381027a8
MG
135 "debounce_delay: " + debounce_delay + "ms\n" +
136 "settle_time: " + settle_time + "us\n" +
722ab95d 137 "poll_time: " + poll_time + "ms\n" +
2bb83a0e
MG
138 "safe_to_load: " + safe_to_load + " (module loaded by this app)\n" +
139 "hw_debounce: " + (hw_debounce_en?"enabled":"disabled") + ", " + ((hw_debounce_time+1)*31) + "us (" + hw_debounce_time + ")");
dea0f4b0 140
1559225a
MG
141 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
142 textDelay.setText(Integer.toString(module.getSavedDelay()));
143 textDelay.setEnabled(true);
c51c7009 144
381027a8
MG
145 EditText textSettle = (EditText)findViewById(R.id.settle_time);
146 textSettle.setText(Integer.toString(module.getSavedSettle()));
147 textSettle.setEnabled(true);
148
149 EditText textPoll = (EditText)findViewById(R.id.poll_time);
150 textPoll.setText(Integer.toString(module.getSavedPoll()));
151 textPoll.setEnabled(true);
152
2bb83a0e
MG
153 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
154 textHwDebounceTime.setText(Integer.toString(module.getSavedHwDebounceTime()));
155 textHwDebounceTime.setEnabled(true);
156
381027a8
MG
157 Button set = (Button)findViewById(R.id.set);
158 if (loaded) {
159 set.setEnabled(true);
dea0f4b0 160 } else {
381027a8 161 set.setEnabled(false);
dea0f4b0
MG
162 }
163
164 Button load = (Button)findViewById(R.id.load);
165 if (loaded) {
166 load.setEnabled(false);
167 } else {
168 load.setEnabled(true);
169 }
170
171 Button unload = (Button)findViewById(R.id.unload);
172 if (loaded) {
173 unload.setEnabled(true);
174 } else {
175 unload.setEnabled(false);
176 }
226a7d4d 177
dea0f4b0 178 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
c3053460 179 on_boot.setChecked(module.get_on_boot());
dea0f4b0
MG
180 if (safe_to_load) {
181 on_boot.setEnabled(true);
182 } else {
183 on_boot.setEnabled(false);
184 }
2bb83a0e
MG
185
186 CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce);
187 hw_debounce.setChecked(module.getSavedHwDebounce());
188 hw_debounce.setEnabled(true);
dea0f4b0
MG
189 }
190
191 private void disableUI() {
1559225a
MG
192 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
193 textDelay.setEnabled(false);
194
381027a8
MG
195 EditText textSettle = (EditText)findViewById(R.id.settle_time);
196 textSettle.setEnabled(false);
197
198 EditText textPoll = (EditText)findViewById(R.id.poll_time);
199 textPoll.setEnabled(false);
200
2bb83a0e
MG
201 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
202 textHwDebounceTime.setEnabled(false);
203
381027a8
MG
204 Button set = (Button)findViewById(R.id.set);
205 set.setEnabled(false);
dea0f4b0
MG
206
207 Button load = (Button)findViewById(R.id.load);
208 load.setEnabled(false);
209
210 Button unload = (Button)findViewById(R.id.unload);
211 unload.setEnabled(false);
212
213 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
214 on_boot.setEnabled(false);
2bb83a0e
MG
215
216 CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce);
217 hw_debounce.setEnabled(false);
dea0f4b0
MG
218 }
219
220 public void loadModule(View view) {
221 disableUI();
226a7d4d
MG
222 if (!module.isLoaded()) {
223 module.loadModule();
224 }
dea0f4b0
MG
225 updateUI();
226 }
ae569a50 227
dea0f4b0
MG
228 public void unloadModule(View view) {
229 disableUI();
230 if (module.isLoaded()) {
231 module.unloadModule();
232 }
233 updateUI();
234 }
ee6322a1 235
381027a8 236 public void setValues(View view) {
dea0f4b0 237 disableUI();
dea0f4b0
MG
238 if (!module.isLoaded()) {
239 module.loadModule();
240 }
381027a8 241 module.setAllValues();
dea0f4b0 242 updateUI();
226a7d4d 243 }
fd8f8652 244
c3053460
MG
245 public void toggle_on_boot(View view) {
246 CheckBox on_boot = (CheckBox)view;
247
248 module.set_on_boot(on_boot.isChecked());
249 }
250
2bb83a0e
MG
251 public void toggle_hw_debounce(View view) {
252 CheckBox hw_debounce = (CheckBox)view;
253
254 module.setSavedHwDebounce(hw_debounce.isChecked());
255 }
256
fd8f8652
MG
257 @Override
258 public boolean onCreateOptionsMenu(Menu menu) {
259 MenuInflater inflater = getMenuInflater();
260 inflater.inflate(R.menu.main, menu);
261 return true;
262 }
263
264 @Override
265 public boolean onOptionsItemSelected(MenuItem item) {
266 switch (item.getItemId()) {
267 case R.id.about:
268 showDialog(42);
269 return true;
270 default:
271 return super.onOptionsItemSelected(item);
272 }
273 }
274
275 protected Dialog onCreateDialog(int id) {
276 Dialog dlg = null;
277
278 AlertDialog.Builder about = new AlertDialog.Builder(this);
279 about.setMessage("Milestone 2 Debounce\n\n(C) 2011 Michael Gernoth <michael@gernoth.net>\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2 of the License.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA")
280 .setCancelable(true)
281 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
282 public void onClick(DialogInterface dialog, int id) {
283 dialog.cancel();
284 }
285 });
286 dlg = about.create();
287
288 return dlg;
289 }
7bcde8d7 290}
Impressum, Datenschutz