Project

General

Profile

Revision 335

Added by James Kong over 16 years ago

Rewrote bom.c and bom.h for the new analog8 code and for compatibility with bom 1.5 and
rbom. BOM values are now buffered and updating them will require a call to bom_refresh.
Old bom function calls have been left for backwards compatibility, but are now
deprecated. See comments for appropriate use. BOM 1.5 and rbom sections have not been
implemented yet.

Fixed some analog bugs. Made some analog functions static.

May not compile! Not tested!

View differences:

branches/analog/trunk/code/projects/libdragonfly/analog.c
48 48
 * @{
49 49
 **/
50 50

  
51
int adc_loop_running = 0;
52
int adc_current_port = 0;
53
adc_t an_val[10];
51
static int adc_loop_running = 0;
52
static int adc_current_port = 0;
53
static adc_t an_val[10];
54 54

  
55 55
/**
56 56
 * Initializes the ADC.
......
230 230
 *
231 231
 * @see analog_init
232 232
 **/
233
void set_adc_mux(int which)
233
static void set_adc_mux(int which)
234 234
{  
235 235
  // mask so only proper bits are possible.  
236 236
  PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04);
branches/analog/trunk/code/projects/libdragonfly/bom.c
39 39
#include "analog.h"
40 40

  
41 41
//constants
42
const int lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
42
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
43 43

  
44
// internal function prototypes
45
void output_high(int which);
46
void output_low(int which);
44
// internal function prototypes
45
static void output_high(which);
46
static void output_low(which);
47
static void bom_select(char which);
47 48

  
48 49
/*
49 50
 Bk R Y (Analog)
......
77 78
#define MONK3 _PIN_E6     //blue
78 79
#define MONK2 _PIN_E7     //white
79 80

  
80
#define BOM_VALUE_THRESHOLD 200
81
#define BOM_VALUE_THRESHOLD 200
82
#define NUM_BOM_LEDS 16
81 83

  
82 84

  
83 85
/**
......
93 95
 * Include bom.h to access these functions.
94 96
 *
95 97
 * @{
96
 **/
98
 **/
99

  
100
static unsigned int bom_val[NUM_BOM_LEDS];
101
static char bom_type = BOM;
102
  
103
/**
104
 * Initializes the BOM.
105
 * Call bom_init before reading bom values or turning bom leds.
106
 *
107
 * @bugs INCOMPLETE - need to fill in init routine for BOM15
108
 * 
109
 * @see bom_refresh, bom_leds_on, bom_leds_off
110
 **/
111
void bom_init(char type) {
112
    bom_type = type;
113
    
114
    switch(bom_type) {
115
    case BOM:
116
        break;
117
    case BOM15:
118
        break;
119
    case RBOM:
120
        break;
121
    default:
122
    }
123
}
124

  
125
/**
126
 * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
127
 *    and updates the corresponding bom value with an analog_get8 reading.  analog_init and bom_init
128
 *    must be called for this to work.
129
 *
130
 *
131
 * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
132
 *    Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
133
 *
134
 * @see bom_get
135
 **/
136
void bom_refresh(int bit_field) {
137
    int i;
138
    
139
    analog_stop_loop();
140
    
141
    for(i = 0; i < NUM_BOM_LEDS; i++) {
142
        if(bit_field & 0x1) {
143
            bom_select(i);
144
            bom_val[i] = analog_get8(MONKI);
145
        }
146
        bit_field = bitfield >> 1;
147
    }
148
    
149
    analog_start_loop();
150
}
151

  
152
/**
153
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
154
 *
155
 * @param which which bom value to return
156
 *
157
 * @return the bom value
158
 *
159
 * see bom_refresh
160
 **/
161
int bom_get(char which) {
162
    return bom_val[which];
163
}
164

  
165
/** 
166
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
167
 *
168
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
169
 *    BOM_VALUE_THRESHOLD
170
 **/
171
int bom_get_max(void) {
172
    int i, lowest_val, lowest_i;
173
    
174
    lowest_val = bom_val[0];
175
    lowest_i = 0;
176
    for(i = 1; i < NUM_BOM_LEDS; i++) {
177
        if(bom_val[i] < lowest) {
178
            lowest_val = bom_val[i];
179
            lowest_i = i;
180
        }
181
    }
182
    
183
    if(lowest_val < BOM_VALUE_THRESHOLD)
184
        return lowest_i;
185
    else
186
        return -1;
187
}
188

  
189
/**
190
 * Iterates through each bit in the bit_field. For each set bit, turns on the corresponding bom led.
191
 *    bom_init must be called for this to work. Only works with BOM_ALL if using the original bom.
192
 *
193
 * @param bit_field specifies which leds should be turned on.  Use BOM_ALL to turn on all bom leds.
194
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
195
 **/
196
void bom_leds_on(int bit_field) {
197
    switch(bom_type) {
198
    case BOM:
199
        if(bit_field == BOM_ALL) {
200
            digital_output(MONKL, 1);
201
        }
202
        break;
203
    case BOM15:
204
        //add bom 1.5 code here
205
        break;
206
    case RBOM:
207
        //add rbom code here
208
        break;
209
}
210

  
211
/**
212
 * Iterates through each bit in the bit_field. For each set bit, turns off the corresponding bom led.
213
 *    bom_init must be called for this to work. Only works with BOM_ALL if using the original bom.
214
 *
215
 * @param bit_field specifies which leds should be turned off.  Use BOM_ALL to turn off all bom leds.
216
 *    Ex. if 0x000B is passed, leds 0 and 3 will be turned off.
217
 **/
218
void bom_leds_off(int bit_field) {
219
    switch(bom_type) {
220
    case BOM:
221
        if(bit_field == BOM_ALL) {
222
            digital_output(MONKL, 0);
223
        }
224
        break;
225
    case BOM15:
226
        //add bom 1.5 code here
227
        break;
228
    case RBOM:
229
        //add rbom code here
230
        break;
231
}
97 232

  
233

  
98 234
/**
99
 * Returns the direction of the maximum BOM reading,
235
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
100 236
 * as an integer in the range 0-15. 0 indicates to the
101 237
 * robot's right, while the rest of the sensors are
102 238
 * numbered counterclockwise. This is useful for determining
......
166 302
}
167 303

  
168 304
/**
169
 * Flashes the BOM. analog_init must be called before this
170
 * function can be used.
305
 * (DEPRECATED) Turns on all bom leds.
171 306
 * 
172
 * @see bom_off, analog_init
307
 * @see bom_off
173 308
 **/
174 309
void bom_on(void)
175 310
{
......
177 312
}
178 313

  
179 314
/**
180
 * Stops flashing the BOM. analog_init must be called
181
 * before this function can be used.
315
 * (DEPRECATED) Turns off all bom leds.
182 316
 * 
183
 * @see bom_on, analog_init
317
 * @see bom_on
184 318
 **/
185 319
void bom_off(void)
186 320
{
......
196 330
void output_low(int which) {
197 331
	digital_output(which, 0);
198 332
}
333
+static void bom_select(char which) {
334
      if (which&8)
335
        digital_output(MONK3, 1);
336
      else
337
        digital_output(MONK3, 0);
199 338

  
339
      if (which&4)
340
        digital_output(MONK2, 1);
341
      else
342
        digital_output(MONK2, 0);
343

  
344
      if (which&2)
345
        digital_output(MONK1, 1);
346
      else
347
        digital_output(MONK1, 0);
348

  
349
      if (which&1)
350
        digital_output(MONK0, 1);
351
      else
352
        digital_output(MONK0, 0);
353
}
branches/analog/trunk/code/projects/libdragonfly/bom.h
42 42
 * @addtogroup bom
43 43
 * @{
44 44
 **/
45
 
46
/** @brief Include all elements in the 16-bit bitfield **/
47
#define BOM_ALL 0xFFFF
48
/** @brief Original BOM - No Range, No Individual LED control **/
49
#define BOM     0
50
/** @brief BOM 1.5 - No Range, Individual LED control **/
51
#define BOM15   1
52
/** @brief RBOM - Range, Individual LED control **/
53
#define RBOM    2
54
 
55
/** @brief Initialize the bom according to bom type **/
56
void bom_init(char type);
57
/** @brief Refresh bom_val[] with new values from analog8.  analog_init and bom_init must be called for this to work. **/
58
void bom_refresh(int bit_field);
59
/** @brief Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values. **/
60
int bom_get(char which);
61
/** @brief Compares all the values in bom_val[] and returns the index to the highest value element. **/
62
int bom_get_max(void);
63
/** @brief Turns on the selected bom leds. Only works with BOM_ALL if using the original bom. **/
64
void bom_leds_on(int bit_field);
65
/** @brief Turns off the selected bom leds. Only works with BOM_ALL if using the original bom. **/
66
void bom_leds_off(int bit_field);
45 67

  
46
/** @brief Returns the location of the maximum BOM reading. **/
47
int get_max_bom( void );
48
/** @brief Turns the BOM on. **/
68
/** @brief (DEPRECATED) Gets and compares all bom values.  Returns the index to the highest value element. **/
69
int get_max_bom(void);
70
/** @brief (DEPRECATED) Turns on all bom leds. **/
49 71
void bom_on(void);
50
/** @brief Turns the BOM off. **/
72
/** @brief (DEPRECATED) Turns off all bom leds. **/
51 73
void bom_off(void);
52 74

  
53 75
/** @} **/

Also available in: Unified diff