Revision 1461
updated all the library code to have sensible _init behavior.
Almost all of the library components have a global variable which gets set after init and the functions inside will fail with an error code if init has not been called. Also, the init functions themselves check this variable and will bail out without doing any damage if that init has already been called
rangefinder.c | ||
---|---|---|
63 | 63 |
#include "analog.h" |
64 | 64 |
#include "dio.h" |
65 | 65 |
|
66 |
unsigned char range_initd=0; |
|
67 |
|
|
68 |
|
|
66 | 69 |
/* |
67 | 70 |
read_distance returns the 8-bit reading from the rangefinder |
68 | 71 |
parameters: |
... | ... | |
112 | 115 |
* Initializes the rangefinders. This must be called before |
113 | 116 |
* range_read_distance. |
114 | 117 |
* |
118 |
* @return 0 if init succesfull, an error code otherwise |
|
119 |
* |
|
115 | 120 |
* @see range_read_distance |
116 | 121 |
**/ |
117 |
void range_init(void)
|
|
122 |
int range_init(void)
|
|
118 | 123 |
{ |
124 |
if(range_initd) |
|
125 |
return ERROR_INIT_ALREADY_INITD; |
|
126 |
|
|
119 | 127 |
digital_output(_PIN_B4,0); |
128 |
|
|
129 |
range_initd=1; |
|
130 |
return 0; |
|
120 | 131 |
} |
121 | 132 |
|
122 | 133 |
/** |
... | ... | |
126 | 137 |
* @param range_id the rangefinder to use. This should be one |
127 | 138 |
* of the constants IR1 - IR5. |
128 | 139 |
* |
129 |
* @return the distance measured by the rangefinder |
|
140 |
* @return the distance measured by the rangefinder, or an error code
|
|
130 | 141 |
* |
131 | 142 |
* @see range_init |
132 | 143 |
**/ |
133 | 144 |
int range_read_distance(int range_id) { |
145 |
if(!range_initd) |
|
146 |
return -3; |
|
147 |
|
|
134 | 148 |
return linearize_distance(analog8(range_id)); |
135 | 149 |
} |
136 | 150 |
|
Also available in: Unified diff