int main(void)
{
	DDRB |= (1 << PB2); // Ack
	acc_data data;
	char buf[50];
	t_message dcc_data;
	state_t state;
	unsigned char decode_state = 0;
	
	mm1acc_init();
	init_dcc_decode();
	
	uart_init(UART_BAUD_SELECT(38400, F_CPU));
	uart_puts("mm1acc serial monitor\n");
	sei();
	uint8_t	b = 0;
	uint8_t	crc = 0;
    /* insert your hardware initialization here */
    for(;;){
        /* insert your main loop code here */
		if (mm1acc_check(&data) == MM1_VALID) {
			sprintf(buf, "M0x%.2x - 0x%.2x %x\n", data.address, data.port, data.function);
			uart_puts(buf);
		}
		if ((state = dccacc_check(&dcc_data)) == DCC_VALID) {
			//if (dcc_data.dcc[0] < 128) {
			if (dcc_data.dcc[0] == 0 && dcc_data.dcc[1] == 0 && dcc_data.size == 2) {
				sprintf(buf, ".");
				uart_puts(buf);
			} else {
				if ((decode_state = analyze_message(&dcc_data)) > 0) {
				sprintf(buf, "D0x%.2x - ", state);
				uart_puts(buf);
				crc = 0;
				for (b = 0;b < dcc_data.size; b++) {
					crc = (crc ^ dcc_data.dcc[b]);
					sprintf(buf, "0x%.2x ", dcc_data.dcc[b]);
					uart_puts(buf);
				}
				sprintf(buf, "0x%.2x ", crc);
				uart_puts(buf);
				if (crc == 0) {
					sprintf(buf, "r: 0x%.2x", decode_state);
				}
				uart_puts(buf);
				sprintf(buf, "\n");
/*				uint8_t	val =0x62;
				switch (dcc_data[0]) {
					case 0x78:
						// Read CV
						if ((dcc_data[2] & 0b11101000) == 0b11101000) {
							uint8_t	bit = (1 << (dcc_data[2] & 0x7));
							if ((val & bit) > 0) {
								ack();
							}
						}
						break;
						
					default:
						break;
				}*/
					uart_puts(buf);
				}
			}
		}
		
    }
    return 0;   /* never reached */
}
示例#2
0
static void* input_thread(void* arg)
{
  hs_analysis_thread* at = (hs_analysis_thread*)arg;
  hs_log(g_module, 6, "starting input thread: %d", at->tid);

  hs_heka_message msg;
  hs_init_heka_message(&msg, 8);

  hs_config* cfg = at->plugins->cfg;
  hs_lookup_input_checkpoint(&cfg->cp_reader,
                             hs_input_dir,
                             at->input.name,
                             cfg->output_path,
                             &at->input.ib.cp);
  at->cp.id = at->input.ib.cp.id;
  at->cp.offset = at->input.ib.cp.offset;

  size_t bytes_read = 0;
#ifdef HINDSIGHT_CLI
  bool input_stop = false;
  while (!(at->plugins->stop && input_stop)) {
#else
  while (!at->plugins->stop) {
#endif
    if (at->input.fh) {
      if (hs_find_message(&msg, &at->input.ib)) {
        at->msg = &msg;
        at->current_t = time(NULL);
        analyze_message(at);

        // advance the checkpoint
        pthread_mutex_lock(&at->cp_lock);
        at->plugins->sample = false;
        at->cp.id = at->input.ib.cp.id;
        at->cp.offset = at->input.ib.cp.offset -
          (at->input.ib.readpos - at->input.ib.scanpos);
        pthread_mutex_unlock(&at->cp_lock);
      } else {
        bytes_read = hs_read_file(&at->input);
      }

      if (!bytes_read) {
#ifdef HINDSIGHT_CLI
        size_t cid = at->input.ib.cp.id;
#endif
        // see if the next file is there yet
        hs_open_file(&at->input, hs_input_dir, at->input.ib.cp.id + 1);
#ifdef HINDSIGHT_CLI
        if (cid == at->input.ib.cp.id && at->plugins->stop) {
          input_stop = true;
        }
#endif
      }
    } else { // still waiting on the first file
      hs_open_file(&at->input, hs_input_dir, at->input.ib.cp.id);
#ifdef HINDSIGHT_CLI
      if (!at->input.fh && at->plugins->stop) {
        input_stop = true;
      }
#endif
    }

    if (bytes_read || at->msg) {
      at->msg = NULL;
    } else {
      // trigger any pending timer events
      hs_clear_heka_message(&msg); // create an idle/empty message
      at->msg = &msg;
      at->current_t = time(NULL);
      analyze_message(at);
      at->msg = NULL;
      sleep(1);
    }
  }
  shutdown_timer_event(at);
  hs_free_heka_message(&msg);
  hs_log(g_module, 6, "exiting input_thread: %d", at->tid);
  pthread_exit(NULL);
}


void hs_init_analysis_plugins(hs_analysis_plugins* plugins,
                              hs_config* cfg,
                              hs_message_match_builder* mmb)
{
  hs_init_output(&plugins->output, cfg->output_path, hs_analysis_dir);

  plugins->thread_cnt = cfg->analysis_threads;
  plugins->cfg = cfg;
  plugins->stop = false;
  plugins->sample = false;
  plugins->mmb = mmb;

  plugins->list = malloc(sizeof(hs_analysis_thread) * cfg->analysis_threads);
  for (unsigned i = 0; i < cfg->analysis_threads; ++i) {
    init_analysis_thread(plugins, i);
  }
  plugins->threads = malloc(sizeof(pthread_t*) * (cfg->analysis_threads));
}