/* 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_PORT 0x2 #define MY_HOST 0x0 #define DEST_HOST 0x0 #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); /* Flashes the IR sign on the LCD to denote reception */ dlcd_show(LCD_CIRCLE_2); dlcd_show(LCD_CIRCLE_3); msleep (100); dlcd_hide(LCD_CIRCLE_2); dlcd_hide(LCD_CIRCLE_3); /* Processes the byte */ c = gMessagingData[0]; gNewData = 0; /* Displays the data */ lcd_int (c); /* No moving around please for the moment 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 () { unsigned long seed = MY_PORT; int i; int len, result; char send_buffer [8]; while (1) { /* sets i to a new random action */ seed = seed * 1664525L * seed + 1013904223L; i = seed % 3; send_buffer [0] = i; len = 1; /* This sends a byte to the other process; keeps trying until it succeeds */ do { dlcd_show (LCD_CIRCLE_0); dlcd_show (LCD_CIRCLE_1); result = lnp_addressing_write(send_buffer, len, DEST_ADDR, MY_PORT); /* waits between attempts */ lcd_int(i); msleep (100); dlcd_hide (LCD_CIRCLE_0); dlcd_hide (LCD_CIRCLE_1); } while (result); /* interval of time before sending another command */ msleep (900); } } int main() { pid_t pid1, pid2; /* I do it here because of a legOS bug */ gNewData = 0; /* 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; }