comparison main.c @ 112:5234ccc416e8 tilt

split ds18b20 and fixed sensors
author Matt Johnston <matt@ucc.asn.au>
date Wed, 19 Sep 2012 23:23:06 +0800
parents 2ba9065e7c4f
children d69eb9f3274d
comparison
equal deleted inserted replaced
111:cdb26addf4f2 112:5234ccc416e8
11 #include <util/atomic.h> 11 #include <util/atomic.h>
12 #include <util/crc16.h> 12 #include <util/crc16.h>
13 13
14 #include "simple_ds18b20.h" 14 #include "simple_ds18b20.h"
15 #include "onewire.h" 15 #include "onewire.h"
16 #include "bma180.h"
16 17
17 // configuration params 18 // configuration params
18 // - measurement interval 19 // - measurement interval
19 // - transmit interval 20 // - transmit interval
20 // - bluetooth params 21 // - bluetooth params
43 44
44 // total amount of 16bit values available for measurements. 45 // total amount of 16bit values available for measurements.
45 // adjust emperically, be sure to allow enough stack space too 46 // adjust emperically, be sure to allow enough stack space too
46 #define TOTAL_MEASUREMENTS 840 47 #define TOTAL_MEASUREMENTS 840
47 48
49 // number of fixed sensors, should match setup_fixed_sensors()
50 #define N_FIXED_SENSORS 4
51
48 // each sensor slot uses 8 bytes 52 // each sensor slot uses 8 bytes
49 #define MAX_SENSORS 6 53 #define MAX_DS18B20 6
50 54
51 // fixed at 8, have a shorter name 55 // fixed at 8, have a shorter name
52 #define ID_LEN OW_ROMCODE_SIZE 56 #define ID_LEN OW_ROMCODE_SIZE
53 57
54 // #define HAVE_UART_ECHO 58 // #define HAVE_UART_ECHO
99 103
100 static uint8_t readpos; 104 static uint8_t readpos;
101 static char readbuf[30]; 105 static char readbuf[30];
102 static uint8_t have_cmd; 106 static uint8_t have_cmd;
103 107
108 // number of total sensors. non-ds18b20 sensors go at the end of the list.
104 static uint8_t n_sensors; 109 static uint8_t n_sensors;
105 static uint8_t sensor_id[MAX_SENSORS][ID_LEN]; 110 // number of ds18b20 sensors
106 111 static uint8_t n_ds18b20;
112 static uint8_t ds18b20_id[MAX_DS18B20][ID_LEN];
113
114 static const char *fixed_sensor_P[N_FIXED_SENSORS];
107 115
108 int uart_putchar(char c, FILE *stream); 116 int uart_putchar(char c, FILE *stream);
109 static void long_delay(int ms); 117 static void long_delay(int ms);
110 static void blink(); 118 static void blink();
111 static uint16_t adc_vcc(); 119 static uint16_t adc_vcc();
317 fprintf_P(crc_stdout, PSTR("measure=%hu\n"), measure_wake); 325 fprintf_P(crc_stdout, PSTR("measure=%hu\n"), measure_wake);
318 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake); 326 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake);
319 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); 327 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs);
320 fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK); 328 fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK);
321 fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE); 329 fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE);
322 fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS); 330 fprintf_P(crc_stdout, PSTR("maxds=%hhu\n"), MAX_DS18B20);
323 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS); 331 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS);
324 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors); 332 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors);
333 fprintf_P(crc_stdout, PSTR("n_ds=%hhu\n"), n_ds18b20);
325 for (uint8_t s = 0; s < n_sensors; s++) 334 for (uint8_t s = 0; s < n_sensors; s++)
326 { 335 {
327 fprintf_P(crc_stdout, PSTR("sensor_id%hhu="), s); 336 fprintf_P(crc_stdout, PSTR("sensor_id%hhu="), s);
328 printhex(sensor_id[s], ID_LEN, crc_stdout); 337 const char *type_P;
338 if (s < n_ds18b20)
339 {
340 printhex(ds18b20_id[s], ID_LEN, crc_stdout);
341 type_P = PSTR("ds\n");
342 }
343 else
344 {
345 fprintf_P(crc_stdout, fixed_sensor_P[s-n_ds18b20]);
346 type_P = PSTR("fixed\n");
347 }
348 fputc('\n', crc_stdout);
349 fprintf_P(crc_stdout, PSTR("sensor_type%hhu="), s);
350 fprintf_P(crc_stdout, type_P);
329 fputc('\n', crc_stdout); 351 fputc('\n', crc_stdout);
330 } 352 }
331 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements); 353 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements);
332 for (uint16_t n = 0; n < n_measurements; n++) 354 for (uint16_t n = 0; n < n_measurements; n++)
333 { 355 {
392 long_delay(DS18B20_TCONV_12BIT); 414 long_delay(DS18B20_TCONV_12BIT);
393 simple_ds18b20_read_all(); 415 simple_ds18b20_read_all();
394 } 416 }
395 417
396 static void 418 static void
397 init_sensors() 419 init_fixed_sensors(void)
420 {
421 fixed_sensor_P[0] = PSTR("accelx");
422 fixed_sensor_P[1] = PSTR("accely");
423 fixed_sensor_P[2] = PSTR("accelz");
424 fixed_sensor_P[3] = PSTR("acceltemp");
425
426 // default to 1g, 10hz
427 bma180_init(0, 0);
428 bma180_sleep();
429
430 n_sensors += N_FIXED_SENSORS;
431 }
432
433 static void
434 init_ds_sensors()
398 { 435 {
399 uint8_t id[OW_ROMCODE_SIZE]; 436 uint8_t id[OW_ROMCODE_SIZE];
400 printf_P(PSTR("init sensors\n")); 437 printf_P(PSTR("init sensors\n"));
401 ow_reset(); 438 ow_reset();
402 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) 439 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
410 if( diff == OW_DATA_ERR ) { 447 if( diff == OW_DATA_ERR ) {
411 printf_P( PSTR("Bus Error\r") ); 448 printf_P( PSTR("Bus Error\r") );
412 return; 449 return;
413 } 450 }
414 451
415 if (n_sensors < MAX_SENSORS) 452 if (n_ds18b20 < MAX_DS18B20)
416 { 453 {
417 memcpy(sensor_id[n_sensors], id, ID_LEN); 454 memcpy(ds18b20_id[n_sensors], id, ID_LEN);
418 printf_P(PSTR("Added sensor %hhu : "), n_sensors); 455 printf_P(PSTR("Added sensor %hhu : "), n_sensors);
419 printhex(id, ID_LEN, stdout); 456 printhex(id, ID_LEN, stdout);
420 putchar('\n'); 457 putchar('\n');
421 n_sensors++; 458 n_sensors++;
459 n_ds18b20++;
422 } 460 }
423 else 461 else
424 { 462 {
425 printf_P(PSTR("Too many sensors\n")); 463 printf_P(PSTR("Too many sensors\n"));
426 } 464 }
447 { 485 {
448 printf_P(PSTR("measure %hu\n"), measure_wake); 486 printf_P(PSTR("measure %hu\n"), measure_wake);
449 printf_P(PSTR("comms %hu\n"), comms_wake); 487 printf_P(PSTR("comms %hu\n"), comms_wake);
450 printf_P(PSTR("wake %hhu\n"), wake_secs); 488 printf_P(PSTR("wake %hhu\n"), wake_secs);
451 printf_P(PSTR("tick %d\n"), TICK); 489 printf_P(PSTR("tick %d\n"), TICK);
452 printf_P(PSTR("sensors %hhu (%hhu)\n"), 490 printf_P(PSTR("sensors %hhu\n"), n_sensors);
453 n_sensors, MAX_SENSORS); 491 printf_P(PSTR("ds18b20 %hhu (%hhu)\n"),
492 n_ds18b20, MAX_DS18B20);
454 printf_P(PSTR("meas %hu (%hu)\n"), 493 printf_P(PSTR("meas %hu (%hu)\n"),
455 max_measurements, TOTAL_MEASUREMENTS); 494 max_measurements, TOTAL_MEASUREMENTS);
456 } 495 }
457 496
458 static void 497 static void
673 } 712 }
674 713
675 for (uint8_t s = 0; s < n_sensors; s++) 714 for (uint8_t s = 0; s < n_sensors; s++)
676 { 715 {
677 uint16_t reading; 716 uint16_t reading;
678 uint8_t ret = simple_ds18b20_read_raw(sensor_id[s], &reading); 717 uint8_t ret = simple_ds18b20_read_raw(ds18b20_id[s], &reading);
679 if (ret != DS18X20_OK) 718 if (ret != DS18X20_OK)
680 { 719 {
681 reading = VALUE_BROKEN; 720 reading = VALUE_BROKEN;
682 } 721 }
683 set_measurement(s, n_measurements, reading); 722 set_measurement(s, n_measurements, reading);
764 803
765 printf(PSTR("Started.\n")); 804 printf(PSTR("Started.\n"));
766 805
767 load_params(); 806 load_params();
768 807
769 init_sensors(); 808 // ds sensors come before fixed sensors.
809 init_ds_sensors();
810
811 init_fixed_sensors();
770 812
771 uart_off(); 813 uart_off();
772 814
773 // turn off everything except timer2 815 // turn off everything except timer2
774 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); 816 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);