]> git.zerfleddert.de Git - ms2-fixes/blob - MS2Debounce/src/de/rmdir/ms2debounce/MS2Debounce.java
3763a65ac32aa0ca115c1862aef85cba3eda5946
[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 private boolean drive_inactive_en;
33
34 public MS2Debounce()
35 {
36 super();
37 module = new DebounceModuleHelper(this);
38 }
39
40 @Override
41 public void onCreate(Bundle savedInstanceState)
42 {
43 super.onCreate(savedInstanceState);
44
45 setContentView(R.layout.main);
46
47 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
48 textDelay.addTextChangedListener(new TextWatcher() {
49 @Override
50 public void afterTextChanged(Editable delay) {
51 if (delay.toString().length() > 0) {
52 module.setSavedDelay(Integer.parseInt(delay.toString()));
53 }
54 }
55
56 @Override
57 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
58 }
59
60 @Override
61 public void onTextChanged(CharSequence s, int start, int before, int count) {
62 }
63 });
64
65 EditText textSettle = (EditText)findViewById(R.id.settle_time);
66 textSettle.addTextChangedListener(new TextWatcher() {
67 @Override
68 public void afterTextChanged(Editable settle_time) {
69 if (settle_time.toString().length() > 0) {
70 module.setSavedSettle(Integer.parseInt(settle_time.toString()));
71 }
72 }
73
74 @Override
75 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
76 }
77
78 @Override
79 public void onTextChanged(CharSequence s, int start, int before, int count) {
80 }
81 });
82
83 EditText textPoll = (EditText)findViewById(R.id.poll_time);
84 textPoll.addTextChangedListener(new TextWatcher() {
85 @Override
86 public void afterTextChanged(Editable poll_time) {
87 if (poll_time.toString().length() > 0) {
88 module.setSavedPoll(Integer.parseInt(poll_time.toString()));
89 }
90 }
91
92 @Override
93 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
94 }
95
96 @Override
97 public void onTextChanged(CharSequence s, int start, int before, int count) {
98 }
99 });
100
101 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
102 textHwDebounceTime.addTextChangedListener(new TextWatcher() {
103 @Override
104 public void afterTextChanged(Editable hw_debounce_time) {
105 if (hw_debounce_time.toString().length() > 0) {
106 module.setSavedHwDebounceTime(Integer.parseInt(hw_debounce_time.toString()));
107 }
108 }
109
110 @Override
111 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
112 }
113
114 @Override
115 public void onTextChanged(CharSequence s, int start, int before, int count) {
116 }
117 });
118
119 updateUI();
120 }
121
122 private void updateUI() {
123 disableUI();
124
125 // Calling these is expensive, so cache the result...
126 loaded = module.isLoaded();
127 safe_to_load = module.is_safe_to_load();
128 debounce_delay = module.getDelay();
129 settle_time = module.getSettle();
130 poll_time = module.getPoll();
131 hw_debounce_en = module.getHwDebounce();
132 hw_debounce_time = module.getHwDebounceTime();
133 drive_inactive_en = module.getDriveInactive();
134
135 TextView text = (TextView)findViewById(R.id.text);
136 text.setText("Module loaded: " + loaded + "\n" +
137 "debounce_delay: " + debounce_delay + "ms\n" +
138 "settle_time: " + settle_time + "us\n" +
139 "poll_time: " + poll_time + "ms\n" +
140 "safe_to_load: " + safe_to_load + " (module loaded by this app)\n" +
141 "hw_debounce: " + (hw_debounce_en?"en":"dis") + "abled, " + ((hw_debounce_time+1)*31) + "us (" + hw_debounce_time + "), drive inactive: " + (drive_inactive_en?"en":"dis") + "abled");
142
143 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
144 textDelay.setText(Integer.toString(module.getSavedDelay()));
145 textDelay.setEnabled(true);
146
147 EditText textSettle = (EditText)findViewById(R.id.settle_time);
148 textSettle.setText(Integer.toString(module.getSavedSettle()));
149 textSettle.setEnabled(true);
150
151 EditText textPoll = (EditText)findViewById(R.id.poll_time);
152 textPoll.setText(Integer.toString(module.getSavedPoll()));
153 textPoll.setEnabled(true);
154
155 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
156 textHwDebounceTime.setText(Integer.toString(module.getSavedHwDebounceTime()));
157 textHwDebounceTime.setEnabled(true);
158
159 Button set = (Button)findViewById(R.id.set);
160 if (loaded) {
161 set.setEnabled(true);
162 } else {
163 set.setEnabled(false);
164 }
165
166 Button load = (Button)findViewById(R.id.load);
167 if (loaded) {
168 load.setEnabled(false);
169 } else {
170 load.setEnabled(true);
171 }
172
173 Button unload = (Button)findViewById(R.id.unload);
174 if (loaded) {
175 unload.setEnabled(true);
176 } else {
177 unload.setEnabled(false);
178 }
179
180 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
181 on_boot.setChecked(module.get_on_boot());
182 if (safe_to_load) {
183 on_boot.setEnabled(true);
184 } else {
185 on_boot.setEnabled(false);
186 }
187
188 CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce);
189 hw_debounce.setChecked(module.getSavedHwDebounce());
190 hw_debounce.setEnabled(true);
191
192 CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive);
193 drive_inactive.setChecked(module.getSavedDriveInactive());
194 drive_inactive.setEnabled(true);
195 }
196
197 private void disableUI() {
198 EditText textDelay = (EditText)findViewById(R.id.debounce_delay);
199 textDelay.setEnabled(false);
200
201 EditText textSettle = (EditText)findViewById(R.id.settle_time);
202 textSettle.setEnabled(false);
203
204 EditText textPoll = (EditText)findViewById(R.id.poll_time);
205 textPoll.setEnabled(false);
206
207 EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time);
208 textHwDebounceTime.setEnabled(false);
209
210 Button set = (Button)findViewById(R.id.set);
211 set.setEnabled(false);
212
213 Button load = (Button)findViewById(R.id.load);
214 load.setEnabled(false);
215
216 Button unload = (Button)findViewById(R.id.unload);
217 unload.setEnabled(false);
218
219 CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot);
220 on_boot.setEnabled(false);
221
222 CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce);
223 hw_debounce.setEnabled(false);
224
225 CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive);
226 drive_inactive.setEnabled(false);
227 }
228
229 public void loadModule(View view) {
230 disableUI();
231 if (!module.isLoaded()) {
232 module.loadModule();
233 }
234 updateUI();
235 }
236
237 public void unloadModule(View view) {
238 disableUI();
239 if (module.isLoaded()) {
240 module.unloadModule();
241 }
242 updateUI();
243 }
244
245 public void setValues(View view) {
246 disableUI();
247 if (!module.isLoaded()) {
248 module.loadModule();
249 }
250 module.setAllValues();
251 updateUI();
252 }
253
254 public void toggle_on_boot(View view) {
255 CheckBox on_boot = (CheckBox)view;
256
257 module.set_on_boot(on_boot.isChecked());
258 }
259
260 public void toggle_hw_debounce(View view) {
261 CheckBox hw_debounce = (CheckBox)view;
262
263 module.setSavedHwDebounce(hw_debounce.isChecked());
264 }
265
266 public void toggle_drive_inactive(View view) {
267 CheckBox drive_inactive = (CheckBox)view;
268
269 module.setSavedDriveInactive(drive_inactive.isChecked());
270 }
271
272 @Override
273 public boolean onCreateOptionsMenu(Menu menu) {
274 MenuInflater inflater = getMenuInflater();
275 inflater.inflate(R.menu.main, menu);
276 return true;
277 }
278
279 @Override
280 public boolean onOptionsItemSelected(MenuItem item) {
281 switch (item.getItemId()) {
282 case R.id.about:
283 showDialog(42);
284 return true;
285 default:
286 return super.onOptionsItemSelected(item);
287 }
288 }
289
290 protected Dialog onCreateDialog(int id) {
291 Dialog dlg = null;
292
293 AlertDialog.Builder about = new AlertDialog.Builder(this);
294 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")
295 .setCancelable(true)
296 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
297 public void onClick(DialogInterface dialog, int id) {
298 dialog.cancel();
299 }
300 });
301 dlg = about.create();
302
303 return dlg;
304 }
305 }
Impressum, Datenschutz