From 1a1287f089b21d1218ce17ad71f2ec7484ef6e3f Mon Sep 17 00:00:00 2001 From: Davide Bongiovanni Date: Wed, 18 Jan 2017 23:32:22 +0100 Subject: [PATCH] Modified master architecture. Adjusted behavior to the new slave device architecture. The program quits as soon as it's done talking to the slave. --- RPi/i2c_challenge.c | 61 +++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/RPi/i2c_challenge.c b/RPi/i2c_challenge.c index cd4a114..094f272 100644 --- a/RPi/i2c_challenge.c +++ b/RPi/i2c_challenge.c @@ -21,41 +21,30 @@ #include #include -int main (int argc, char *argv[]) -{ - int fd; - int data; - int send=1; - wiringPiSetup () ; - fd=wiringPiI2CSetup (0x20) ; /*Use i2cdetect command to find your respective device address*/ - if(fd==-1) - { - printf("Can't setup the I2C device\n"); - return -1; - } - else - { - delay(1); - while(1) { - wiringPiI2CWrite(fd, send); - data=wiringPiI2CRead(fd); - printf(" Received data=%d\n", data); - if(data==-1) - { - printf("No data\n"); - //return -1; - } - else if(data == 17) { - printf("Exit code received \n"); - break; - } - else if(data != 0) { - printf("Sending response %d\n", data+3 ); - wiringPiI2CWrite(fd, data+3); - } - - } - } - return 0; +#define I2C_ADDR 0x20 + +uint8_t solve_challenge(uint8_t arg) { + return (arg >> 3) ^ (arg << 3); } +int main(int argc, char *argv[]) { + + int fd; // Linux filehandle to the i2c device + uint8_t challenge; + uint8_t response; + wiringPiSetup(); + + fd = wiringPiI2CSetup(I2C_ADDR); // Initialize the i2c protocol with the address + if (fd == -1) { // Failed to contact device + printf("Can't setup the I2C device\n"); + return -1; + } + + delay(1); + + challenge = wiringPiI2CRead(fd); + response = solve_challenge(challenge); + wiringPiI2CWrite(response); + + return 0; +}