![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Testcode for LVC Robot club
SharpDigiDist100/SharpDigiDist100.cpp@1:fe52aa73cd6a, 2012-04-23 (annotated)
- Committer:
- jonmarsh
- Date:
- Mon Apr 23 09:32:50 2012 +0000
- Revision:
- 1:fe52aa73cd6a
- Parent:
- 0:f4922a2a0292
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jonmarsh | 0:f4922a2a0292 | 1 | #include "SharpDigiDist100.h" |
jonmarsh | 0:f4922a2a0292 | 2 | |
jonmarsh | 0:f4922a2a0292 | 3 | SharpDigiDist100::SharpDigiDist100(PinName pin):intin(pin),pinin(pin) |
jonmarsh | 0:f4922a2a0292 | 4 | { |
jonmarsh | 0:f4922a2a0292 | 5 | timer1.reset(); |
jonmarsh | 0:f4922a2a0292 | 6 | intin.rise(this,&SharpDigiDist100::onInt); |
jonmarsh | 0:f4922a2a0292 | 7 | intin.fall(this,&SharpDigiDist100::onInt); |
jonmarsh | 0:f4922a2a0292 | 8 | timer1.start(); |
jonmarsh | 0:f4922a2a0292 | 9 | //onInt(); |
jonmarsh | 0:f4922a2a0292 | 10 | current = Far; |
jonmarsh | 0:f4922a2a0292 | 11 | //last = NA; |
jonmarsh | 0:f4922a2a0292 | 12 | onChangeAttached = false; |
jonmarsh | 0:f4922a2a0292 | 13 | } |
jonmarsh | 0:f4922a2a0292 | 14 | |
jonmarsh | 0:f4922a2a0292 | 15 | void SharpDigiDist100::onInt() |
jonmarsh | 0:f4922a2a0292 | 16 | { |
jonmarsh | 0:f4922a2a0292 | 17 | timer1.stop(); |
jonmarsh | 0:f4922a2a0292 | 18 | last = current; |
jonmarsh | 0:f4922a2a0292 | 19 | int timeFromLast = timer1.read_ms(); |
jonmarsh | 0:f4922a2a0292 | 20 | if (timeFromLast < 100) |
jonmarsh | 0:f4922a2a0292 | 21 | { |
jonmarsh | 0:f4922a2a0292 | 22 | current = Far; |
jonmarsh | 0:f4922a2a0292 | 23 | timeout.attach(this,&SharpDigiDist100::onInt, 0.3); |
jonmarsh | 0:f4922a2a0292 | 24 | } |
jonmarsh | 0:f4922a2a0292 | 25 | else |
jonmarsh | 0:f4922a2a0292 | 26 | { |
jonmarsh | 0:f4922a2a0292 | 27 | if(pinin) |
jonmarsh | 0:f4922a2a0292 | 28 | { |
jonmarsh | 0:f4922a2a0292 | 29 | current = Mid; |
jonmarsh | 0:f4922a2a0292 | 30 | } |
jonmarsh | 0:f4922a2a0292 | 31 | else |
jonmarsh | 0:f4922a2a0292 | 32 | { |
jonmarsh | 0:f4922a2a0292 | 33 | current = Near; |
jonmarsh | 0:f4922a2a0292 | 34 | } |
jonmarsh | 0:f4922a2a0292 | 35 | } |
jonmarsh | 0:f4922a2a0292 | 36 | if(current != last) |
jonmarsh | 0:f4922a2a0292 | 37 | { |
jonmarsh | 0:f4922a2a0292 | 38 | if(onChangeAttached) |
jonmarsh | 0:f4922a2a0292 | 39 | { |
jonmarsh | 0:f4922a2a0292 | 40 | onChange(); |
jonmarsh | 0:f4922a2a0292 | 41 | } |
jonmarsh | 0:f4922a2a0292 | 42 | } |
jonmarsh | 0:f4922a2a0292 | 43 | timer1.reset(); |
jonmarsh | 0:f4922a2a0292 | 44 | timer1.start(); |
jonmarsh | 0:f4922a2a0292 | 45 | } |
jonmarsh | 0:f4922a2a0292 | 46 | |
jonmarsh | 0:f4922a2a0292 | 47 | int SharpDigiDist100::getDistance() |
jonmarsh | 0:f4922a2a0292 | 48 | { |
jonmarsh | 0:f4922a2a0292 | 49 | return current; |
jonmarsh | 0:f4922a2a0292 | 50 | } |
jonmarsh | 0:f4922a2a0292 | 51 | |
jonmarsh | 0:f4922a2a0292 | 52 | void SharpDigiDist100::attachOnChange(void (*ptr) (void)) |
jonmarsh | 0:f4922a2a0292 | 53 | { |
jonmarsh | 0:f4922a2a0292 | 54 | onChange = ptr; |
jonmarsh | 0:f4922a2a0292 | 55 | onChangeAttached = true; |
jonmarsh | 0:f4922a2a0292 | 56 | } |