]> git.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/MS2Debounce.java
integrate hw debounce into gui
[ms2-fixes] / MS2Debounce / src / de / rmdir / ms2debounce / MS2Debounce.java
1 package de.rmdir.ms2debounce;
2
3 import android.app.Activity;
4 import android.app.Dialog;
5 import android.app.AlertDialog;
6 import android.os.Bundle;
7 import android.content.Intent;
8 import android.content.DialogInterface;
9 import android.widget.TextView;
10 import android.widget.EditText;
11 import android.widget.Button;
12 import android.widget.CheckBox;
13 import android.view.View;
14 import android.view.Menu;
15 import android.view.MenuInflater;
16 import android.view.MenuItem;
17 import android.text.TextWatcher;
18 import android.text.Editable;
19
20 public class MS2Debounce extends Activity
21 {
22 private DebounceModuleHelper module;
23
24 // Calling these is expensive, so cache the result...
25 private boolean loaded;
26 private boolean safe_to_load;
27 private int debounce_delay;
28 private int settle_time;
29 private int poll_time;
30 private boolean hw_debounce_en;
31 private int hw_debounce_time;
32
33 public MS2Debounce()
34 {
35 super();
36 module = new DebounceModuleHelper(this);
37 }
38
39 @Override
40 public void onCreate(Bundle savedInstanceState)
41 {
42 super.onCreate(savedInstanceState);
43
44 setContentView(R.layout.main);
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()));
52 }
53 }
54
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()));
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
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
118 updateUI();
119 }
120
121 private void updateUI() {
122 disableUI();
123
124 // Calling these is expensive, so cache the result...
125 loaded = module.isLoaded();
126 safe_to_load = module.is_safe_to_load();
127 debounce_delay = module.getDelay();
128 settle_time = module.getSettle();
129 poll_time = module.getPoll();
130 hw_debounce_en = module.getHwDebounce();
131 hw_debounce_time = module.getHwDebounceTime();
132
133 TextView text = (TextView)findViewById(R.id.text);
134 text.setText("Module loaded: " + loaded + "\n" +
135 "debounce_delay: " + debounce_delay + "ms\n" +
136 "settle_time: " + settle_time + "us\n" +
137 "poll_time: " + poll_time + "ms\n" +
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 + ")");
140
141 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
142 textDelay.setText(Integer.toString(module.getSavedDelay()));
143 textDelay.setEnabled(true);
144
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
153 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
154 textHwDebounceTime.setText(Integer.toString(module.getSavedHwDebounceTime()));
155 textHwDebounceTime.setEnabled(true);
156
157 Button set = (Button)findViewById(R.id.set);
158 if (loaded) {
159 set.setEnabled(true);
160 } else {
161 set.setEnabled(false);
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 }
177
178 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
179 on_boot.setChecked(module.get_on_boot());
180 if (safe_to_load) {
181 on_boot.setEnabled(true);
182 } else {
183 on_boot.setEnabled(false);
184 }
185
186 CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce);
187 hw_debounce.setChecked(module.getSavedHwDebounce());
188 hw_debounce.setEnabled(true);
189 }
190
191 private void disableUI() {
192 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
193 textDelay.setEnabled(false);
194
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
201 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
202 textHwDebounceTime.setEnabled(false);
203
204 Button set = (Button)findViewById(R.id.set);
205 set.setEnabled(false);
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);
215
216 CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce);
217 hw_debounce.setEnabled(false);
218 }
219
220 public void loadModule(View view) {
221 disableUI();
222 if (!module.isLoaded()) {
223 module.loadModule();
224 }
225 updateUI();
226 }
227
228 public void unloadModule(View view) {
229 disableUI();
230 if (module.isLoaded()) {
231 module.unloadModule();
232 }
233 updateUI();
234 }
235
236 public void setValues(View view) {
237 disableUI();
238 if (!module.isLoaded()) {
239 module.loadModule();
240 }
241 module.setAllValues();
242 updateUI();
243 }
244
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
251 public void toggle_hw_debounce(View view) {
252 CheckBox hw_debounce = (CheckBox)view;
253
254 module.setSavedHwDebounce(hw_debounce.isChecked());
255 }
256
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 }
290 }
Impressum, Datenschutz