C++ Library for the PsiSwarm Robot - Version 0.8

Dependents:   PsiSwarm_V8_Blank_CPP Autonomia_RndmWlk

Fork of PsiSwarmV7_CPP by Psi Swarm Robot

Committer:
jah128
Date:
Mon Oct 17 13:09:10 2016 +0000
Revision:
12:878c6e9d9e60
Parent:
11:312663037b8c
Added Psiswarm class; changed Dances to Animations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 11:312663037b8c 1 /* University of York Robotics Laboratory PsiSwarm Library: I2C Setup Source File
jah128 11:312663037b8c 2 *
jah128 11:312663037b8c 3 * Copyright 2016 University of York
jah128 11:312663037b8c 4 *
jah128 11:312663037b8c 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
jah128 11:312663037b8c 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
jah128 11:312663037b8c 7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
jah128 11:312663037b8c 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jah128 11:312663037b8c 9 * See the License for the specific language governing permissions and limitations under the License.
jah128 11:312663037b8c 10 *
jah128 11:312663037b8c 11 * File: i2c_setup.cpp
jah128 11:312663037b8c 12 *
jah128 11:312663037b8c 13 * (C) Dept. Electronics & Computer Science, University of York
jah128 11:312663037b8c 14 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 11:312663037b8c 15 *
jah128 11:312663037b8c 16 * PsiSwarm Library Version: 0.8
jah128 11:312663037b8c 17 *
jah128 11:312663037b8c 18 * October 2016
jah128 11:312663037b8c 19 *
jah128 11:312663037b8c 20 *
jah128 11:312663037b8c 21 */
jah128 11:312663037b8c 22
jah128 11:312663037b8c 23 #include "psiswarm.h"
jah128 11:312663037b8c 24
jah128 11:312663037b8c 25 char gpio_byte0;
jah128 11:312663037b8c 26 char gpio_byte1;
jah128 11:312663037b8c 27 char user_id_set = 0;
jah128 11:312663037b8c 28 char wheel_enc_set = 0;
jah128 11:312663037b8c 29 char switch_set = 0;
jah128 11:312663037b8c 30
jah128 11:312663037b8c 31 char emitter_byte = 0x00;
jah128 11:312663037b8c 32
jah128 11:312663037b8c 33 Timeout update_timeout;
jah128 11:312663037b8c 34
jah128 11:312663037b8c 35 char test;
jah128 11:312663037b8c 36
jah128 11:312663037b8c 37 char Setup::get_dc_status()
jah128 11:312663037b8c 38 {
jah128 11:312663037b8c 39 IF_read_aux_ic_data();
jah128 11:312663037b8c 40 return status_dc_in;
jah128 11:312663037b8c 41 }
jah128 11:312663037b8c 42
jah128 11:312663037b8c 43 void Setup::IF_set_IR_emitter_output(char emitter, char state)
jah128 11:312663037b8c 44 {
jah128 11:312663037b8c 45 if(emitter <3) {
jah128 11:312663037b8c 46 if(state == 0) {
jah128 11:312663037b8c 47 char shift = 1 << emitter;
jah128 11:312663037b8c 48 emitter_byte &= (0xFF - shift);
jah128 11:312663037b8c 49 }
jah128 11:312663037b8c 50 if(state == 1) {
jah128 11:312663037b8c 51 char shift = 1 << emitter;
jah128 11:312663037b8c 52 emitter_byte |= shift;
jah128 11:312663037b8c 53 }
jah128 11:312663037b8c 54 char data[2];
jah128 11:312663037b8c 55 data [0] = 0x0A; //Write to OLAT register
jah128 11:312663037b8c 56 data [1] = emitter_byte; //GP0-3 are outputs on aux expansion IC
jah128 11:312663037b8c 57 //pc.printf("%c\n", emitter_byte);
jah128 11:312663037b8c 58 primary_i2c.write(AUX_IC_ADDRESS,data,2,false);
jah128 11:312663037b8c 59 }
jah128 11:312663037b8c 60 }
jah128 11:312663037b8c 61
jah128 11:312663037b8c 62 void Setup::IF_set_base_LED(char state)
jah128 11:312663037b8c 63 {
jah128 11:312663037b8c 64 if(state == 0) {
jah128 11:312663037b8c 65 emitter_byte &= 0xF7;
jah128 11:312663037b8c 66 } else emitter_byte |= 0x08;
jah128 11:312663037b8c 67 char data[2];
jah128 11:312663037b8c 68 data [0] = 0x0A; //Write to OLAT register
jah128 11:312663037b8c 69 data [1] = emitter_byte; //GP0-3 are outputs on aux expansion IC
jah128 11:312663037b8c 70 primary_i2c.write(AUX_IC_ADDRESS,data,2,false);
jah128 11:312663037b8c 71
jah128 11:312663037b8c 72 }
jah128 11:312663037b8c 73
jah128 11:312663037b8c 74 unsigned short Setup::IF_read_IR_adc_value(char adc, char index)
jah128 11:312663037b8c 75 {
jah128 11:312663037b8c 76 char address = ADC1_ADDRESS;
jah128 11:312663037b8c 77 if(adc == 2) address=ADC2_ADDRESS;
jah128 11:312663037b8c 78 // Returns the raw sensor value for the IR sensor defined by index (range 0-7).
jah128 11:312663037b8c 79 short value = 0;
jah128 11:312663037b8c 80 // Read a single value from the ADC
jah128 11:312663037b8c 81 if(index<8) {
jah128 11:312663037b8c 82 char apb[1];
jah128 11:312663037b8c 83 char data[2];
jah128 11:312663037b8c 84 switch(index) {
jah128 11:312663037b8c 85 case 0:
jah128 11:312663037b8c 86 apb[0]=0x80;
jah128 11:312663037b8c 87 break;
jah128 11:312663037b8c 88 case 1:
jah128 11:312663037b8c 89 apb[0]=0x90;
jah128 11:312663037b8c 90 break;
jah128 11:312663037b8c 91 case 2:
jah128 11:312663037b8c 92 apb[0]=0xA0;
jah128 11:312663037b8c 93 break;
jah128 11:312663037b8c 94 case 3:
jah128 11:312663037b8c 95 apb[0]=0xB0;
jah128 11:312663037b8c 96 break;
jah128 11:312663037b8c 97 case 4:
jah128 11:312663037b8c 98 apb[0]=0xC0;
jah128 11:312663037b8c 99 break;
jah128 11:312663037b8c 100 case 5:
jah128 11:312663037b8c 101 apb[0]=0xD0;
jah128 11:312663037b8c 102 break;
jah128 11:312663037b8c 103 case 6:
jah128 11:312663037b8c 104 apb[0]=0xE0;
jah128 11:312663037b8c 105 break;
jah128 11:312663037b8c 106 case 7:
jah128 11:312663037b8c 107 apb[0]=0xF0;
jah128 11:312663037b8c 108 break;
jah128 11:312663037b8c 109 }
jah128 11:312663037b8c 110 primary_i2c.write(address,apb,1,false);
jah128 11:312663037b8c 111 primary_i2c.read(address,data,2,false);
jah128 11:312663037b8c 112 value=((data[0] % 16)<<8)+data[1];
jah128 11:312663037b8c 113 if(value > 4096) value=4096;
jah128 11:312663037b8c 114 value=4096-value;
jah128 11:312663037b8c 115 }
jah128 11:312663037b8c 116 return value;
jah128 11:312663037b8c 117 }
jah128 11:312663037b8c 118
jah128 11:312663037b8c 119 char Setup::IF_setup_led_expansion_ic(void)
jah128 11:312663037b8c 120 {
jah128 11:312663037b8c 121 //LED expansion IC is PCA9555
jah128 11:312663037b8c 122 //Address is 0100 001x (0x42) {defined by LED_IC_ADDRESS}
jah128 11:312663037b8c 123 //All 16 entries are outputs as they drive LEDs; the relevant registers are 2&3 (output port registers) and 6&7 (config. registers: a 0=output)
jah128 11:312663037b8c 124 //Message structure: {Address-RW}{Command}{Port 0}{Port 1}
jah128 11:312663037b8c 125 //Command bytes: 00000010 (0x02) = Write to output port
jah128 11:312663037b8c 126 //Command bytes: 00000110 (0x06) = Write to config registers
jah128 11:312663037b8c 127 //Note that for the LEDs, 0 = on, 1 = off
jah128 11:312663037b8c 128 //Port 0 = LED 1:4 Red:Green
jah128 11:312663037b8c 129 //Port 1 = LED 5:8 Red:Green
jah128 11:312663037b8c 130 char data [3];
jah128 11:312663037b8c 131 data [0] = 0x06; //Write config registers
jah128 11:312663037b8c 132 data [1] = 0x00; //All 8 pins in port 0 are outputs (0)
jah128 11:312663037b8c 133 data [2] = 0x00; //All 8 pins in port 1 are outputs (0)
jah128 11:312663037b8c 134 primary_i2c.write(LED_IC_ADDRESS,data,3,false);
jah128 11:312663037b8c 135
jah128 11:312663037b8c 136 //Turn all LEDs on
jah128 11:312663037b8c 137 data [0] = 0x02; //Write to output port
jah128 11:312663037b8c 138 data [1] = 0x00; //Enable LED1-4 (both colours)
jah128 11:312663037b8c 139 data [2] = 0x00; //Enable LED5-8 (both colours)
jah128 11:312663037b8c 140 primary_i2c.write(LED_IC_ADDRESS,data,3,false);
jah128 11:312663037b8c 141
jah128 11:312663037b8c 142 wait(0.05);
jah128 11:312663037b8c 143 //Turn all LEDs off
jah128 11:312663037b8c 144 data [0] = 0x02; //Write to output port
jah128 11:312663037b8c 145 data [1] = 0xFF; //Enable LED1-4 (both colours)
jah128 11:312663037b8c 146 data [2] = 0xFF; //Enable LED5-8 (both colours)
jah128 11:312663037b8c 147 return primary_i2c.write(LED_IC_ADDRESS,data,3,false);
jah128 11:312663037b8c 148 }
jah128 11:312663037b8c 149
jah128 11:312663037b8c 150 //Returns 0 if successful, 1 if test mode button pressed
jah128 11:312663037b8c 151 void Setup::IF_setup_gpio_expansion_ic(void)
jah128 11:312663037b8c 152 {
jah128 11:312663037b8c 153 //Main GPIO expansion IC is PCA9555
jah128 11:312663037b8c 154 //Address is 0100 000x (0x40) {defined by GPIO_IC_ADDRESS}
jah128 11:312663037b8c 155 //All 16 entries are inputs; the relevant registers are 0&1 (input port registers), 4&5 (polarity inv. registers) and 6&7 (config. registers: a 0=output)
jah128 11:312663037b8c 156 //Message structure: {Address-RW}{Command}{Port 0}{Port 1}
jah128 11:312663037b8c 157 //Command bytes: 00000010 (0x02) = Write to output port
jah128 11:312663037b8c 158 //Command bytes: 00000110 (0x06) = Write to config registers
jah128 11:312663037b8c 159 //Note that for the LEDs, 0 = on, 1 = off
jah128 11:312663037b8c 160 //Port 0 = PGDL; PGDR; PGDIR; UP; DOWN; LEFT; RIGHT; CENTER
jah128 11:312663037b8c 161 //Port 1 = ENC_LA; ENC_LB; ENC_RA; ENC_RB; ID0; ID1; ID2; ID3
jah128 11:312663037b8c 162 char data [3];
jah128 11:312663037b8c 163 char okay = 1;
jah128 11:312663037b8c 164 data [0] = 0x06; //Write config registers
jah128 11:312663037b8c 165 data [1] = 0xFF; //All 8 pins in port 0 are inputs (1)
jah128 11:312663037b8c 166 data [2] = 0xFF; //All 8 pins in port 1 are inputs (1)
jah128 11:312663037b8c 167 if(primary_i2c.write(GPIO_IC_ADDRESS,data,3,false) != 0) {
jah128 11:312663037b8c 168 system_warnings += 2;
jah128 11:312663037b8c 169 okay = 0;
jah128 12:878c6e9d9e60 170 psi.debug("- WARNING: No I2C acknowledge for main GPIO IC\n");
jah128 11:312663037b8c 171 if(HALT_ON_GPIO_ERROR){
jah128 12:878c6e9d9e60 172 psi.debug("- PROGRAM HALTED. Check that robot is switched on!\n");
jah128 11:312663037b8c 173 while(1){
jah128 11:312663037b8c 174 mbed_led1=1;
jah128 11:312663037b8c 175 mbed_led2=1;
jah128 11:312663037b8c 176 mbed_led3=0;
jah128 11:312663037b8c 177 mbed_led4=0;
jah128 11:312663037b8c 178 wait(0.25);
jah128 11:312663037b8c 179 mbed_led1=0;
jah128 11:312663037b8c 180 mbed_led2=0;
jah128 11:312663037b8c 181 mbed_led3=1;
jah128 11:312663037b8c 182 mbed_led4=1;
jah128 11:312663037b8c 183 wait(0.25);
jah128 11:312663037b8c 184 }
jah128 11:312663037b8c 185 }
jah128 11:312663037b8c 186 }
jah128 11:312663037b8c 187 //Set all inputs to polarity-inverted (so a logic low = 1)
jah128 11:312663037b8c 188 data [0] = 0x04; //Write to polarity inversion ports
jah128 11:312663037b8c 189 data [1] = 0xF8; //Invert polarity of all switch input bits in input port 0 [but not power-good inputs]
jah128 11:312663037b8c 190 data [2] = 0xFF; //Invert polarity of all bits in input port 1
jah128 11:312663037b8c 191 primary_i2c.write(GPIO_IC_ADDRESS,data,3,false);
jah128 11:312663037b8c 192
jah128 11:312663037b8c 193 wait(0.01);
jah128 11:312663037b8c 194
jah128 11:312663037b8c 195 //Read data
jah128 11:312663037b8c 196 char read_data[2];
jah128 11:312663037b8c 197 char command[1]; //Command to read from input port 0
jah128 11:312663037b8c 198 command[0]=0;
jah128 11:312663037b8c 199 primary_i2c.write(GPIO_IC_ADDRESS,command,1,false);
jah128 11:312663037b8c 200 primary_i2c.read(GPIO_IC_ADDRESS,read_data,2,false);
jah128 11:312663037b8c 201 gpio_byte0 = read_data[0];
jah128 11:312663037b8c 202 //char ret_val = (gpio_byte0 & 0xF8) >> 3; //Returns a >0 value if a button is being pushed
jah128 11:312663037b8c 203 gpio_byte1 = read_data[1];
jah128 12:878c6e9d9e60 204 if(okay && testing_voltage_regulators_flag)psi.debug("- Checking 3.3V voltage regulators\n");
jah128 11:312663037b8c 205 IF_parse_gpio_byte0(gpio_byte0);
jah128 11:312663037b8c 206 IF_parse_gpio_byte1(gpio_byte1);
jah128 11:312663037b8c 207 testing_voltage_regulators_flag = 0;
jah128 11:312663037b8c 208 //Setup interrupt handler for GPIO interrupts
jah128 11:312663037b8c 209 gpio_interrupt.mode(PullUp);
jah128 11:312663037b8c 210 gpio_interrupt.rise(this,&Setup::IF_handle_gpio_interrupt);
jah128 11:312663037b8c 211 //pc.printf("%c %c",gpio_byte0,gpio_byte1);
jah128 11:312663037b8c 212
jah128 11:312663037b8c 213 //Secondary GPIO expansion IC is MCP23009
jah128 11:312663037b8c 214 //Address is 0100 111 (0x4E) {defined by AUX_IC_ADDRESS}
jah128 11:312663037b8c 215 //GP0,1,2,3 are outputs for driving infrared emitters and the base LED
jah128 11:312663037b8c 216 //IODIR register wants to be 0xF0 (1=input, 0=output)
jah128 11:312663037b8c 217 data [0] = 0x00; //Write to IODIR register
jah128 11:312663037b8c 218 data [1] = 0xF0; //Set GP0-3 as outputs
jah128 11:312663037b8c 219 primary_i2c.write(AUX_IC_ADDRESS,data,2,false);
jah128 11:312663037b8c 220
jah128 11:312663037b8c 221 if(primary_i2c.write(AUX_IC_ADDRESS,data,2,false) != 0) {
jah128 11:312663037b8c 222 system_warnings += 4;
jah128 12:878c6e9d9e60 223 psi.debug("- WARNING: No I2C acknowledge for aux GPIO IC\n");
jah128 11:312663037b8c 224 }
jah128 11:312663037b8c 225 data [0] = 0x06; //Write to GPPU register
jah128 11:312663037b8c 226 data [1] = 0x3F; //Set GP0-3 as active pull-up outputs and P4,P5 as pull-up inputs
jah128 11:312663037b8c 227 primary_i2c.write(AUX_IC_ADDRESS,data,2,false);
jah128 11:312663037b8c 228
jah128 11:312663037b8c 229 //My interrupt is not so reliable: poll with a 50ms timeout in case interrupts aren't handled
jah128 11:312663037b8c 230 update_timeout.attach_us(this,&Setup::IF_update_gpio_inputs,50000);
jah128 11:312663037b8c 231 //return ret_val;
jah128 11:312663037b8c 232 }
jah128 11:312663037b8c 233
jah128 11:312663037b8c 234 void Setup::IF_read_aux_ic_data()
jah128 11:312663037b8c 235 {
jah128 11:312663037b8c 236 //Read the values of the input pins on the auxilliary GPIO expander
jah128 11:312663037b8c 237 char write_data [1];
jah128 11:312663037b8c 238 char read_data [1];
jah128 11:312663037b8c 239 write_data[0] = 0x09;
jah128 11:312663037b8c 240 primary_i2c.write(AUX_IC_ADDRESS,write_data,1,false);
jah128 11:312663037b8c 241 primary_i2c.read(AUX_IC_ADDRESS,read_data,1,false);
jah128 11:312663037b8c 242 char old_charging_state = status_dc_in;
jah128 11:312663037b8c 243 status_dc_in = 1-((read_data[0] & 0x10) >> 4);
jah128 11:312663037b8c 244 if(status_dc_in!=old_charging_state){
jah128 12:878c6e9d9e60 245 if(status_dc_in == 0)psi.debug("No DC input\n");
jah128 12:878c6e9d9e60 246 else psi.debug("DC input to charge pins\n");
jah128 11:312663037b8c 247 }
jah128 11:312663037b8c 248 //pc.printf("Aux IC Data:%X Charge:%d\n",read_data[0],charge_in);
jah128 11:312663037b8c 249 }
jah128 11:312663037b8c 250
jah128 11:312663037b8c 251 void Setup::IF_parse_gpio_byte0(char byte)
jah128 11:312663037b8c 252 {
jah128 11:312663037b8c 253 gpio_byte0 = byte;
jah128 11:312663037b8c 254 //GPIO byte zero contains the power line traces and the switch states
jah128 11:312663037b8c 255 char current_switch = ((gpio_byte0 & 0xF8) >> 3);
jah128 11:312663037b8c 256 if(switch_set == 1) {
jah128 11:312663037b8c 257 if(current_switch != switch_byte) {
jah128 11:312663037b8c 258 previous_switch_byte = switch_byte;
jah128 11:312663037b8c 259 switch_byte = current_switch;
jah128 11:312663037b8c 260 event++;
jah128 11:312663037b8c 261 switch_event = 1;
jah128 11:312663037b8c 262 }
jah128 11:312663037b8c 263 } else {
jah128 11:312663037b8c 264 switch_byte = current_switch;
jah128 11:312663037b8c 265 switch_set = 1;
jah128 11:312663037b8c 266 }
jah128 11:312663037b8c 267 if(((gpio_byte0 & 0x01)) != power_good_motor_left){
jah128 11:312663037b8c 268 power_good_motor_left = (gpio_byte0 & 0x01);
jah128 11:312663037b8c 269 if(!power_good_motor_left){
jah128 12:878c6e9d9e60 270 if(testing_voltage_regulators_flag || SHOW_VR_WARNINGS)psi.debug("- WARNING: Voltage regulator left motor low\n");
jah128 11:312663037b8c 271 }
jah128 12:878c6e9d9e60 272 else if(testing_voltage_regulators_flag)psi.debug("- Power good left motor v.reg\n");
jah128 11:312663037b8c 273 }
jah128 11:312663037b8c 274 if(((gpio_byte0 & 0x02) >> 1) != power_good_motor_right){
jah128 11:312663037b8c 275 power_good_motor_right = (gpio_byte0 & 0x02) >> 1;
jah128 11:312663037b8c 276 if(!power_good_motor_right){
jah128 12:878c6e9d9e60 277 if(testing_voltage_regulators_flag || SHOW_VR_WARNINGS)psi.debug("- WARNING: Voltage regulator right motor low\n");
jah128 11:312663037b8c 278 }
jah128 12:878c6e9d9e60 279 else if(testing_voltage_regulators_flag)psi.debug("- Power good right motor v.reg\n");
jah128 11:312663037b8c 280 }
jah128 11:312663037b8c 281 if(((gpio_byte0 & 0x04) >> 2) != power_good_infrared){
jah128 11:312663037b8c 282 power_good_infrared = (gpio_byte0 & 0x04) >> 2;
jah128 11:312663037b8c 283 if(!power_good_infrared){
jah128 12:878c6e9d9e60 284 if(testing_voltage_regulators_flag || SHOW_VR_WARNINGS)psi.debug("- WARNING: Voltage regulator infrared low\n");
jah128 11:312663037b8c 285 }
jah128 12:878c6e9d9e60 286 else if(testing_voltage_regulators_flag)psi.debug("- Power good infrared and aux v.reg\n");
jah128 11:312663037b8c 287 }
jah128 11:312663037b8c 288 if(USE_LED4_FOR_VR_WARNINGS){
jah128 11:312663037b8c 289 mbed_led4 = (!power_good_motor_left || !power_good_motor_right || !power_good_infrared);
jah128 11:312663037b8c 290 }
jah128 11:312663037b8c 291 //Halt the system if settings flag is set and all v-regs are bad [usually this means robot is switched off!]
jah128 11:312663037b8c 292 if(HALT_ON_ALL_VREGS_LOW && !power_good_motor_left && !power_good_motor_right && !power_good_infrared){
jah128 12:878c6e9d9e60 293 psi.debug("- PROGRAM HALTED. Check that robot is switched on!\n");
jah128 11:312663037b8c 294 while(1){
jah128 11:312663037b8c 295 mbed_led1=1;
jah128 11:312663037b8c 296 mbed_led2=0;
jah128 11:312663037b8c 297 mbed_led3=1;
jah128 11:312663037b8c 298 mbed_led4=0;
jah128 11:312663037b8c 299 wait(0.25);
jah128 11:312663037b8c 300 mbed_led1=0;
jah128 11:312663037b8c 301 mbed_led2=1;
jah128 11:312663037b8c 302 mbed_led3=0;
jah128 11:312663037b8c 303 mbed_led4=1;
jah128 11:312663037b8c 304 wait(0.25);
jah128 11:312663037b8c 305 }
jah128 11:312663037b8c 306 }
jah128 11:312663037b8c 307 }
jah128 11:312663037b8c 308
jah128 11:312663037b8c 309 void Setup::IF_parse_gpio_byte1(char byte)
jah128 11:312663037b8c 310 {
jah128 11:312663037b8c 311 gpio_byte1 = byte;
jah128 11:312663037b8c 312 //GPIO byte one contains the wheel encoders and the ID switch
jah128 11:312663037b8c 313 char current_id = ((gpio_byte1 & 0xF0)>> 4);
jah128 11:312663037b8c 314 if(user_id_set == 1) {
jah128 11:312663037b8c 315 if(robot_id != current_id) {
jah128 11:312663037b8c 316 previous_robot_id = robot_id;
jah128 11:312663037b8c 317 robot_id = current_id;
jah128 11:312663037b8c 318 event++;
jah128 11:312663037b8c 319 change_id_event = 1;
jah128 11:312663037b8c 320 }
jah128 11:312663037b8c 321 } else {
jah128 11:312663037b8c 322 robot_id = current_id;
jah128 11:312663037b8c 323 user_id_set = 1;
jah128 11:312663037b8c 324 }
jah128 11:312663037b8c 325 char current_encoder = (gpio_byte1 & 0x0F);
jah128 11:312663037b8c 326 if(wheel_enc_set == 1) {
jah128 11:312663037b8c 327 if(wheel_encoder_byte != current_encoder) {
jah128 11:312663037b8c 328 previous_wheel_encoder_byte = wheel_encoder_byte;
jah128 11:312663037b8c 329 wheel_encoder_byte = current_encoder;
jah128 11:312663037b8c 330 event++;
jah128 11:312663037b8c 331 encoder_event = 1;
jah128 11:312663037b8c 332 }
jah128 11:312663037b8c 333 } else {
jah128 11:312663037b8c 334 wheel_encoder_byte = current_encoder;
jah128 11:312663037b8c 335 wheel_enc_set = 1;
jah128 11:312663037b8c 336 }
jah128 11:312663037b8c 337 }
jah128 11:312663037b8c 338
jah128 11:312663037b8c 339 void Setup::IF_handle_gpio_interrupt()
jah128 11:312663037b8c 340 {
jah128 11:312663037b8c 341 test = 1-test;
jah128 11:312663037b8c 342 if(USE_LED3_FOR_INTERRUPTS) mbed_led3 = test;
jah128 11:312663037b8c 343 IF_update_gpio_inputs();
jah128 11:312663037b8c 344 }
jah128 11:312663037b8c 345
jah128 11:312663037b8c 346 char Setup::IF_is_switch_pressed()
jah128 11:312663037b8c 347 {
jah128 11:312663037b8c 348 //Read data
jah128 11:312663037b8c 349 char data[1];
jah128 11:312663037b8c 350 char command[1] = {0}; //Command to read from input port 0
jah128 11:312663037b8c 351 primary_i2c.write(GPIO_IC_ADDRESS,command,1,false);
jah128 11:312663037b8c 352 primary_i2c.read(GPIO_IC_ADDRESS,data,1,false);
jah128 11:312663037b8c 353 return (data[0] & 0x80); //Returns a 1 if the center button is being pushed
jah128 11:312663037b8c 354 }
jah128 11:312663037b8c 355
jah128 11:312663037b8c 356
jah128 11:312663037b8c 357 char Setup::IF_get_switch_state()
jah128 11:312663037b8c 358 {
jah128 11:312663037b8c 359 //Read data
jah128 11:312663037b8c 360 char data[1];
jah128 11:312663037b8c 361 char command[1] = {0}; //Command to read from input port 0
jah128 11:312663037b8c 362 primary_i2c.write(GPIO_IC_ADDRESS,command,1,false);
jah128 11:312663037b8c 363 primary_i2c.read(GPIO_IC_ADDRESS,data,1,false);
jah128 11:312663037b8c 364 return (data[0] & 0xF8) >> 3; //Returns the current switch state
jah128 11:312663037b8c 365 }
jah128 11:312663037b8c 366
jah128 11:312663037b8c 367 void Setup::IF_update_gpio_inputs()
jah128 11:312663037b8c 368 {
jah128 11:312663037b8c 369 update_timeout.detach();
jah128 11:312663037b8c 370 //Read data
jah128 11:312663037b8c 371 char data[2];
jah128 11:312663037b8c 372 char command[1] = {0}; //Command to read from input port 0
jah128 11:312663037b8c 373 primary_i2c.write(GPIO_IC_ADDRESS,command,1,false);
jah128 11:312663037b8c 374 primary_i2c.read(GPIO_IC_ADDRESS,data,2,false);
jah128 11:312663037b8c 375 if(data[0]!=gpio_byte0) {
jah128 11:312663037b8c 376 IF_parse_gpio_byte0(data[0]);
jah128 11:312663037b8c 377 }
jah128 11:312663037b8c 378 if(data[1]!=gpio_byte1) {
jah128 11:312663037b8c 379 IF_parse_gpio_byte1(data[1]);
jah128 11:312663037b8c 380 }
jah128 11:312663037b8c 381 update_timeout.attach_us(this,&Setup::IF_update_gpio_inputs,50000);
jah128 11:312663037b8c 382 }
jah128 11:312663037b8c 383
jah128 11:312663037b8c 384
jah128 11:312663037b8c 385 void Setup::IF_write_to_led_ic(char byte_0, char byte_1)
jah128 11:312663037b8c 386 {
jah128 11:312663037b8c 387 //Set LEDs
jah128 11:312663037b8c 388 char data[3];
jah128 11:312663037b8c 389 data [0] = 0x02; //Write to output port
jah128 11:312663037b8c 390 data [1] = byte_0;
jah128 11:312663037b8c 391 data [2] = byte_1;
jah128 11:312663037b8c 392 primary_i2c.write(LED_IC_ADDRESS,data,3,false);
jah128 11:312663037b8c 393 }
jah128 11:312663037b8c 394
jah128 11:312663037b8c 395
jah128 11:312663037b8c 396 void Setup::IF_setup_temperature_sensor()
jah128 11:312663037b8c 397 {
jah128 11:312663037b8c 398 char data[3];
jah128 11:312663037b8c 399 data[0] = 0x04; //Set critical temp limit
jah128 11:312663037b8c 400 data[1] = TEMPERATURE_CRITICAL_HI;
jah128 11:312663037b8c 401 data[2] = TEMPEARTURE_CRITICAL_LO;
jah128 11:312663037b8c 402 primary_i2c.write(TEMPERATURE_ADDRESS,data,3,false);
jah128 11:312663037b8c 403 data[0] = 0x02; //Set high temp limit
jah128 11:312663037b8c 404 data[1] = TEMPERATURE_HIGH_HI;
jah128 11:312663037b8c 405 data[2] = TEMPEARTURE_HIGH_LO;
jah128 11:312663037b8c 406 primary_i2c.write(TEMPERATURE_ADDRESS,data,3,false);
jah128 11:312663037b8c 407 data[0] = 0x03; //Set low temp limit
jah128 11:312663037b8c 408 data[1] = TEMPERATURE_LOW_HI;
jah128 11:312663037b8c 409 data[2] = TEMPEARTURE_LOW_LO;
jah128 11:312663037b8c 410 primary_i2c.write(TEMPERATURE_ADDRESS,data,3,false);
jah128 11:312663037b8c 411 }
jah128 11:312663037b8c 412
jah128 11:312663037b8c 413 float Setup::IF_read_from_temperature_sensor()
jah128 11:312663037b8c 414 {
jah128 11:312663037b8c 415 char command[1] = {0x05}; //Write to Ta Register
jah128 11:312663037b8c 416 char data[3];
jah128 11:312663037b8c 417 signed int temp;
jah128 11:312663037b8c 418 float temperature;
jah128 11:312663037b8c 419 primary_i2c.write(TEMPERATURE_ADDRESS,command,1,false);
jah128 11:312663037b8c 420 primary_i2c.read(TEMPERATURE_ADDRESS,data,2,false);
jah128 11:312663037b8c 421
jah128 11:312663037b8c 422 //Convert the temperature data
jah128 11:312663037b8c 423 //First Check flag bits
jah128 11:312663037b8c 424 char UpperByte = data[0];
jah128 11:312663037b8c 425 char LowerByte = data[1];
jah128 11:312663037b8c 426 if ((UpperByte & 0x80) == 0x80) {
jah128 12:878c6e9d9e60 427 psi.debug("- WARNING: Temperature sensor reports critical temperature\n");
jah128 11:312663037b8c 428 }
jah128 11:312663037b8c 429 if ((UpperByte & 0x40) == 0x40) {
jah128 12:878c6e9d9e60 430 psi.debug("- WARNING: Temperature sensor reports above upper limit\n");
jah128 11:312663037b8c 431 }
jah128 11:312663037b8c 432 if ((UpperByte & 0x20) == 0x20) {
jah128 12:878c6e9d9e60 433 psi.debug("- WARNING: Temperature sensor reports below lower limit\n");
jah128 11:312663037b8c 434 }
jah128 11:312663037b8c 435 UpperByte = UpperByte & 0x1F; //Clear flag bits
jah128 11:312663037b8c 436 if ((UpperByte & 0x10) == 0x10) {
jah128 11:312663037b8c 437 UpperByte = UpperByte & 0x0F; //Clear SIGN
jah128 11:312663037b8c 438 temp = (UpperByte * 256) + LowerByte;
jah128 11:312663037b8c 439 temperature = - (temp / 16.0f);
jah128 11:312663037b8c 440 } else {
jah128 11:312663037b8c 441 temp = (UpperByte * 256) + LowerByte;
jah128 11:312663037b8c 442 temperature = (temp / 16.0f);
jah128 11:312663037b8c 443 }
jah128 11:312663037b8c 444 return temperature;
jah128 11:312663037b8c 445 }