OSDN Git Service

Add support for signed 13 and 15 bits samples
authorPatrick Porlan <patrick.porlan@intel.com>
Wed, 23 Jul 2014 09:36:24 +0000 (11:36 +0200)
committerAdriana Reus <adriana.reus@intel.com>
Mon, 4 Aug 2014 14:33:23 +0000 (17:33 +0300)
The BMC150 magnetometer declares its x and y samples
as le:s13/16>>3 and its z samples as le:s15/16>>1, so
generalize a little bit our handling of signed integers.

Change-Id: I308629ac84ce898da63c31b757da358391ee530c
Signed-off-by: Patrick Porlan <patrick.porlan@intel.com>
transform.c

index 04221b1..aaf33c3 100644 (file)
@@ -126,6 +126,8 @@ static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
        uint64_t u64;
        int i;
        int zeroed_bits = type->storagebits - type->realbits;
+       uint64_t sign_mask;
+       uint64_t value_mask;
 
        u64 = 0;
 
@@ -141,14 +143,15 @@ static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
        if (type->sign == 'u')
                return (int64_t) u64; /* We don't handle unsigned 64 bits int */
 
+       /* Signed integer */
+
        switch (type->realbits) {
+               case 0 ... 1:
+                       return 0;
+
                case 8:
                        return (int64_t) (int8_t) u64;
 
-               case 12:
-                       return (int64_t)  (u64 >>  11) ?
-                                       (((int64_t)-1) ^ 0xfff) | u64 : u64;
-
                case 16:
                        return (int64_t) (int16_t) u64;
 
@@ -157,10 +160,17 @@ static int64_t sample_as_int64(unsigned char* sample, struct datum_info_t* type)
 
                case 64:
                        return (int64_t) u64;
-       }
 
-       ALOGE("Unhandled sample storage size\n");
-       return 0;
+               default:
+                       sign_mask = 1 << (type->realbits-1);
+                       value_mask = sign_mask - 1;
+
+                       if (u64 & sign_mask)
+                               /* Negative value: return 2-complement */
+                               return - ((~u64 & value_mask) + 1);
+                       else
+                               return (int64_t) u64; /* Positive value */
+       }
 }