Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Committer:
wim
Date:
Fri Aug 26 20:35:11 2011 +0000
Revision:
5:38b853bb1afa
Parent:
2:1dab1089c332
Included Messages to Host

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 2:1dab1089c332 1 /* Status_Display - LED Status Display control
wim 2:1dab1089c332 2 * Copyright (c) 2011 Wim Huiskamp
wim 2:1dab1089c332 3 *
wim 2:1dab1089c332 4 * Released under the MIT License: http://mbed.org/license/mit
wim 2:1dab1089c332 5 *
wim 2:1dab1089c332 6 * version 0.2 Initial Release
wim 2:1dab1089c332 7 */
wim 2:1dab1089c332 8 #include "mbed.h"
wim 2:1dab1089c332 9 #include "PCF8574_DataBus.h"
wim 2:1dab1089c332 10 #include "PCF8574_EnableBus.h"
wim 2:1dab1089c332 11 #include "MBED_ControlBus.h"
wim 2:1dab1089c332 12 #include "Status_Display.h"
wim 2:1dab1089c332 13
wim 2:1dab1089c332 14
wim 2:1dab1089c332 15 /** Create a Status_Display object connected to the specified buses
wim 2:1dab1089c332 16 *
wim 2:1dab1089c332 17 * @param PCF8574_DataBus data databus to connect to
wim 2:1dab1089c332 18 * @param PCF8574_EnableBus enable enablebus to connect to
wim 2:1dab1089c332 19 * @param MBED_ControlBus control controlbus to connect to
wim 2:1dab1089c332 20 */
wim 2:1dab1089c332 21 Status_Display::Status_Display (PCF8574_DataBus &databus, PCF8574_EnableBus &enablebus, MBED_ControlBus &controlbus) :
wim 2:1dab1089c332 22 _databus(databus), _enablebus(enablebus), _controlbus(controlbus) {
wim 2:1dab1089c332 23 _init();
wim 2:1dab1089c332 24 }
wim 2:1dab1089c332 25
wim 2:1dab1089c332 26 /** Init Status_Display
wim 2:1dab1089c332 27 * @param
wim 2:1dab1089c332 28 * @returns
wim 2:1dab1089c332 29 *
wim 2:1dab1089c332 30 * @brief
wim 2:1dab1089c332 31 */
wim 2:1dab1089c332 32 void Status_Display::_init(void)
wim 2:1dab1089c332 33 {
wim 2:1dab1089c332 34 // Apply reset
wim 2:1dab1089c332 35 // reset(); // Note that this would also reset the Alphanumeric display.
wim 2:1dab1089c332 36
wim 2:1dab1089c332 37 _latch1 = 0x00; // Make sure that all LEDs are disabled
wim 2:1dab1089c332 38 _latch2 = 0x00; // Make sure that all LEDs are disabled
wim 2:1dab1089c332 39 _brightness = D_STATUS_LED_BRIGHT_DEF; // Default Brightness
wim 2:1dab1089c332 40
wim 2:1dab1089c332 41 _write_latch1(); // Write Status LED
wim 2:1dab1089c332 42 _write_latch2(); // Write Status LED
wim 2:1dab1089c332 43 _write_latchbrightness(); // Write Brightness
wim 2:1dab1089c332 44
wim 2:1dab1089c332 45 NoGo (LED_OFF); // NoGo Off (separate control)
wim 2:1dab1089c332 46 }
wim 2:1dab1089c332 47
wim 2:1dab1089c332 48
wim 2:1dab1089c332 49 /** Set or Reset LED lamptest, note that current LED state is lost
wim 2:1dab1089c332 50 *
wim 2:1dab1089c332 51 * @param LED_State led_state
wim 2:1dab1089c332 52 *
wim 2:1dab1089c332 53 * @brief
wim 2:1dab1089c332 54 */
wim 2:1dab1089c332 55 void Status_Display::lamptest (LED_State led_state) {
wim 2:1dab1089c332 56 int result = 1;
wim 2:1dab1089c332 57
wim 2:1dab1089c332 58 if (led_state == LED_ON) {
wim 2:1dab1089c332 59 // Status LEDs controlled by Latch1
wim 2:1dab1089c332 60 _latch1 = D_STATUS_LED_MULT | D_STATUS_LED_LASER | D_STATUS_LED_BATT | D_STATUS_LED_TEMP | D_STATUS_LED_ENRGY;
wim 2:1dab1089c332 61 // Backlight On
wim 2:1dab1089c332 62 _latch1 |= D_STATUS_BACKLIGHT;
wim 2:1dab1089c332 63
wim 2:1dab1089c332 64 // Status LEDs controlled by Latch2
wim 2:1dab1089c332 65 _latch2 = D_STATUS_LED_CODE | D_STATUS_LED_RANGE | D_STATUS_LED_DESIG | D_STATUS_LED_ADDR | D_STATUS_LED_FREQ | D_STATUS_LED_PATH;
wim 2:1dab1089c332 66
wim 2:1dab1089c332 67 // NoGo On (separate control)
wim 2:1dab1089c332 68 NoGo (LED_ON);
wim 2:1dab1089c332 69 }
wim 2:1dab1089c332 70 else {
wim 2:1dab1089c332 71 _latch1 = 0x00; // Make sure that all LEDs are disabled
wim 2:1dab1089c332 72 _latch2 = 0x00; // Make sure that all LEDs are disabled
wim 2:1dab1089c332 73
wim 2:1dab1089c332 74 // NoGo Off (separate control)
wim 2:1dab1089c332 75 NoGo (LED_OFF);
wim 2:1dab1089c332 76 }
wim 2:1dab1089c332 77
wim 2:1dab1089c332 78 // Write LED status
wim 2:1dab1089c332 79 _write_latch1();
wim 2:1dab1089c332 80 _write_latch2();
wim 2:1dab1089c332 81 }
wim 2:1dab1089c332 82
wim 2:1dab1089c332 83
wim 2:1dab1089c332 84
wim 2:1dab1089c332 85 /** Set or Reset Status LEDs
wim 2:1dab1089c332 86 *
wim 2:1dab1089c332 87 * @param LED_Pin led_pin
wim 2:1dab1089c332 88 * @param LED_State led_state
wim 2:1dab1089c332 89 *
wim 2:1dab1089c332 90 * @brief
wim 2:1dab1089c332 91 */
wim 2:1dab1089c332 92 void Status_Display::LED (LED_Pin led_pin, LED_State led_state) {
wim 2:1dab1089c332 93 int result = 1;
wim 2:1dab1089c332 94
wim 2:1dab1089c332 95 switch (led_pin) {
wim 2:1dab1089c332 96 // Status LEDs controlled by Latch1
wim 2:1dab1089c332 97 case LED_MULT : if (led_state == LED_ON)
wim 2:1dab1089c332 98 _latch1 |= D_STATUS_LED_MULT; // LED Pin High
wim 2:1dab1089c332 99 else
wim 2:1dab1089c332 100 _latch1 &= ~D_STATUS_LED_MULT; // LED Pin Low
wim 2:1dab1089c332 101 break;
wim 2:1dab1089c332 102 case LED_LASER : if (led_state == LED_ON)
wim 2:1dab1089c332 103 _latch1 |= D_STATUS_LED_LASER; // LED Pin High
wim 2:1dab1089c332 104 else
wim 2:1dab1089c332 105 _latch1 &= ~D_STATUS_LED_LASER; // LED Pin Low
wim 2:1dab1089c332 106 break;
wim 2:1dab1089c332 107 case LED_BATT : if (led_state == LED_ON)
wim 2:1dab1089c332 108 _latch1 |= D_STATUS_LED_BATT; // LED Pin High
wim 2:1dab1089c332 109 else
wim 2:1dab1089c332 110 _latch1 &= ~D_STATUS_LED_BATT; // LED Pin Low
wim 2:1dab1089c332 111 break;
wim 2:1dab1089c332 112 case LED_TEMP : if (led_state == LED_ON)
wim 2:1dab1089c332 113 _latch1 |= D_STATUS_LED_TEMP; // LED Pin High
wim 2:1dab1089c332 114 else
wim 2:1dab1089c332 115 _latch1 &= ~D_STATUS_LED_TEMP; // LED Pin Low
wim 2:1dab1089c332 116 break;
wim 2:1dab1089c332 117 case LED_ENRGY : if (led_state == LED_ON)
wim 2:1dab1089c332 118 _latch1 |= D_STATUS_LED_ENRGY; // LED Pin High
wim 2:1dab1089c332 119 else
wim 2:1dab1089c332 120 _latch1 &= ~D_STATUS_LED_ENRGY; // LED Pin Low
wim 2:1dab1089c332 121 break;
wim 2:1dab1089c332 122
wim 2:1dab1089c332 123 // Status LEDs controlled by Latch2
wim 2:1dab1089c332 124 case LED_CODE : if (led_state == LED_ON)
wim 2:1dab1089c332 125 _latch2 |= D_STATUS_LED_CODE; // LED Pin High
wim 2:1dab1089c332 126 else
wim 2:1dab1089c332 127 _latch2 &= ~D_STATUS_LED_CODE; // LED Pin Low
wim 2:1dab1089c332 128 break;
wim 2:1dab1089c332 129 case LED_RANGE : if (led_state == LED_ON)
wim 2:1dab1089c332 130 _latch2 |= D_STATUS_LED_RANGE; // LED Pin High
wim 2:1dab1089c332 131 else
wim 2:1dab1089c332 132 _latch2 &= ~D_STATUS_LED_RANGE; // LED Pin Low
wim 2:1dab1089c332 133 break;
wim 2:1dab1089c332 134 case LED_DESIG : if (led_state == LED_ON)
wim 2:1dab1089c332 135 _latch2 |= D_STATUS_LED_DESIG; // LED Pin High
wim 2:1dab1089c332 136 else
wim 2:1dab1089c332 137 _latch2 &= ~D_STATUS_LED_DESIG; // LED Pin Low
wim 2:1dab1089c332 138 break;
wim 2:1dab1089c332 139 case LED_ADDR : if (led_state == LED_ON)
wim 2:1dab1089c332 140 _latch2 |= D_STATUS_LED_ADDR; // LED Pin High
wim 2:1dab1089c332 141 else
wim 2:1dab1089c332 142 _latch2 &= ~D_STATUS_LED_ADDR; // LED Pin Low
wim 2:1dab1089c332 143 break;
wim 2:1dab1089c332 144 case LED_FREQ : if (led_state == LED_ON)
wim 2:1dab1089c332 145 _latch2 |= D_STATUS_LED_FREQ; // LED Pin High
wim 2:1dab1089c332 146 else
wim 2:1dab1089c332 147 _latch2 &= ~D_STATUS_LED_FREQ; // LED Pin Low
wim 2:1dab1089c332 148 break;
wim 2:1dab1089c332 149 case LED_PATH : if (led_state == LED_ON)
wim 2:1dab1089c332 150 _latch2 |= D_STATUS_LED_PATH; // LED Pin High
wim 2:1dab1089c332 151 else
wim 2:1dab1089c332 152 _latch2 &= ~D_STATUS_LED_PATH; // LED Pin Low
wim 2:1dab1089c332 153 break;
wim 2:1dab1089c332 154
wim 2:1dab1089c332 155 default: // Oops, we should never end up here....
wim 2:1dab1089c332 156 result = -1;
wim 2:1dab1089c332 157 }
wim 2:1dab1089c332 158
wim 2:1dab1089c332 159 _write_latch1(); // Write LED status
wim 2:1dab1089c332 160 _write_latch2();
wim 2:1dab1089c332 161 }
wim 2:1dab1089c332 162
wim 2:1dab1089c332 163
wim 2:1dab1089c332 164 /** Set or Clear the NoGo LED
wim 2:1dab1089c332 165 *
wim 2:1dab1089c332 166 * @param LED_State nogo_state
wim 2:1dab1089c332 167 *
wim 2:1dab1089c332 168 * @brief
wim 2:1dab1089c332 169 */
wim 2:1dab1089c332 170 void Status_Display::NoGo(LED_State NoGo_state) {
wim 2:1dab1089c332 171 // Note that the NoGo LED has a separate pin and is directly connected to the Enable bus
wim 2:1dab1089c332 172 // The LED is controlled from this class because it is more convenient and logical
wim 2:1dab1089c332 173
wim 2:1dab1089c332 174 if (NoGo_state == LED_ON) {
wim 5:38b853bb1afa 175 _enablebus.nogo(LOW); // NoGo LED On
wim 2:1dab1089c332 176 }
wim 2:1dab1089c332 177 else {
wim 5:38b853bb1afa 178 _enablebus.nogo(HIGH); // NoGo LED Off
wim 2:1dab1089c332 179 }
wim 2:1dab1089c332 180
wim 2:1dab1089c332 181 }
wim 2:1dab1089c332 182
wim 2:1dab1089c332 183 /** Set or Clear the Backlight
wim 2:1dab1089c332 184 *
wim 2:1dab1089c332 185 * @param LED_State led_state
wim 2:1dab1089c332 186 *
wim 2:1dab1089c332 187 * @brief
wim 2:1dab1089c332 188 */
wim 2:1dab1089c332 189 void Status_Display::backlight (LED_State backlight_state) {
wim 2:1dab1089c332 190
wim 2:1dab1089c332 191 if (backlight_state == LED_ON) {
wim 2:1dab1089c332 192 _latch1 |= D_STATUS_BACKLIGHT; // Backlight On
wim 2:1dab1089c332 193 }
wim 2:1dab1089c332 194 else {
wim 2:1dab1089c332 195 _latch1 &= ~D_STATUS_BACKLIGHT; // Backlight Off
wim 2:1dab1089c332 196 }
wim 2:1dab1089c332 197
wim 2:1dab1089c332 198 _write_latch1(); // Write Backlight state
wim 2:1dab1089c332 199 }
wim 2:1dab1089c332 200
wim 2:1dab1089c332 201
wim 2:1dab1089c332 202 /** Set Backlight and Status LEDs Brightness
wim 2:1dab1089c332 203 *
wim 2:1dab1089c332 204 * @param uint8_t brightness
wim 2:1dab1089c332 205 *
wim 2:1dab1089c332 206 * @brief
wim 2:1dab1089c332 207 */
wim 2:1dab1089c332 208 void Status_Display::set_brightness (uint8_t brightness) {
wim 2:1dab1089c332 209
wim 2:1dab1089c332 210 _brightness = brightness & STATUS_LED_BRIGHT_MASK;
wim 2:1dab1089c332 211
wim 2:1dab1089c332 212 _write_latchbrightness(); // Write Brightness value
wim 2:1dab1089c332 213 }
wim 2:1dab1089c332 214
wim 2:1dab1089c332 215
wim 2:1dab1089c332 216 /** Set Backlight and Status LEDs Brightness
wim 2:1dab1089c332 217 *
wim 2:1dab1089c332 218 * @param Brightness brightness
wim 2:1dab1089c332 219 *
wim 2:1dab1089c332 220 * @brief
wim 2:1dab1089c332 221 */
wim 2:1dab1089c332 222 void Status_Display::set_brightness (Brightness brightness) {
wim 2:1dab1089c332 223
wim 2:1dab1089c332 224 switch (brightness) {
wim 2:1dab1089c332 225 case BRT_OFF : set_brightness(D_STATUS_LED_BRIGHT_OFF);
wim 2:1dab1089c332 226 break;
wim 2:1dab1089c332 227 case BRT_LOW : set_brightness(D_STATUS_LED_BRIGHT_LOW);
wim 2:1dab1089c332 228 break;
wim 2:1dab1089c332 229 case BRT_MED : set_brightness(D_STATUS_LED_BRIGHT_MED);
wim 2:1dab1089c332 230 break;
wim 2:1dab1089c332 231 case BRT_HIGH : set_brightness(D_STATUS_LED_BRIGHT_HGH);
wim 2:1dab1089c332 232 break;
wim 2:1dab1089c332 233 default: // Oops, we should never end up here....
wim 2:1dab1089c332 234 set_brightness(D_STATUS_LED_BRIGHT_DEF);
wim 2:1dab1089c332 235 }
wim 2:1dab1089c332 236
wim 2:1dab1089c332 237 }
wim 2:1dab1089c332 238
wim 2:1dab1089c332 239
wim 2:1dab1089c332 240 /** Write Latch1 value
wim 2:1dab1089c332 241 *
wim 2:1dab1089c332 242 * @param none
wim 2:1dab1089c332 243 *
wim 2:1dab1089c332 244 * @brief
wim 2:1dab1089c332 245 */
wim 2:1dab1089c332 246 void Status_Display::_write_latch1()
wim 2:1dab1089c332 247 {
wim 2:1dab1089c332 248 // // Switch databus buffer to outputs
wim 2:1dab1089c332 249 // _controlbus.busdir(WRITE);
wim 2:1dab1089c332 250 // // Switch databus to outputs
wim 2:1dab1089c332 251 // _databus.busdir(WRITE);
wim 2:1dab1089c332 252
wim 2:1dab1089c332 253 // Set CE low and wait
wim 2:1dab1089c332 254 _enablebus.chipselect(LATCHEN_1, LOW);
wim 2:1dab1089c332 255 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 256
wim 2:1dab1089c332 257 // Write data to the databus
wim 2:1dab1089c332 258 _databus.write(_latch1);
wim 2:1dab1089c332 259
wim 2:1dab1089c332 260 // Set WR low, wait, then set high and wait
wim 2:1dab1089c332 261 _controlbus.WR(LOW);
wim 2:1dab1089c332 262 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 263 _controlbus.WR(HIGH);
wim 2:1dab1089c332 264 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 265
wim 2:1dab1089c332 266
wim 2:1dab1089c332 267 // Set CE high and wait
wim 2:1dab1089c332 268 _enablebus.chipselect(LATCHEN_1, HIGH);
wim 2:1dab1089c332 269 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 270
wim 2:1dab1089c332 271
wim 2:1dab1089c332 272 // // Switch databus back to inputs
wim 2:1dab1089c332 273 // _databus.busdir(READ);
wim 2:1dab1089c332 274 // // Switch databus buffer back to inputs
wim 2:1dab1089c332 275 // _controlbus.busdir(READ);
wim 2:1dab1089c332 276
wim 2:1dab1089c332 277 }
wim 2:1dab1089c332 278
wim 2:1dab1089c332 279
wim 2:1dab1089c332 280 /** Write Latch2 value
wim 2:1dab1089c332 281 *
wim 2:1dab1089c332 282 * @param none
wim 2:1dab1089c332 283 *
wim 2:1dab1089c332 284 * @brief
wim 2:1dab1089c332 285 */
wim 2:1dab1089c332 286 void Status_Display::_write_latch2()
wim 2:1dab1089c332 287 {
wim 2:1dab1089c332 288 // // Switch databus buffer to outputs
wim 2:1dab1089c332 289 // _controlbus.busdir(WRITE);
wim 2:1dab1089c332 290 // // Switch databus to outputs
wim 2:1dab1089c332 291 // _databus.busdir(WRITE);
wim 2:1dab1089c332 292
wim 2:1dab1089c332 293 // Set CE low and wait
wim 2:1dab1089c332 294 _enablebus.chipselect(LATCHEN_2, LOW);
wim 2:1dab1089c332 295 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 296
wim 2:1dab1089c332 297 // Write data to the databus
wim 2:1dab1089c332 298 _databus.write(_latch2);
wim 2:1dab1089c332 299
wim 2:1dab1089c332 300 // Set WR low, wait, then set high and wait
wim 2:1dab1089c332 301 _controlbus.WR(LOW);
wim 2:1dab1089c332 302 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 303 _controlbus.WR(HIGH);
wim 2:1dab1089c332 304 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 305
wim 2:1dab1089c332 306 // Set CE high and wait
wim 2:1dab1089c332 307 _enablebus.chipselect(LATCHEN_2, HIGH);
wim 2:1dab1089c332 308 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 309
wim 2:1dab1089c332 310
wim 2:1dab1089c332 311 // // Switch databus back to inputs
wim 2:1dab1089c332 312 // _databus.busdir(READ);
wim 2:1dab1089c332 313 // // Switch databus buffer back to inputs
wim 2:1dab1089c332 314 // _controlbus.busdir(READ);
wim 2:1dab1089c332 315
wim 2:1dab1089c332 316 }
wim 2:1dab1089c332 317
wim 2:1dab1089c332 318 /** Write Brightness value
wim 2:1dab1089c332 319 *
wim 2:1dab1089c332 320 * @param none
wim 2:1dab1089c332 321 *
wim 2:1dab1089c332 322 * @brief
wim 2:1dab1089c332 323 */
wim 2:1dab1089c332 324 void Status_Display::_write_latchbrightness()
wim 2:1dab1089c332 325 {
wim 2:1dab1089c332 326 // // Switch databus buffer to outputs
wim 2:1dab1089c332 327 // _controlbus.busdir(WRITE);
wim 2:1dab1089c332 328 // // Switch databus to outputs
wim 2:1dab1089c332 329 // _databus.busdir(WRITE);
wim 2:1dab1089c332 330
wim 2:1dab1089c332 331 // Set CE low and wait
wim 2:1dab1089c332 332 _enablebus.chipselect(CS_BRIGHT, LOW);
wim 2:1dab1089c332 333 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 334
wim 2:1dab1089c332 335 // Write data to the databus
wim 2:1dab1089c332 336 _databus.write(_brightness);
wim 2:1dab1089c332 337
wim 2:1dab1089c332 338 // Set WR low, wait, then set high and wait
wim 2:1dab1089c332 339 _controlbus.WR(LOW);
wim 2:1dab1089c332 340 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 341 _controlbus.WR(HIGH);
wim 2:1dab1089c332 342 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 343
wim 2:1dab1089c332 344 // Set CE high and wait
wim 2:1dab1089c332 345 _enablebus.chipselect(CS_BRIGHT, HIGH);
wim 2:1dab1089c332 346 wait_ms(STATUS_1TCY_WAIT_MS);
wim 2:1dab1089c332 347
wim 2:1dab1089c332 348
wim 2:1dab1089c332 349 // // Switch databus back to inputs
wim 2:1dab1089c332 350 // _databus.busdir(READ);
wim 2:1dab1089c332 351 // // Switch databus buffer back to inputs
wim 2:1dab1089c332 352 // _controlbus.busdir(READ);
wim 2:1dab1089c332 353
wim 2:1dab1089c332 354 }
wim 2:1dab1089c332 355