/* sender.c */ /* The sender both sends, and listens. */ /* I just include everything, to be on the safe side. */ #include #include #include #include #include #include #include #include #include #include #include /* 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) { /* 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 */ switch (c) { case 0: dlcd_show (LCD_CIRCLE_0); msleep(500); dlcd_hide (LCD_CIRCLE_0); break; case 1: dlcd_show (LCD_CIRCLE_1); msleep(500); dlcd_hide (LCD_CIRCLE_1); break; case 2: dlcd_show (LCD_CIRCLE_2); msleep(500); dlcd_hide (LCD_CIRCLE_2); break; case 3: dlcd_show (LCD_CIRCLE_3); msleep(500); dlcd_hide (LCD_CIRCLE_3); break; } } } /* This is the data sender */ int talk () { unsigned long seed = 34; /* why 34? why not? */ int i; int len, result; char send_buffer [8]; while (1) { /* sets i to a new random action */ seed = seed * 257 % 37 + 7; i = seed % 4; send_buffer [0] = i; len = 1; /* This sends a byte to the other process; keeps trying until it succeeds */ do { result = lnp_integrity_write(send_buffer, len); /* Displays which number it has sent */ lcd_int(i); /* waits between attempts */ msleep (100); } 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_integrity_set_handler (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; }