/* checks the state of the button presses */ void checkState(unsigned char conv_result) { // START_STOP if ( ( (conv_result >= 40) && (conv_result <= 60) ) && (state != 1) ) { state = 1; // start the Stopwatch TCCR1B |= (1 << CS11) | (1 << CS10); } else if ( (conv_result >= 40) && (conv_result <= 60) && (state == 1) ) { state = 0; // stop the Stopwatch TCCR1B &= ~(1 << CS11) & ~(1 << CS10); } // LAP else if ( (conv_result >= 90) && (conv_result <= 110) && (state == 1) ) { state = 2; // stop the Stopwatch and continue counting } else if ( (conv_result >= 90) && (conv_result <= 110) && (state == 2) ) { state = 1; // stop the Stopwatch and continue counting if currently lapped } else if ( (conv_result >= 90) && (conv_result <= 110) && (state == 0) ) { init_count(); } }
int process_file(FILE* file, struct srv_connection *conn){ struct select s_val; int count[CHAR_CT]; int res; init_count(count); while(1){ if (!prepare_select(conn, &s_val)){ printf("File has processed, All clients ended"); break; } res = select(s_val.max_d+1, &(s_val.readfds), NULL, NULL, &(s_val.t)); if (res<0) { printf("err select\n"); continue; } else if (res==0){ printf("\ns timeout s\n"); continue; } process_select(conn, s_val, file, count); } print_result(count, CHAR_CT); return 0; }
static uint read_mailbox(const char *arg, mlhead_t *msgs) { if (verbose) { printf("Reading %s\n", arg); fflush(stdout); } init_count(); mbox_mode = true; bogoreader_init(1, &arg); while ((*reader_more)()) { wordhash_t *whp = NULL; wordhash_t *whc = wordhash_new(); collect_words(whc); if (ds_path != NULL && (msgs_good + msgs_bad) == 0) set_train_msg_counts(whc); if (whc->count == 0 && !quiet) { printf("msg #%u, count is %u\n", message_count, whc->count); bt_trap(); } if (bogolex_file != NULL) { wordhash_sort(whc); lookup_words(whc); write_msgcount_file(whc); } else if (whc->count != 0) { if (!msg_count_file) whp = convert_wordhash_to_propslist(whc, train); else whp = convert_propslist_to_countlist(whc); msglist_add(msgs, whp); } update_count(); if (whc != whp) wordhash_free(whc); } print_final_count(); ns_and_sp->count += message_count; bogoreader_fini(); return message_count; }
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size) { char* buffer; void* ctx; msgpack_zone* z; if(initial_buffer_size < COUNTER_SIZE) { initial_buffer_size = COUNTER_SIZE; } buffer = (char*)malloc(initial_buffer_size); if(buffer == NULL) { return false; } ctx = malloc(sizeof(template_context)); if(ctx == NULL) { free(buffer); return false; } z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); if(z == NULL) { free(ctx); free(buffer); return false; } mpac->buffer = buffer; mpac->used = COUNTER_SIZE; mpac->free = initial_buffer_size - mpac->used; mpac->off = COUNTER_SIZE; mpac->parsed = 0; mpac->last_parsed = 0; mpac->initial_buffer_size = initial_buffer_size; mpac->z = z; mpac->ctx = ctx; init_count(mpac->buffer); template_init(CTX_CAST(mpac->ctx)); CTX_CAST(mpac->ctx)->user.z = mpac->z; CTX_CAST(mpac->ctx)->user.referenced = false; return true; }
void count(const AzPmat *m) { if (v_border.rowNum() == 0) init_count(); AzDmat md; m->get(&md); const double *border = v_border.point(); int row, col; for (col = 0; col < md.colNum(); ++col) { for (row = 0; row < md.rowNum(); ++row) { double val = md.get(row, col); int bx; for (bx = 0; bx < v_border.rowNum(); ++bx) { if (val <= border[bx]) { v_pop.add(bx, 1); break; } } } } }
int main(void) { // Initialize the ADC registers init_adc(); init_lcd(); // initialize the display writecommand(0x01); // clear display welcomemessage(); // print welcome message writecommand(0x01); // clear display init_count(); init_timer(25000); // initializes the timer with the count of 0.1s sei(); // start the ISR unsigned char old_value = 0; unsigned char conv_result = 0; while (1) // Loop forever { ADCSRA |= ( 1 << ADSC ); // Start the conversion while ( (ADCSRA & ( 1 << ADSC)) != 0 ) {} conv_result = ADCH; if (old_value != conv_result) { checkState(conv_result); old_value = conv_result; _delay_ms(200); } } return 0; /* never reached */ }
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size) { if(mpac->used == mpac->off && get_count(mpac->buffer) == 1 && !CTX_REFERENCED(mpac)) { // rewind buffer mpac->free += mpac->used - COUNTER_SIZE; mpac->used = COUNTER_SIZE; mpac->off = COUNTER_SIZE; if(mpac->free >= size) { return true; } } if(mpac->off == COUNTER_SIZE) { size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE while(next_size < size + mpac->used) { next_size *= 2; } char* tmp = (char*)realloc(mpac->buffer, next_size); if(tmp == NULL) { return false; } mpac->buffer = tmp; mpac->free = next_size - mpac->used; } else { size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE size_t not_parsed = mpac->used - mpac->off; while(next_size < size + not_parsed + COUNTER_SIZE) { next_size *= 2; } char* tmp = (char*)malloc(next_size); if(tmp == NULL) { return false; } init_count(tmp); memcpy(tmp+COUNTER_SIZE, mpac->buffer+mpac->off, not_parsed); if(CTX_REFERENCED(mpac)) { if(!msgpack_zone_push_finalizer(mpac->z, decl_count, mpac->buffer)) { free(tmp); return false; } CTX_REFERENCED(mpac) = false; } else { decl_count(mpac->buffer); } mpac->buffer = tmp; mpac->used = not_parsed + COUNTER_SIZE; mpac->free = next_size - mpac->used; mpac->off = COUNTER_SIZE; } return true; }
int main(int argc, char *argv[]) { char *configfile = NULL; FILE *fin, *bin; char *linebuf = NULL; size_t buflen = 0; int iterations = 3; int mode = 3; int c; float d; float *loglik; float p; int i, j, k; opterr = 0; while ((c = getopt(argc, argv, "c:n:hp:")) != -1) { switch (c) { case 'c': configfile = optarg; break; case 'h': usage(); exit(EXIT_SUCCESS); case 'n': iterations = atoi(optarg); break; case 'p': mode = atoi(optarg); if (mode != 1 && mode != 2 && mode != 3) { fprintf(stderr, "illegal mode: %d\n", mode); exit(EXIT_FAILURE); } break; case '?': fprintf(stderr, "illegal options\n"); exit(EXIT_FAILURE); default: abort(); } } if (configfile == NULL) { fin = stdin; } else { fin = fopen(configfile, "r"); if (fin == NULL) { handle_error("fopen"); } } i = 0; while ((c = getline(&linebuf, &buflen, fin)) != -1) { if (c <= 1 || linebuf[0] == '#') continue; if (i == 0) { if (sscanf(linebuf, "%d", &nstates) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } prior = (float *) malloc(sizeof(float) * nstates); if (prior == NULL) handle_error("malloc"); trans = (float *) malloc(sizeof(float) * nstates * nstates); if (trans == NULL) handle_error("malloc"); xi = (float *) malloc(sizeof(float) * nstates * nstates); if (xi == NULL) handle_error("malloc"); pi = (float *) malloc(sizeof(float) * nstates); if (pi == NULL) handle_error("malloc"); } else if (i == 1) { if (sscanf(linebuf, "%d", &nobvs) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } obvs = (float *) malloc(sizeof(float) * nstates * nobvs); if (obvs == NULL) handle_error("malloc"); gmm = (float *) malloc(sizeof(float) * nstates * nobvs); if (gmm == NULL) handle_error("malloc"); } else if (i == 2) { /* read initial state probabilities */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < nstates; j++) { if (fscanf(bin, "%f", &d) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } prior[j] = logf(d); } fclose(bin); } else if (i <= 2 + nstates) { /* read state transition probabilities */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < nstates; j++) { if (fscanf(bin, "%f", &d) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } trans[IDX((i - 3),j,nstates)] = logf(d); } fclose(bin); } else if (i <= 2 + nstates * 2) { /* read output probabilities */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < nobvs; j++) { if (fscanf(bin, "%f", &d) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } obvs[IDX((i - 3 - nstates),j,nobvs)] = logf(d); } fclose(bin); } else if (i == 3 + nstates * 2) { if (sscanf(linebuf, "%d %d", &nseq, &length) != 2) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } data = (int *) malloc (sizeof(int) * nseq * length); if (data == NULL) handle_error("malloc"); } else if (i <= 3 + nstates * 2 + nseq) { /* read data */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < length; j++) { if (fscanf(bin, "%d", &k) != 1 || k < 0 || k >= nobvs) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } data[(i - 4 - nstates * 2) * length + j] = k; } fclose(bin); } i++; } fclose(fin); if (linebuf) free(linebuf); if (i < 4 + nstates * 2 + nseq) { fprintf(stderr, "configuration incomplete.\n"); freeall(); exit(EXIT_FAILURE); } if (mode == 3) { loglik = (float *) malloc(sizeof(float) * nseq); if (loglik == NULL) handle_error("malloc"); for (i = 0; i < iterations; i++) { init_count(); for (j = 0; j < nseq; j++) { loglik[j] = forward_backward(data + length * j, length, 1); } p = sumf(loglik, nseq); update_prob(); printf("iteration %d log-likelihood: %.4f\n", i + 1, p); printf("updated parameters:\n"); printf("# initial state probability\n"); for (j = 0; j < nstates; j++) { printf(" %.4f", exp(prior[j])); } printf("\n"); printf("# state transition probability\n"); for (j = 0; j < nstates; j++) { for (k = 0; k < nstates; k++) { printf(" %.4f", exp(trans[IDX(j,k,nstates)])); } printf("\n"); } printf("# state output probility\n"); for (j = 0; j < nstates; j++) { for (k = 0; k < nobvs; k++) { printf(" %.4f", exp(obvs[IDX(j,k,nobvs)])); } printf("\n"); } printf("\n"); } free(loglik); } else if (mode == 2) { for (i = 0; i < nseq; i++) { viterbi(data + length * i, length); } } else if (mode == 1) { loglik = (float *) malloc(sizeof(float) * nseq); if (loglik == NULL) handle_error("malloc"); for (i = 0; i < nseq; i++) { loglik[i] = forward_backward(data + length * i, length, 0); } p = sumf(loglik, nseq); for (i = 0; i < nseq; i++) printf("%.4f\n", loglik[i]); printf("total: %.4f\n", p); free(loglik); } freeall(); return 0; }