changeset 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 cdb26addf4f2
children d69eb9f3274d
files bma180.c bma180.h bma180_internal.h main.c
diffstat 4 files changed, 77 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/bma180.c	Wed Sep 19 22:40:49 2012 +0800
+++ b/bma180.c	Wed Sep 19 23:23:06 2012 +0800
@@ -81,7 +81,7 @@
 
     // Set ee_w bit
     temp = bma180_read(CTRLREG0);
-    temp |= 0x10;
+    temp |= EE_W;
     bma180_write(CTRLREG0, temp);  // Have to set ee_w to write any other registers
 
     // Set BW
@@ -144,3 +144,21 @@
 
     *temperature = bma180_read(TEMPERATURE);
 }
+
+void
+bma180_sleep(void)
+{
+    uint8_t temp;
+    temp = bma180_read(CTRLREG0);
+    temp |= SLEEP;
+    bma180_write(CTRLREG0, temp);  // Have to set ee_w to write any other registers
+}
+
+void
+bma180_wake(void)
+{
+    uint8_t temp;
+    temp = bma180_read(CTRLREG0);
+    temp &= ~SLEEP;
+    bma180_write(CTRLREG0, temp);  // Have to set ee_w to write any other registers
+}
--- a/bma180.h	Wed Sep 19 22:40:49 2012 +0800
+++ b/bma180.h	Wed Sep 19 23:23:06 2012 +0800
@@ -10,5 +10,7 @@
 
 void bma180_get_reading(uint16_t *x, uint16_t *y, uint16_t *z, uint8_t *temperature);
 void bma180_init(uint8_t range, uint8_t bw);
+void bma180_sleep(void);
+void bma180_wake(void);
 
 #endif /* BMA180_H_ */
--- a/bma180_internal.h	Wed Sep 19 22:40:49 2012 +0800
+++ b/bma180_internal.h	Wed Sep 19 23:23:06 2012 +0800
@@ -41,6 +41,9 @@
 
 #define OLSB1 0x35
 
+#define EE_W (1<<4)
+#define SLEEP (1<<1)
+
 //====================//
 //Range setting
 #define RANGESHIFT 1
--- a/main.c	Wed Sep 19 22:40:49 2012 +0800
+++ b/main.c	Wed Sep 19 23:23:06 2012 +0800
@@ -13,6 +13,7 @@
 
 #include "simple_ds18b20.h"
 #include "onewire.h"
+#include "bma180.h"
 
 // configuration params
 // - measurement interval
@@ -45,8 +46,11 @@
 // adjust emperically, be sure to allow enough stack space too
 #define TOTAL_MEASUREMENTS 840
 
+// number of fixed sensors, should match setup_fixed_sensors()
+#define N_FIXED_SENSORS 4
+
 // each sensor slot uses 8 bytes
-#define MAX_SENSORS 6
+#define MAX_DS18B20 6
 
 // fixed at 8, have a shorter name
 #define ID_LEN OW_ROMCODE_SIZE
@@ -101,9 +105,13 @@
 static char readbuf[30];
 static uint8_t have_cmd;
 
+// number of total sensors. non-ds18b20 sensors go at the end of the list.
 static uint8_t n_sensors;
-static uint8_t sensor_id[MAX_SENSORS][ID_LEN];
+// number of ds18b20 sensors
+static uint8_t n_ds18b20;
+static uint8_t ds18b20_id[MAX_DS18B20][ID_LEN];
 
+static const char *fixed_sensor_P[N_FIXED_SENSORS];
 
 int uart_putchar(char c, FILE *stream);
 static void long_delay(int ms);
@@ -319,13 +327,27 @@
     fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs);
     fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK);
     fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE);
-    fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS);
+    fprintf_P(crc_stdout, PSTR("maxds=%hhu\n"), MAX_DS18B20);
     fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS);
     fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors);
+    fprintf_P(crc_stdout, PSTR("n_ds=%hhu\n"), n_ds18b20);
     for (uint8_t s = 0; s < n_sensors; s++)
     {
         fprintf_P(crc_stdout, PSTR("sensor_id%hhu="), s);
-        printhex(sensor_id[s], ID_LEN, crc_stdout);
+        const char *type_P;
+        if (s < n_ds18b20)
+        {
+            printhex(ds18b20_id[s], ID_LEN, crc_stdout);
+            type_P = PSTR("ds\n");
+        }
+        else
+        {
+            fprintf_P(crc_stdout, fixed_sensor_P[s-n_ds18b20]);
+            type_P = PSTR("fixed\n");
+        }
+        fputc('\n', crc_stdout);
+        fprintf_P(crc_stdout, PSTR("sensor_type%hhu="), s);
+        fprintf_P(crc_stdout, type_P);
         fputc('\n', crc_stdout);
     }
     fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements);
@@ -394,7 +416,22 @@
 }
 
 static void
-init_sensors()
+init_fixed_sensors(void)
+{
+    fixed_sensor_P[0] = PSTR("accelx");
+    fixed_sensor_P[1] = PSTR("accely");
+    fixed_sensor_P[2] = PSTR("accelz");
+    fixed_sensor_P[3] = PSTR("acceltemp");
+
+    // default to 1g, 10hz
+    bma180_init(0, 0);
+    bma180_sleep();
+
+    n_sensors += N_FIXED_SENSORS;
+}
+
+static void
+init_ds_sensors()
 {
     uint8_t id[OW_ROMCODE_SIZE];
     printf_P(PSTR("init sensors\n"));
@@ -412,13 +449,14 @@
             return;
         }
 
-        if (n_sensors < MAX_SENSORS)
+        if (n_ds18b20 < MAX_DS18B20)
         {
-            memcpy(sensor_id[n_sensors], id, ID_LEN);
+            memcpy(ds18b20_id[n_sensors], id, ID_LEN);
             printf_P(PSTR("Added sensor %hhu : "), n_sensors);
             printhex(id, ID_LEN, stdout);
             putchar('\n');
             n_sensors++;
+            n_ds18b20++;
         }
         else
         {
@@ -449,8 +487,9 @@
     printf_P(PSTR("comms %hu\n"), comms_wake);
     printf_P(PSTR("wake %hhu\n"), wake_secs);
     printf_P(PSTR("tick %d\n"), TICK);
-    printf_P(PSTR("sensors %hhu (%hhu)\n"), 
-            n_sensors, MAX_SENSORS);
+    printf_P(PSTR("sensors %hhu\n"), n_sensors);
+    printf_P(PSTR("ds18b20 %hhu (%hhu)\n"), 
+            n_ds18b20, MAX_DS18B20);
     printf_P(PSTR("meas %hu (%hu)\n"),
             max_measurements, TOTAL_MEASUREMENTS);
 }
@@ -675,7 +714,7 @@
     for (uint8_t s = 0; s < n_sensors; s++)
     {
         uint16_t reading;
-        uint8_t ret = simple_ds18b20_read_raw(sensor_id[s], &reading);
+        uint8_t ret = simple_ds18b20_read_raw(ds18b20_id[s], &reading);
         if (ret != DS18X20_OK)
         {
             reading = VALUE_BROKEN;
@@ -766,7 +805,10 @@
 
     load_params();
 
-    init_sensors();
+    // ds sensors come before fixed sensors.
+    init_ds_sensors();
+
+    init_fixed_sensors();
 
     uart_off();