![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ft. button press reset
Fork of BeaconDemo_RobotCode by
PsiSwarm/i2c.cpp@0:8a5497a2e366, 2015-10-03 (annotated)
- Committer:
- jah128
- Date:
- Sat Oct 03 22:48:50 2015 +0000
- Revision:
- 0:8a5497a2e366
- Child:
- 5:598298aa4900
Initial commit of PsiSwarm API and example code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:8a5497a2e366 | 1 | /* University of York Robotics Laboratory PsiSwarm Library: I2C Source File |
jah128 | 0:8a5497a2e366 | 2 | * |
jah128 | 0:8a5497a2e366 | 3 | * File: i2c.cpp |
jah128 | 0:8a5497a2e366 | 4 | * |
jah128 | 0:8a5497a2e366 | 5 | * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York |
jah128 | 0:8a5497a2e366 | 6 | * |
jah128 | 0:8a5497a2e366 | 7 | * PsiSwarm Library Version: 0.2 |
jah128 | 0:8a5497a2e366 | 8 | * |
jah128 | 0:8a5497a2e366 | 9 | * September 2015 |
jah128 | 0:8a5497a2e366 | 10 | * |
jah128 | 0:8a5497a2e366 | 11 | */ |
jah128 | 0:8a5497a2e366 | 12 | |
jah128 | 0:8a5497a2e366 | 13 | #include "psiswarm.h" |
jah128 | 0:8a5497a2e366 | 14 | |
jah128 | 0:8a5497a2e366 | 15 | char gpio_byte0; |
jah128 | 0:8a5497a2e366 | 16 | char gpio_byte1; |
jah128 | 0:8a5497a2e366 | 17 | char user_id_set = 0; |
jah128 | 0:8a5497a2e366 | 18 | char wheel_enc_set = 0; |
jah128 | 0:8a5497a2e366 | 19 | char switch_set = 0; |
jah128 | 0:8a5497a2e366 | 20 | |
jah128 | 0:8a5497a2e366 | 21 | char emitter_byte = 0x00; |
jah128 | 0:8a5497a2e366 | 22 | |
jah128 | 0:8a5497a2e366 | 23 | Timeout update_timeout; |
jah128 | 0:8a5497a2e366 | 24 | |
jah128 | 0:8a5497a2e366 | 25 | DigitalOut test_led(LED2); |
jah128 | 0:8a5497a2e366 | 26 | |
jah128 | 0:8a5497a2e366 | 27 | char test; |
jah128 | 0:8a5497a2e366 | 28 | |
jah128 | 0:8a5497a2e366 | 29 | void IF_set_IR_emitter_output(char emitter, char state) |
jah128 | 0:8a5497a2e366 | 30 | { |
jah128 | 0:8a5497a2e366 | 31 | if(emitter <3) { |
jah128 | 0:8a5497a2e366 | 32 | if(state == 0) { |
jah128 | 0:8a5497a2e366 | 33 | char shift = 1 << emitter; |
jah128 | 0:8a5497a2e366 | 34 | emitter_byte &= (0xFF - shift); |
jah128 | 0:8a5497a2e366 | 35 | } |
jah128 | 0:8a5497a2e366 | 36 | if(state == 1) { |
jah128 | 0:8a5497a2e366 | 37 | char shift = 1 << emitter; |
jah128 | 0:8a5497a2e366 | 38 | emitter_byte |= shift; |
jah128 | 0:8a5497a2e366 | 39 | } |
jah128 | 0:8a5497a2e366 | 40 | char data[2]; |
jah128 | 0:8a5497a2e366 | 41 | data [0] = 0x0A; //Write to OLAT register |
jah128 | 0:8a5497a2e366 | 42 | data [1] = emitter_byte; //GP0-3 are outputs on aux expansion IC |
jah128 | 0:8a5497a2e366 | 43 | //pc.printf("%c\n", emitter_byte); |
jah128 | 0:8a5497a2e366 | 44 | primary_i2c.write(AUX_IC_ADDRESS,data,2,false); |
jah128 | 0:8a5497a2e366 | 45 | } |
jah128 | 0:8a5497a2e366 | 46 | } |
jah128 | 0:8a5497a2e366 | 47 | |
jah128 | 0:8a5497a2e366 | 48 | void IF_set_base_LED(char state) |
jah128 | 0:8a5497a2e366 | 49 | { |
jah128 | 0:8a5497a2e366 | 50 | if(state == 0) { |
jah128 | 0:8a5497a2e366 | 51 | emitter_byte &= 0xF7; |
jah128 | 0:8a5497a2e366 | 52 | } else emitter_byte |= 0x08; |
jah128 | 0:8a5497a2e366 | 53 | char data[2]; |
jah128 | 0:8a5497a2e366 | 54 | data [0] = 0x0A; //Write to OLAT register |
jah128 | 0:8a5497a2e366 | 55 | data [1] = emitter_byte; //GP0-3 are outputs on aux expansion IC |
jah128 | 0:8a5497a2e366 | 56 | primary_i2c.write(AUX_IC_ADDRESS,data,2,false); |
jah128 | 0:8a5497a2e366 | 57 | |
jah128 | 0:8a5497a2e366 | 58 | } |
jah128 | 0:8a5497a2e366 | 59 | |
jah128 | 0:8a5497a2e366 | 60 | unsigned short IF_read_IR_adc_value(char adc, char index) |
jah128 | 0:8a5497a2e366 | 61 | { |
jah128 | 0:8a5497a2e366 | 62 | char address = ADC1_ADDRESS; |
jah128 | 0:8a5497a2e366 | 63 | if(adc == 2) address=ADC2_ADDRESS; |
jah128 | 0:8a5497a2e366 | 64 | // Returns the raw sensor value for the IR sensor defined by index (range 0-7). |
jah128 | 0:8a5497a2e366 | 65 | short value = 0; |
jah128 | 0:8a5497a2e366 | 66 | // Read a single value from the ADC |
jah128 | 0:8a5497a2e366 | 67 | if(index<8) { |
jah128 | 0:8a5497a2e366 | 68 | char apb[1]; |
jah128 | 0:8a5497a2e366 | 69 | char data[2]; |
jah128 | 0:8a5497a2e366 | 70 | switch(index) { |
jah128 | 0:8a5497a2e366 | 71 | case 0: |
jah128 | 0:8a5497a2e366 | 72 | apb[0]=0x80; |
jah128 | 0:8a5497a2e366 | 73 | break; |
jah128 | 0:8a5497a2e366 | 74 | case 1: |
jah128 | 0:8a5497a2e366 | 75 | apb[0]=0x90; |
jah128 | 0:8a5497a2e366 | 76 | break; |
jah128 | 0:8a5497a2e366 | 77 | case 2: |
jah128 | 0:8a5497a2e366 | 78 | apb[0]=0xA0; |
jah128 | 0:8a5497a2e366 | 79 | break; |
jah128 | 0:8a5497a2e366 | 80 | case 3: |
jah128 | 0:8a5497a2e366 | 81 | apb[0]=0xB0; |
jah128 | 0:8a5497a2e366 | 82 | break; |
jah128 | 0:8a5497a2e366 | 83 | case 4: |
jah128 | 0:8a5497a2e366 | 84 | apb[0]=0xC0; |
jah128 | 0:8a5497a2e366 | 85 | break; |
jah128 | 0:8a5497a2e366 | 86 | case 5: |
jah128 | 0:8a5497a2e366 | 87 | apb[0]=0xD0; |
jah128 | 0:8a5497a2e366 | 88 | break; |
jah128 | 0:8a5497a2e366 | 89 | case 6: |
jah128 | 0:8a5497a2e366 | 90 | apb[0]=0xE0; |
jah128 | 0:8a5497a2e366 | 91 | break; |
jah128 | 0:8a5497a2e366 | 92 | case 7: |
jah128 | 0:8a5497a2e366 | 93 | apb[0]=0xF0; |
jah128 | 0:8a5497a2e366 | 94 | break; |
jah128 | 0:8a5497a2e366 | 95 | } |
jah128 | 0:8a5497a2e366 | 96 | primary_i2c.write(address,apb,1,false); |
jah128 | 0:8a5497a2e366 | 97 | primary_i2c.read(address,data,2,false); |
jah128 | 0:8a5497a2e366 | 98 | value=((data[0] % 16)<<8)+data[1]; |
jah128 | 0:8a5497a2e366 | 99 | if(value > 4096) value=4096; |
jah128 | 0:8a5497a2e366 | 100 | value=4096-value; |
jah128 | 0:8a5497a2e366 | 101 | } |
jah128 | 0:8a5497a2e366 | 102 | return value; |
jah128 | 0:8a5497a2e366 | 103 | } |
jah128 | 0:8a5497a2e366 | 104 | |
jah128 | 0:8a5497a2e366 | 105 | char IF_setup_led_expansion_ic(void) |
jah128 | 0:8a5497a2e366 | 106 | { |
jah128 | 0:8a5497a2e366 | 107 | //LED expansion IC is PCA9555 |
jah128 | 0:8a5497a2e366 | 108 | //Address is 0100 001x (0x42) {defined by LED_IC_ADDRESS} |
jah128 | 0:8a5497a2e366 | 109 | //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 | 0:8a5497a2e366 | 110 | //Message structure: {Address-RW}{Command}{Port 0}{Port 1} |
jah128 | 0:8a5497a2e366 | 111 | //Command bytes: 00000010 (0x02) = Write to output port |
jah128 | 0:8a5497a2e366 | 112 | //Command bytes: 00000110 (0x06) = Write to config registers |
jah128 | 0:8a5497a2e366 | 113 | //Note that for the LEDs, 0 = on, 1 = off |
jah128 | 0:8a5497a2e366 | 114 | //Port 0 = LED 1:4 Red:Green |
jah128 | 0:8a5497a2e366 | 115 | //Port 1 = LED 5:8 Red:Green |
jah128 | 0:8a5497a2e366 | 116 | char data [3]; |
jah128 | 0:8a5497a2e366 | 117 | data [0] = 0x06; //Write config registers |
jah128 | 0:8a5497a2e366 | 118 | data [1] = 0x00; //All 8 pins in port 0 are outputs (0) |
jah128 | 0:8a5497a2e366 | 119 | data [2] = 0x00; //All 8 pins in port 1 are outputs (0) |
jah128 | 0:8a5497a2e366 | 120 | primary_i2c.write(LED_IC_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 121 | |
jah128 | 0:8a5497a2e366 | 122 | //Turn all LEDs on |
jah128 | 0:8a5497a2e366 | 123 | data [0] = 0x02; //Write to output port |
jah128 | 0:8a5497a2e366 | 124 | data [1] = 0x00; //Enable LED1-4 (both colours) |
jah128 | 0:8a5497a2e366 | 125 | data [2] = 0x00; //Enable LED5-8 (both colours) |
jah128 | 0:8a5497a2e366 | 126 | primary_i2c.write(LED_IC_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 127 | |
jah128 | 0:8a5497a2e366 | 128 | wait(0.05); |
jah128 | 0:8a5497a2e366 | 129 | //Turn all LEDs off |
jah128 | 0:8a5497a2e366 | 130 | data [0] = 0x02; //Write to output port |
jah128 | 0:8a5497a2e366 | 131 | data [1] = 0xFF; //Enable LED1-4 (both colours) |
jah128 | 0:8a5497a2e366 | 132 | data [2] = 0xFF; //Enable LED5-8 (both colours) |
jah128 | 0:8a5497a2e366 | 133 | return primary_i2c.write(LED_IC_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 134 | } |
jah128 | 0:8a5497a2e366 | 135 | |
jah128 | 0:8a5497a2e366 | 136 | //Returns 0 if successful, 1 if test mode button pressed |
jah128 | 0:8a5497a2e366 | 137 | void IF_setup_gpio_expansion_ic(void) |
jah128 | 0:8a5497a2e366 | 138 | { |
jah128 | 0:8a5497a2e366 | 139 | //Main GPIO expansion IC is PCA9555 |
jah128 | 0:8a5497a2e366 | 140 | //Address is 0100 000x (0x40) {defined by GPIO_IC_ADDRESS} |
jah128 | 0:8a5497a2e366 | 141 | //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 | 0:8a5497a2e366 | 142 | //Message structure: {Address-RW}{Command}{Port 0}{Port 1} |
jah128 | 0:8a5497a2e366 | 143 | //Command bytes: 00000010 (0x02) = Write to output port |
jah128 | 0:8a5497a2e366 | 144 | //Command bytes: 00000110 (0x06) = Write to config registers |
jah128 | 0:8a5497a2e366 | 145 | //Note that for the LEDs, 0 = on, 1 = off |
jah128 | 0:8a5497a2e366 | 146 | //Port 0 = PGDL; PGDR; PGDIR; UP; DOWN; LEFT; RIGHT; CENTER |
jah128 | 0:8a5497a2e366 | 147 | //Port 1 = ENC_LA; ENC_LB; ENC_RA; ENC_RB; ID0; ID1; ID2; ID3 |
jah128 | 0:8a5497a2e366 | 148 | char data [3]; |
jah128 | 0:8a5497a2e366 | 149 | data [0] = 0x06; //Write config registers |
jah128 | 0:8a5497a2e366 | 150 | data [1] = 0xFF; //All 8 pins in port 0 are inputs (1) |
jah128 | 0:8a5497a2e366 | 151 | data [2] = 0xFF; //All 8 pins in port 1 are inputs (1) |
jah128 | 0:8a5497a2e366 | 152 | if(primary_i2c.write(GPIO_IC_ADDRESS,data,3,false) != 0) { |
jah128 | 0:8a5497a2e366 | 153 | system_warnings += 2; |
jah128 | 0:8a5497a2e366 | 154 | debug("- WARNING: No I2C acknowledge for main GPIO IC\n"); |
jah128 | 0:8a5497a2e366 | 155 | } |
jah128 | 0:8a5497a2e366 | 156 | //Set all inputs to polarity-inverted (so a logic low = 1) |
jah128 | 0:8a5497a2e366 | 157 | data [0] = 0x04; //Write to polarity inversion ports |
jah128 | 0:8a5497a2e366 | 158 | data [1] = 0xFF; //Invert polarity of all bits in input port 0 |
jah128 | 0:8a5497a2e366 | 159 | data [2] = 0xFF; //Invert polarity of all bits in input port 1 |
jah128 | 0:8a5497a2e366 | 160 | primary_i2c.write(GPIO_IC_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 161 | |
jah128 | 0:8a5497a2e366 | 162 | wait(0.01); |
jah128 | 0:8a5497a2e366 | 163 | |
jah128 | 0:8a5497a2e366 | 164 | //Read data |
jah128 | 0:8a5497a2e366 | 165 | char read_data[2]; |
jah128 | 0:8a5497a2e366 | 166 | char command[1]; //Command to read from input port 0 |
jah128 | 0:8a5497a2e366 | 167 | command[0]=0; |
jah128 | 0:8a5497a2e366 | 168 | primary_i2c.write(GPIO_IC_ADDRESS,command,1,false); |
jah128 | 0:8a5497a2e366 | 169 | primary_i2c.read(GPIO_IC_ADDRESS,read_data,2,false); |
jah128 | 0:8a5497a2e366 | 170 | gpio_byte0 = read_data[0]; |
jah128 | 0:8a5497a2e366 | 171 | //char ret_val = (gpio_byte0 & 0xF8) >> 3; //Returns a >0 value if a button is being pushed |
jah128 | 0:8a5497a2e366 | 172 | gpio_byte1 = read_data[1]; |
jah128 | 0:8a5497a2e366 | 173 | IF_parse_gpio_byte0(gpio_byte0); |
jah128 | 0:8a5497a2e366 | 174 | IF_parse_gpio_byte1(gpio_byte1); |
jah128 | 0:8a5497a2e366 | 175 | //Setup interrupt handler for GPIO interrupts |
jah128 | 0:8a5497a2e366 | 176 | gpio_interrupt.mode(PullUp); |
jah128 | 0:8a5497a2e366 | 177 | gpio_interrupt.rise(&IF_handle_gpio_interrupt); |
jah128 | 0:8a5497a2e366 | 178 | //pc.printf("%c %c",gpio_byte0,gpio_byte1); |
jah128 | 0:8a5497a2e366 | 179 | |
jah128 | 0:8a5497a2e366 | 180 | //Secondary GPIO expansion IC is MCP23009 |
jah128 | 0:8a5497a2e366 | 181 | //Address is 0100 111 (0x4E) {defined by AUX_IC_ADDRESS} |
jah128 | 0:8a5497a2e366 | 182 | //GP0,1,2,3 are outputs for driving infrared emitters and the base LED |
jah128 | 0:8a5497a2e366 | 183 | //IODIR register wants to be 0xF0 (1=input, 0=output) |
jah128 | 0:8a5497a2e366 | 184 | data [0] = 0x00; //Write to IODIR register |
jah128 | 0:8a5497a2e366 | 185 | data [1] = 0xF0; //Set GP0-3 as outputs |
jah128 | 0:8a5497a2e366 | 186 | primary_i2c.write(AUX_IC_ADDRESS,data,2,false); |
jah128 | 0:8a5497a2e366 | 187 | |
jah128 | 0:8a5497a2e366 | 188 | if(primary_i2c.write(AUX_IC_ADDRESS,data,2,false) != 0) { |
jah128 | 0:8a5497a2e366 | 189 | system_warnings += 4; |
jah128 | 0:8a5497a2e366 | 190 | debug("- WARNING: No I2C acknowledge for aux GPIO IC\n"); |
jah128 | 0:8a5497a2e366 | 191 | } |
jah128 | 0:8a5497a2e366 | 192 | data [0] = 0x06; //Write to GPPU register |
jah128 | 0:8a5497a2e366 | 193 | data [1] = 0x0F; //Set GP0-3 as active pull-up outputs |
jah128 | 0:8a5497a2e366 | 194 | primary_i2c.write(AUX_IC_ADDRESS,data,2,false); |
jah128 | 0:8a5497a2e366 | 195 | |
jah128 | 0:8a5497a2e366 | 196 | //My interrupt is not so reliable: poll with a 50ms timeout in case interrupts aren't handled |
jah128 | 0:8a5497a2e366 | 197 | update_timeout.attach_us(&IF_update_gpio_inputs,50000); |
jah128 | 0:8a5497a2e366 | 198 | //return ret_val; |
jah128 | 0:8a5497a2e366 | 199 | } |
jah128 | 0:8a5497a2e366 | 200 | |
jah128 | 0:8a5497a2e366 | 201 | void IF_parse_gpio_byte0(char byte) |
jah128 | 0:8a5497a2e366 | 202 | { |
jah128 | 0:8a5497a2e366 | 203 | gpio_byte0 = byte; |
jah128 | 0:8a5497a2e366 | 204 | //GPIO byte zero contains the power line traces and the switch states |
jah128 | 0:8a5497a2e366 | 205 | char current_switch = ((gpio_byte0 & 0xF8) >> 3); |
jah128 | 0:8a5497a2e366 | 206 | if(switch_set == 1) { |
jah128 | 0:8a5497a2e366 | 207 | if(current_switch != switch_byte) { |
jah128 | 0:8a5497a2e366 | 208 | previous_switch_byte = switch_byte; |
jah128 | 0:8a5497a2e366 | 209 | switch_byte = current_switch; |
jah128 | 0:8a5497a2e366 | 210 | event++; |
jah128 | 0:8a5497a2e366 | 211 | switch_event = 1; |
jah128 | 0:8a5497a2e366 | 212 | } |
jah128 | 0:8a5497a2e366 | 213 | } else { |
jah128 | 0:8a5497a2e366 | 214 | switch_byte = current_switch; |
jah128 | 0:8a5497a2e366 | 215 | switch_set = 1; |
jah128 | 0:8a5497a2e366 | 216 | } |
jah128 | 0:8a5497a2e366 | 217 | } |
jah128 | 0:8a5497a2e366 | 218 | |
jah128 | 0:8a5497a2e366 | 219 | void IF_parse_gpio_byte1(char byte) |
jah128 | 0:8a5497a2e366 | 220 | { |
jah128 | 0:8a5497a2e366 | 221 | gpio_byte1 = byte; |
jah128 | 0:8a5497a2e366 | 222 | //GPIO byte one contains the wheel encoders and the ID switch |
jah128 | 0:8a5497a2e366 | 223 | char current_id = ((gpio_byte1 & 0xF0)>> 4); |
jah128 | 0:8a5497a2e366 | 224 | if(user_id_set == 1) { |
jah128 | 0:8a5497a2e366 | 225 | if(robot_id != current_id) { |
jah128 | 0:8a5497a2e366 | 226 | previous_robot_id = robot_id; |
jah128 | 0:8a5497a2e366 | 227 | robot_id = current_id; |
jah128 | 0:8a5497a2e366 | 228 | event++; |
jah128 | 0:8a5497a2e366 | 229 | change_id_event = 1; |
jah128 | 0:8a5497a2e366 | 230 | } |
jah128 | 0:8a5497a2e366 | 231 | } else { |
jah128 | 0:8a5497a2e366 | 232 | robot_id = current_id; |
jah128 | 0:8a5497a2e366 | 233 | user_id_set = 1; |
jah128 | 0:8a5497a2e366 | 234 | } |
jah128 | 0:8a5497a2e366 | 235 | char current_encoder = (gpio_byte1 & 0x0F); |
jah128 | 0:8a5497a2e366 | 236 | if(wheel_enc_set == 1) { |
jah128 | 0:8a5497a2e366 | 237 | if(wheel_encoder_byte != current_encoder) { |
jah128 | 0:8a5497a2e366 | 238 | previous_wheel_encoder_byte = wheel_encoder_byte; |
jah128 | 0:8a5497a2e366 | 239 | wheel_encoder_byte = current_encoder; |
jah128 | 0:8a5497a2e366 | 240 | event++; |
jah128 | 0:8a5497a2e366 | 241 | encoder_event = 1; |
jah128 | 0:8a5497a2e366 | 242 | } |
jah128 | 0:8a5497a2e366 | 243 | } else { |
jah128 | 0:8a5497a2e366 | 244 | wheel_encoder_byte = current_encoder; |
jah128 | 0:8a5497a2e366 | 245 | wheel_enc_set = 1; |
jah128 | 0:8a5497a2e366 | 246 | } |
jah128 | 0:8a5497a2e366 | 247 | } |
jah128 | 0:8a5497a2e366 | 248 | |
jah128 | 0:8a5497a2e366 | 249 | void IF_handle_gpio_interrupt() |
jah128 | 0:8a5497a2e366 | 250 | { |
jah128 | 0:8a5497a2e366 | 251 | test = 1-test; |
jah128 | 0:8a5497a2e366 | 252 | test_led = test; |
jah128 | 0:8a5497a2e366 | 253 | IF_update_gpio_inputs(); |
jah128 | 0:8a5497a2e366 | 254 | } |
jah128 | 0:8a5497a2e366 | 255 | |
jah128 | 0:8a5497a2e366 | 256 | char IF_is_switch_pressed() |
jah128 | 0:8a5497a2e366 | 257 | { |
jah128 | 0:8a5497a2e366 | 258 | //Read data |
jah128 | 0:8a5497a2e366 | 259 | char data[1]; |
jah128 | 0:8a5497a2e366 | 260 | char command[1] = {0}; //Command to read from input port 0 |
jah128 | 0:8a5497a2e366 | 261 | primary_i2c.write(GPIO_IC_ADDRESS,command,1,false); |
jah128 | 0:8a5497a2e366 | 262 | primary_i2c.read(GPIO_IC_ADDRESS,data,1,false); |
jah128 | 0:8a5497a2e366 | 263 | return (data[0] & 0x80); //Returns a 1 if the center button is being pushed |
jah128 | 0:8a5497a2e366 | 264 | } |
jah128 | 0:8a5497a2e366 | 265 | |
jah128 | 0:8a5497a2e366 | 266 | |
jah128 | 0:8a5497a2e366 | 267 | char IF_get_switch_state() |
jah128 | 0:8a5497a2e366 | 268 | { |
jah128 | 0:8a5497a2e366 | 269 | //Read data |
jah128 | 0:8a5497a2e366 | 270 | char data[1]; |
jah128 | 0:8a5497a2e366 | 271 | char command[1] = {0}; //Command to read from input port 0 |
jah128 | 0:8a5497a2e366 | 272 | primary_i2c.write(GPIO_IC_ADDRESS,command,1,false); |
jah128 | 0:8a5497a2e366 | 273 | primary_i2c.read(GPIO_IC_ADDRESS,data,1,false); |
jah128 | 0:8a5497a2e366 | 274 | return (data[0] & 0xF8) >> 3; //Returns the current switch state |
jah128 | 0:8a5497a2e366 | 275 | } |
jah128 | 0:8a5497a2e366 | 276 | |
jah128 | 0:8a5497a2e366 | 277 | void IF_update_gpio_inputs() |
jah128 | 0:8a5497a2e366 | 278 | { |
jah128 | 0:8a5497a2e366 | 279 | update_timeout.detach(); |
jah128 | 0:8a5497a2e366 | 280 | //Read data |
jah128 | 0:8a5497a2e366 | 281 | char data[2]; |
jah128 | 0:8a5497a2e366 | 282 | char command[1] = {0}; //Command to read from input port 0 |
jah128 | 0:8a5497a2e366 | 283 | primary_i2c.write(GPIO_IC_ADDRESS,command,1,false); |
jah128 | 0:8a5497a2e366 | 284 | primary_i2c.read(GPIO_IC_ADDRESS,data,2,false); |
jah128 | 0:8a5497a2e366 | 285 | if(data[0]!=gpio_byte0) { |
jah128 | 0:8a5497a2e366 | 286 | IF_parse_gpio_byte0(data[0]); |
jah128 | 0:8a5497a2e366 | 287 | } |
jah128 | 0:8a5497a2e366 | 288 | if(data[1]!=gpio_byte1) { |
jah128 | 0:8a5497a2e366 | 289 | IF_parse_gpio_byte1(data[1]); |
jah128 | 0:8a5497a2e366 | 290 | } |
jah128 | 0:8a5497a2e366 | 291 | update_timeout.attach_us(&IF_update_gpio_inputs,50000); |
jah128 | 0:8a5497a2e366 | 292 | } |
jah128 | 0:8a5497a2e366 | 293 | |
jah128 | 0:8a5497a2e366 | 294 | |
jah128 | 0:8a5497a2e366 | 295 | void IF_write_to_led_ic(char byte_0, char byte_1) |
jah128 | 0:8a5497a2e366 | 296 | { |
jah128 | 0:8a5497a2e366 | 297 | //Set LEDs |
jah128 | 0:8a5497a2e366 | 298 | char data[3]; |
jah128 | 0:8a5497a2e366 | 299 | data [0] = 0x02; //Write to output port |
jah128 | 0:8a5497a2e366 | 300 | data [1] = byte_0; |
jah128 | 0:8a5497a2e366 | 301 | data [2] = byte_1; |
jah128 | 0:8a5497a2e366 | 302 | primary_i2c.write(LED_IC_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 303 | } |
jah128 | 0:8a5497a2e366 | 304 | |
jah128 | 0:8a5497a2e366 | 305 | |
jah128 | 0:8a5497a2e366 | 306 | void IF_setup_temperature_sensor() |
jah128 | 0:8a5497a2e366 | 307 | { |
jah128 | 0:8a5497a2e366 | 308 | char data[3]; |
jah128 | 0:8a5497a2e366 | 309 | data[0] = 0x04; //Set critical temp limit |
jah128 | 0:8a5497a2e366 | 310 | data[1] = TEMPERATURE_CRITICAL_HI; |
jah128 | 0:8a5497a2e366 | 311 | data[2] = TEMPEARTURE_CRITICAL_LO; |
jah128 | 0:8a5497a2e366 | 312 | primary_i2c.write(TEMPERATURE_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 313 | data[0] = 0x02; //Set high temp limit |
jah128 | 0:8a5497a2e366 | 314 | data[1] = TEMPERATURE_HIGH_HI; |
jah128 | 0:8a5497a2e366 | 315 | data[2] = TEMPEARTURE_HIGH_LO; |
jah128 | 0:8a5497a2e366 | 316 | primary_i2c.write(TEMPERATURE_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 317 | data[0] = 0x03; //Set low temp limit |
jah128 | 0:8a5497a2e366 | 318 | data[1] = TEMPERATURE_LOW_HI; |
jah128 | 0:8a5497a2e366 | 319 | data[2] = TEMPEARTURE_LOW_LO; |
jah128 | 0:8a5497a2e366 | 320 | primary_i2c.write(TEMPERATURE_ADDRESS,data,3,false); |
jah128 | 0:8a5497a2e366 | 321 | } |
jah128 | 0:8a5497a2e366 | 322 | |
jah128 | 0:8a5497a2e366 | 323 | float IF_read_from_temperature_sensor() |
jah128 | 0:8a5497a2e366 | 324 | { |
jah128 | 0:8a5497a2e366 | 325 | char command[1] = {0x05}; //Write to Ta Register |
jah128 | 0:8a5497a2e366 | 326 | char data[3]; |
jah128 | 0:8a5497a2e366 | 327 | signed int temp; |
jah128 | 0:8a5497a2e366 | 328 | float temperature; |
jah128 | 0:8a5497a2e366 | 329 | primary_i2c.write(TEMPERATURE_ADDRESS,command,1,false); |
jah128 | 0:8a5497a2e366 | 330 | primary_i2c.read(TEMPERATURE_ADDRESS,data,2,false); |
jah128 | 0:8a5497a2e366 | 331 | |
jah128 | 0:8a5497a2e366 | 332 | //Convert the temperature data |
jah128 | 0:8a5497a2e366 | 333 | //First Check flag bits |
jah128 | 0:8a5497a2e366 | 334 | char UpperByte = data[0]; |
jah128 | 0:8a5497a2e366 | 335 | char LowerByte = data[1]; |
jah128 | 0:8a5497a2e366 | 336 | if ((UpperByte & 0x80) == 0x80) { |
jah128 | 0:8a5497a2e366 | 337 | debug("- WARNING: Temperature sensor reports critical temperature\n"); |
jah128 | 0:8a5497a2e366 | 338 | } |
jah128 | 0:8a5497a2e366 | 339 | if ((UpperByte & 0x40) == 0x40) { |
jah128 | 0:8a5497a2e366 | 340 | debug("- WARNING: Temperature sensor reports above upper limit\n"); |
jah128 | 0:8a5497a2e366 | 341 | } |
jah128 | 0:8a5497a2e366 | 342 | if ((UpperByte & 0x20) == 0x20) { |
jah128 | 0:8a5497a2e366 | 343 | debug("- WARNING: Temperature sensor reports below lower limit\n"); |
jah128 | 0:8a5497a2e366 | 344 | } |
jah128 | 0:8a5497a2e366 | 345 | UpperByte = UpperByte & 0x1F; //Clear flag bits |
jah128 | 0:8a5497a2e366 | 346 | if ((UpperByte & 0x10) == 0x10) { |
jah128 | 0:8a5497a2e366 | 347 | UpperByte = UpperByte & 0x0F; //Clear SIGN |
jah128 | 0:8a5497a2e366 | 348 | temp = (UpperByte * 256) + LowerByte; |
jah128 | 0:8a5497a2e366 | 349 | temperature = - (temp / 16.0f); |
jah128 | 0:8a5497a2e366 | 350 | } else { |
jah128 | 0:8a5497a2e366 | 351 | temp = (UpperByte * 256) + LowerByte; |
jah128 | 0:8a5497a2e366 | 352 | temperature = (temp / 16.0f); |
jah128 | 0:8a5497a2e366 | 353 | } |
jah128 | 0:8a5497a2e366 | 354 | return temperature; |
jah128 | 0:8a5497a2e366 | 355 | } |