int main(void) { //! [init] system_clock_config(CLOCK_RESOURCE_XO_26_MHZ, CLOCK_FREQ_26_MHZ); configure_i2c_slave(); //! [packet] struct i2c_slave_packet packet = { .data_length = DATA_LENGTH, .data = write_buffer, }; //! [packet] //! [init] //! [while] while (true) { packet.data = read_buffer; i2c_slave_read_packet_wait(&i2c_slave_instance, &packet); packet.data = write_buffer; i2c_slave_write_packet_wait(&i2c_slave_instance, &packet); } //! [while] }
int main(void) { system_init(); //! [run_initialize_i2c] /* Configure device and enable */ //! [config] configure_i2c_slave(); //! [config] //! [config_callback] configure_i2c_slave_callbacks(); //! [config_callback] //! [run_initialize_i2c] //! [while] while (true) { /* Infinite loop while waiting for I2C master interaction */ } //! [while] }
int main(void) { system_init(); //! [run_initialize_i2c] //! [config] configure_i2c_slave(); //! [config] //! [dir] enum i2c_slave_direction dir; //! [dir] //! [pack] struct i2c_slave_packet packet = { .data_length = DATA_LENGTH, .data = buffer, }; //! [pack] //! [run_initialize_i2c] //! [while] while (true) { /* Wait for direction from master */ //! [get_dir] dir = i2c_slave_get_direction_wait(&i2c_slave_instance); //! [get_dir] /* Transfer packet in direction requested by master */ //! [transfer] if (dir == I2C_SLAVE_DIRECTION_READ) { i2c_slave_read_packet_wait(&i2c_slave_instance, &packet); } else if (dir == I2C_SLAVE_DIRECTION_WRITE) { i2c_slave_write_packet_wait(&i2c_slave_instance, &packet); } //! [transfer] } //! [while] }
int main (void) { system_init(); // Initialize local variables used in main int failed = 0; uint8_t debug_string[24] = "y: x: z: c: "; uint8_t jx, jy; char z, c, lz, lc; jx = jy = z = c = lz = lc = 0; int cycle_index = 0; uint8_t cycle = 0; uint8_t light_mode = 0; int light_modes = 1; uint16_t head, brake = 0; uint8_t LIGHTS_ON = 0; uint8_t FWD = 0; uint8_t HEADLIGHTS = 0; uint8_t DEBUG_BLE = 1; uint16_t BLE_temp = '0'; //uint16_t light_sens; // Configure Devices configure_port_pins(); configure_LED_PWM(); configure_usart(); configure_i2c_slave(); initIMU(); failed = !beginIMU(); configure_i2c_slave_callbacks(); //configure_ADC(); // Set the BLE module name to "long-itude" uint8_t string[17] = "AT+NAMElong-itude"; usart_write_buffer_wait(&usart_instance, string, sizeof(string)); for(int i = 0; i < 50000; ++i); // Switch LED direction to FWD setFWD(); while(1) { //readAccel(); //readGyro(); //readMag(); //light_sens = getLightSens(); // Read data from BLE if (usart_read_wait(&usart_instance, &BLE_temp) == STATUS_OK) { switch(BLE_temp) { case '0': LIGHTS_ON = 0; break; case '1': LIGHTS_ON = 1; break; case '2': DEBUG_BLE = 0; break; case '3': DEBUG_BLE = 1; break; case '4': setFWD(); break; case '5': setREV(); break; } } if(DEBUG_BLE) { uint8_t temp = I2C_slave_read_buffer[0]; debug_string[4] = '0' + temp%10; temp = (temp - temp%10)/10; debug_string[3] = '0' + temp%10; temp = (temp - temp%10)/10; debug_string[2] = '0' + temp%10; temp = (temp - temp%10)/10; temp = I2C_slave_read_buffer[1]; debug_string[11] = '0' + temp%10; temp = (temp - temp%10)/10; debug_string[10] = '0' + temp%10; temp = (temp - temp%10)/10; debug_string[9] = '0' + temp%10; temp = (temp - temp%10)/10; debug_string[16] = '0' + I2C_slave_read_buffer[2]; debug_string[21] = '0' + I2C_slave_read_buffer[3]; debug_string[22] = '\r'; debug_string[23] = '\n'; usart_write_buffer_wait(&usart_instance, debug_string, sizeof(debug_string)); } jx = I2C_slave_read_buffer[0]; jy = I2C_slave_read_buffer[1]; z = I2C_slave_read_buffer[2]; c = I2C_slave_read_buffer[3]; if(z == 0 && lz != 0) { if(jy > 200) { setFWD(); if(HEADLIGHTS && FWD) HEADLIGHTS = 0; else if(!HEADLIGHTS && FWD) HEADLIGHTS = 1; else if(HEADLIGHTS && !FWD) { FWD = 1; } else if(!HEADLIGHTS && !FWD) { HEADLIGHTS = 1; FWD = 1; } } else if(jy < 55) { setREV(); if(HEADLIGHTS && !FWD) HEADLIGHTS = 0; else if(!HEADLIGHTS && !FWD) HEADLIGHTS = 1; else if(HEADLIGHTS && FWD) { FWD = 0; } else if(!HEADLIGHTS && FWD) { HEADLIGHTS = 1; FWD = 0; } } else if(jy > 110 && jy < 150 && jx > 110 && jx < 150) { LIGHTS_ON = !LIGHTS_ON; } else if(jx > 200) { light_mode++; if(light_mode >= light_modes) light_mode = 0; } else if(jx < 55) { light_mode--; if(light_mode <= -1) light_mode = light_modes-1; } } if(LIGHTS_ON) { if(light_mode == 0) { if(cycle == 0) { setLeftRGB(cycle_index,0,0xCFFF-cycle_index); setRightRGB(cycle_index,0,0xCFFF-cycle_index); } if(cycle == 1) { setLeftRGB(0xCFFF-cycle_index,cycle_index,0); setRightRGB(0xCFFF-cycle_index,cycle_index,0); } if(cycle == 2) { setLeftRGB(0,0xCFFF-cycle_index,cycle_index); setRightRGB(0,0xCFFF-cycle_index,cycle_index); } cycle_index += 250; if(cycle_index >= 0xCFFF) { cycle_index = 0; cycle += 1; if(cycle == 3) cycle = 0; } } if(HEADLIGHTS) { head = 0xFFFF; setWhite(head); if(jy < 120) { brake = (0xFFFF/120)*(120-jy); setRed(brake); } else setRed(0); } else { setWhite(0); setRed(0); } } else { setWhite(0); setRed(0); setLeftRGB(0,0,0); setRightRGB(0,0,0); } lc = c; lz = z; } }