/* bytedance1.c */ /* I just include everything, to be on the safe side. */ #include #include #include #include #include #include #include #include #include #include #include /* lnp port definitions */ #define MY_PORT 0x1 #define DEST_HOST 0x8 #define DEST_PORT 0x2 #define DEST_ADDR ( DEST_HOST << 4 | DEST_PORT ) /* range of IR sensor */ #define IR_NEAR 0 #define IR_FAR 1 /* Stuff for receive buffer */ int gNewData = 0; unsigned char gMessagingData[256]; int gDataLength; /* Incoming packet handler */ void packet_handler(const unsigned char* data, unsigned char length, unsigned char src) { /* If the buffer is full, we just lose the data. */ if(gNewData == 0) { memcpy(gMessagingData, data, length); gDataLength = length; /* This is done to wake up the reader process */ gNewData = 1; } } /* This is the wake-up function that checks for new data. */ wakeup_t WaitForData(wakeup_t data) { return gNewData; } /* This is the reader function for incoming data */ int listen () { unsigned char c; while (1) { /* Waits for a byte */ wait_event(WaitForData, 0); /* Processes the byte */ c = gMessagingData[0]; gNewData = 0; /* Displays the data */ lcd_int (c); switch(c) { case 0: motor_a_dir (brake); motor_c_dir (brake); motor_a_speed(MIN_SPEED); motor_c_speed(MIN_SPEED); break; case 1: motor_a_dir (fwd); motor_c_dir (fwd); motor_a_speed(MAX_SPEED / 2); motor_c_speed(MAX_SPEED / 2); break; case 2: motor_a_dir (rev); motor_c_dir (rev); motor_a_speed(MAX_SPEED / 2); motor_c_speed(MAX_SPEED / 2); break; } } } /* This is the data sender */ int talk () { int i = MY_PORT; int len, result; char send_buffer [8]; while (1) { /* sets i to a new random action */ i = (i + 13) * 3 % 3; send_buffer [0] = i; len = 1; /* This sends a byte to the other process; keeps trying until it succeeds */ do { result = lnp_addressing_write(send_buffer, len, DEST_ADDR, MY_PORT); /* waits between attempts */ msleep (500); } while (result); } } int main() { pid_t pid1, pid2; /* Sets the IR distance */ lnp_logical_range(IR_FAR); /* Sets the packet handler */ lnp_addressing_set_handler (MY_PORT, packet_handler); /* Spawns the processes that talk, and listen */ pid1=execi(&talk, 0, NULL, 1, DEFAULT_STACK_SIZE); pid2=execi(&listen, 0, NULL, 1, DEFAULT_STACK_SIZE); return 0; }