]>
Commit | Line | Data |
---|---|---|
7bcde8d7 MG |
1 | package de.rmdir.ms2debounce; |
2 | ||
3 | import android.app.Activity; | |
fd8f8652 MG |
4 | import android.app.Dialog; |
5 | import android.app.AlertDialog; | |
7bcde8d7 | 6 | import android.os.Bundle; |
ae569a50 | 7 | import android.content.Intent; |
fd8f8652 | 8 | import android.content.DialogInterface; |
ee6322a1 | 9 | import android.widget.TextView; |
c51c7009 | 10 | import android.widget.EditText; |
dea0f4b0 MG |
11 | import android.widget.Button; |
12 | import android.widget.CheckBox; | |
13 | import android.view.View; | |
fd8f8652 MG |
14 | import android.view.Menu; |
15 | import android.view.MenuInflater; | |
16 | import android.view.MenuItem; | |
1559225a MG |
17 | import android.text.TextWatcher; |
18 | import android.text.Editable; | |
7bcde8d7 MG |
19 | |
20 | public 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; | |
d002e66d | 32 | private boolean drive_inactive_en; |
e75481cd | 33 | |
dea0f4b0 MG |
34 | public MS2Debounce() |
35 | { | |
36 | super(); | |
37 | module = new DebounceModuleHelper(this); | |
38 | } | |
39 | ||
226a7d4d MG |
40 | @Override |
41 | public void onCreate(Bundle savedInstanceState) | |
42 | { | |
43 | super.onCreate(savedInstanceState); | |
ae569a50 | 44 | |
dea0f4b0 | 45 | setContentView(R.layout.main); |
1559225a MG |
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())); | |
381027a8 MG |
53 | } |
54 | } | |
1559225a | 55 | |
381027a8 MG |
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())); | |
1559225a MG |
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 | ||
2bb83a0e MG |
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 | ||
dea0f4b0 MG |
119 | updateUI(); |
120 | } | |
121 | ||
122 | private void updateUI() { | |
dea0f4b0 MG |
123 | disableUI(); |
124 | ||
01b288b5 | 125 | // Calling these is expensive, so cache the result... |
e75481cd MG |
126 | loaded = module.isLoaded(); |
127 | safe_to_load = module.is_safe_to_load(); | |
128 | debounce_delay = module.getDelay(); | |
381027a8 MG |
129 | settle_time = module.getSettle(); |
130 | poll_time = module.getPoll(); | |
2bb83a0e MG |
131 | hw_debounce_en = module.getHwDebounce(); |
132 | hw_debounce_time = module.getHwDebounceTime(); | |
d002e66d | 133 | drive_inactive_en = module.getDriveInactive(); |
dea0f4b0 | 134 | |
c51c7009 | 135 | TextView text = (TextView)findViewById(R.id.text); |
882feaf4 | 136 | text.setText("Module loaded: " + loaded + "\n" + |
381027a8 MG |
137 | "debounce_delay: " + debounce_delay + "ms\n" + |
138 | "settle_time: " + settle_time + "us\n" + | |
722ab95d | 139 | "poll_time: " + poll_time + "ms\n" + |
2bb83a0e | 140 | "safe_to_load: " + safe_to_load + " (module loaded by this app)\n" + |
d002e66d | 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"); |
dea0f4b0 | 142 | |
1559225a MG |
143 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
144 | textDelay.setText(Integer.toString(module.getSavedDelay())); | |
145 | textDelay.setEnabled(true); | |
c51c7009 | 146 | |
381027a8 MG |
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 | ||
2bb83a0e MG |
155 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); |
156 | textHwDebounceTime.setText(Integer.toString(module.getSavedHwDebounceTime())); | |
157 | textHwDebounceTime.setEnabled(true); | |
158 | ||
381027a8 MG |
159 | Button set = (Button)findViewById(R.id.set); |
160 | if (loaded) { | |
161 | set.setEnabled(true); | |
dea0f4b0 | 162 | } else { |
381027a8 | 163 | set.setEnabled(false); |
dea0f4b0 MG |
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 | } | |
226a7d4d | 179 | |
dea0f4b0 | 180 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); |
c3053460 | 181 | on_boot.setChecked(module.get_on_boot()); |
dea0f4b0 MG |
182 | if (safe_to_load) { |
183 | on_boot.setEnabled(true); | |
184 | } else { | |
185 | on_boot.setEnabled(false); | |
186 | } | |
2bb83a0e MG |
187 | |
188 | CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce); | |
189 | hw_debounce.setChecked(module.getSavedHwDebounce()); | |
190 | hw_debounce.setEnabled(true); | |
d002e66d MG |
191 | |
192 | CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive); | |
193 | drive_inactive.setChecked(module.getSavedDriveInactive()); | |
194 | drive_inactive.setEnabled(true); | |
dea0f4b0 MG |
195 | } |
196 | ||
197 | private void disableUI() { | |
1559225a MG |
198 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); |
199 | textDelay.setEnabled(false); | |
200 | ||
381027a8 MG |
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 | ||
2bb83a0e MG |
207 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); |
208 | textHwDebounceTime.setEnabled(false); | |
209 | ||
381027a8 MG |
210 | Button set = (Button)findViewById(R.id.set); |
211 | set.setEnabled(false); | |
dea0f4b0 MG |
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); | |
2bb83a0e MG |
221 | |
222 | CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce); | |
223 | hw_debounce.setEnabled(false); | |
d002e66d MG |
224 | |
225 | CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive); | |
226 | drive_inactive.setEnabled(false); | |
dea0f4b0 MG |
227 | } |
228 | ||
229 | public void loadModule(View view) { | |
230 | disableUI(); | |
226a7d4d MG |
231 | if (!module.isLoaded()) { |
232 | module.loadModule(); | |
233 | } | |
dea0f4b0 MG |
234 | updateUI(); |
235 | } | |
ae569a50 | 236 | |
dea0f4b0 MG |
237 | public void unloadModule(View view) { |
238 | disableUI(); | |
239 | if (module.isLoaded()) { | |
240 | module.unloadModule(); | |
241 | } | |
242 | updateUI(); | |
243 | } | |
ee6322a1 | 244 | |
381027a8 | 245 | public void setValues(View view) { |
dea0f4b0 | 246 | disableUI(); |
dea0f4b0 MG |
247 | if (!module.isLoaded()) { |
248 | module.loadModule(); | |
249 | } | |
381027a8 | 250 | module.setAllValues(); |
dea0f4b0 | 251 | updateUI(); |
226a7d4d | 252 | } |
fd8f8652 | 253 | |
c3053460 MG |
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 | ||
2bb83a0e MG |
260 | public void toggle_hw_debounce(View view) { |
261 | CheckBox hw_debounce = (CheckBox)view; | |
262 | ||
263 | module.setSavedHwDebounce(hw_debounce.isChecked()); | |
264 | } | |
265 | ||
d002e66d MG |
266 | public void toggle_drive_inactive(View view) { |
267 | CheckBox drive_inactive = (CheckBox)view; | |
268 | ||
269 | module.setSavedDriveInactive(drive_inactive.isChecked()); | |
270 | } | |
271 | ||
fd8f8652 MG |
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 | } | |
7bcde8d7 | 305 | } |