/* receiver.c */ /* The receiver just listens, to all packets. */ /* 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 */ lcd_int(c); 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; } } } 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 */ pid2=execi(&listen, 0, NULL, 1, DEFAULT_STACK_SIZE); return 0; }