/* * return foreign translation of s */ char * PSTR (char *s) { long filepos; if (subtitles_available == 0) init_lang(); if (subtitles_available < 0) return(s); filepos = lookup_offset(message_crc(s)); if (filepos == -1) { return(s); } else { fseek(langf, filepos, SEEK_SET); readstr(langf, strbuf, 1); } if (strbuf[0] == '\0') return(s); for (s = strbuf; *s; ++s) *s = EXT_C(*s); return(strbuf); }
void loop() { // put your main code here, to be run repeatedly if (kilo_uid != SEED_ID){ //send out message if (new_message == 1){ new_message = 0; if (my_gradient > (incoming_gradient+1)){ my_gradient = incoming_gradient+1; transmitmsg.data[0] = my_gradient; transmitmsg.crc = message_crc(&transmitmsg); } } } if (my_gradient == 0){ set_color(RGB(1,0,0)); } if (my_gradient == 1){ set_color(RGB(0,1,0)); } if (my_gradient == 2){ set_color(RGB(0,0,1)); } if (my_gradient == 3){ set_color(RGB(1,1,1)); } }
/*-------------------------------------------------------------------*/ void setup() { /* Initialise LED and motors */ set_color(RGB(0,0,0)); set_motors(0,0); /* Initialise random seed */ uint8_t seed = rand_hard(); rand_seed(seed); /* Initialise the list of words ready for message sending. The word index is contained in the first payload byte, while the follwoing three bytes contain the randomly-chosen word */ int i; for( i = 0; i < num_words; i++ ) { words[i].data[0] = i; words[i].type = NORMAL; words[i].crc = message_crc(&words[i]); } /* Initialise motion variables */ set_motion( FORWARD ); last_motion_ticks = rand_soft() % max_straight_ticks + 1; /* Initialise mng variables */ last_broadcast_ticks = rand_soft() % max_broadcast_ticks + 1; }
void setup() { // Initialize message: // The type is always NORMAL. message.type = NORMAL; // Some dummy data as an example. message.data[0] = 0; // It's important that the CRC is computed after the data has been set; // otherwise it would be wrong. message.crc = message_crc(&message); }
void setup() { // put your setup code here, to be run only once if (kilo_uid == SEED_ID){ my_gradient = 0; } else{ my_gradient = 255; } transmitmsg.type = NORMAL; transmitmsg.data[0] = my_gradient; transmitmsg.crc = message_crc(& transmitmsg); }
void setup() { //If the robot is the seed, its gradient should be 0: overwrite the // previously set value of GRADIENT_MAX. if (kilo_uid == SEED_ID) { own_gradient = 0; } // Set the transmission message. message.type = NORMAL; message.data[0] = own_gradient; message.crc = message_crc(&message); }
message * create_message(unsigned short from, unsigned short to, long ticket, short type, int slot, long value, unsigned short flags) { message *msg; msg = malloc(sizeof(message)); memset(msg, 0, sizeof(message)); msg->from = from; msg->to = to; msg->type = type; msg->ticket = ticket; msg->slot = slot; msg->value = value; msg->flags = flags; crc_t c = message_crc(msg); msg->crc = c; return msg; }
merge(char *base_file, char *lang_file, char *outfile, char *langID) { FILE *fp, *outf; long fpos = 0, filepos; int newmsgs = 0; if ((langf = fopen(lang_file, "r")) == NULL) { perror(lang_file); return -1; } strcpy(langfile, lang_file); if (langID) strcpy(language, langID); else language[0] = '\0'; /* use first language found */ errcount = 0; make_indexfile(NULL); if (errcount) return -1; langfile[0] = '\0'; /* don't print filename in error msgs */ if ((fp = fopen(base_file, "r")) == NULL) { perror(base_file); return -1; } if (outfile == NULL) outf = stdout; else { if ((outf = fopen(outfile, "w")) == NULL) { perror(outfile); return(-1); } } while (readstr(fp, strbuf, 0)) { copypos(fp, outf, fpos); fpos = ftell(fp); filepos = lookup_offset(message_crc(strbuf)); if (filepos == -1) { fprintf(outf, "No translation\n"); ++newmsgs; } else { fseek(langf, filepos, SEEK_SET); readstr(langf, strbuf, 1); copypos(langf, outf, filepos); } while (readstr(fp, strbuf, 1)) if (*strbuf == '\0') break; } copypos(fp, outf, fpos); fflush(outf); if (ferror(outf)) { perror(outfile); return -1; } if (newmsgs) fprintf(stderr, "%d untranslated messages\n", newmsgs); return errcount; }
/* * build the index table in memory, and if indexfile is not NULL, * write it to this file */ static int make_indexfile(char *indexfile) { FILE *indexf; long filepos; int total_msgs = 0; char *res; rewind(langf); indx_hdr.lang_fsize = fsize(langf); strncpy(indx_hdr.lang, language, 15); init_crc(); line = 1; nmsg = 0; while (readstr(langf, strbuf, 0)) { if (nmsg == max_msgs) { if (max_msgs) { max_msgs *= 2; indx_tbl = (struct indx_ent *) realloc(indx_tbl, max_msgs * sizeof(struct indx_ent)); } else { max_msgs = 400; indx_tbl = (struct indx_ent *) malloc(max_msgs * sizeof(struct indx_ent)); } if (indx_tbl == NULL) { fprintf(stderr, "Not enough memory for foreign subtitles\n"); return(-1); } } ++total_msgs; indx_tbl[nmsg].crc = message_crc(strbuf); if (lookup_offset(indx_tbl[nmsg].crc) != -1) error("message CRC not unique.\n"); do { filepos = ftell(langf); res = readstr (langf, strbuf, 1); /* Abort if find newline first */ if (*language == '\0') /* use first language found */ strcpy(language, lang); } while (res && strbuf[0] != '\0' && strcmp(language, lang) != 0); if (res == NULL) break; if (strbuf[0] == '\0') /* No translation */ continue; indx_tbl[nmsg].offset = filepos; ++nmsg; do res = readstr (langf, strbuf, 1); /* Abort if find newline first */ while (res && strbuf[0] != '\0'); } line = 0; indx_hdr.nmsg = nmsg; if (verbose) fprintf(stderr, "%s: %d messages, %d translations for language \"%s\"\n", langfile, total_msgs, nmsg, language); if (nmsg == 0) { fprintf(stderr, "No translations available for language \"%s\"\n\n", language); return(-1); } if (indexfile) { if ((indexf = fopen(indexfile, "wb")) == NULL) fprintf(stderr, "Cannot create %s\n", indexfile); else { fwrite(&indx_hdr, 1, sizeof(indx_hdr), indexf); fwrite(indx_tbl, sizeof(struct indx_ent), nmsg, indexf); if (ferror(indexf) || fclose(indexf)) fprintf(stderr, "error writing %s\n", indexfile); } } return(0); }
int crc_valid(message *msg) { crc_t c = message_crc(msg); return c == msg->crc; }
void loop() { // Only pay attention to messages if this robot is not the seed. if (kilo_uid != SEED_ID) { if (new_message == 1) { // If a neighbor's gradient is 1 or more less than this robot's // gradient, the latter should not increase. // Set last_gradient_anchored to kilo_ticks to inhibit activation of // the increment statement. if (own_gradient >= received_gradient + 1) { last_gradient_anchored = kilo_ticks; } // If a neighbor's gradient is 2 or more less than this robot's // gradient, reduce the latter to the neighbor's gradient + 1. if (own_gradient > received_gradient + 1) { own_gradient = received_gradient + 1; // Update the transmission message whenever the gradient changes. message.type = NORMAL; message.data[0] = own_gradient; message.crc = message_crc(&message); } new_message = 0; } // If no neighbor with a gradient of 1 or more less than this robot's // gradient is detected within 2 seconds, increment the latter by 1. if ((kilo_ticks > (last_gradient_anchored + 64)) && (own_gradient < GRADIENT_MAX)) { own_gradient = own_gradient + 1; } } // Set the LED color based on the gradient. if (own_gradient == 0) { set_color(RGB(1, 1, 1)); // White } else if (own_gradient == 1) { set_color(RGB(1, 0, 0)); // Red } else if (own_gradient == 2) { set_color(RGB(0, 1, 0)); // Green } else if (own_gradient == 3) { set_color(RGB(0, 0, 1)); // Blue } else if (own_gradient == 4) { set_color(RGB(1, 0, 1)); // Magenta } else if (own_gradient >= 5) { set_color(RGB(1, 1, 0)); // Yellow } }