Created
March 3, 2015 09:30
-
-
Save fabiomadge/d5a9b53ef64a16bd38dd to your computer and use it in GitHub Desktop.
Sensor Normalizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// Test | |
// | |
// Created by Fabio Madge Pimentel on 28/08/14. | |
// Copyright (c) 2014 Fabio Madge Pimentel. All rights reserved. | |
// | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#define MM_AMBIENT_LIGHT_READING (20) | |
#define MM_LED_DIMM_ADDR (30) | |
#define MM_LED_GLOBALCONFIG_BASE_ADDR (31) | |
#define MM_LED_GLOBALCONFIG_CONFIG_ADDR (37) | |
#define MM_LED0_BASE_ADDR (64) | |
#define MM_LED1_BASE_ADDR (80) | |
#define MM_LED2_BASE_ADDR (96) | |
#define MM_LED3_BASE_ADDR (112) | |
#define PIC_MAIN_I2C_ADDRESS (0x0A) | |
#define PIC_LEFT_I2C_ADDRESS (0x1A) | |
#define PIC_RIGHT_I2C_ADDRESS (0x2A) | |
#define HALT_ON_ANIMATION_MSG (0) | |
#define HALT_OFF_ANIMATION_MSG (1) | |
#define HALT_PAIR_SUCCESS_ANIMATION_MSG (2) | |
#define UPDATE_BAT_LEVEL_MSG (3) | |
#define UPDATE_DIMM_LEVEL_MSG (4) | |
/* Timeouts used to set up the LED flashing pattern in ms */ | |
#define ON_ANIMATION_TIME (4000) | |
#define OFF_ANIMATION_TIME (4000) | |
#define PAIR_SUCCESS_ANIMATION_TIME (2000) | |
#define UPDATE_BAT_LEVEL_TIME (1000) | |
#define UPDATE_DIMM_LEVEL_TIME (500) | |
//const uint8_t sensor[] = {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}; | |
const uint8_t sensor[] = {0x33, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x27, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77}; | |
void updateDimmLevel(bool initialise); | |
int main(int argc, const char * argv[]) { | |
printf("0\n"); | |
updateDimmLevel(true); | |
for (uint8_t i = 1; i < sizeof(sensor); i++) { | |
printf("%d\n", i); | |
updateDimmLevel(false); | |
} | |
return 0; | |
} | |
void updateDimmLevel(bool initialise){ | |
static uint8_t measurements [4] = { 0 }; | |
static uint8_t oldestMeasurementPointer = 0; | |
static uint8_t lastSent = 0; | |
uint8_t level; | |
uint8_t ev = 0; | |
uint16_t var = 0; /* 4 * Var */ | |
static uint8_t pointer = 0; | |
level = sensor[pointer++]; | |
/* level transformations */ | |
if (initialise) for(uint8_t i = 0; i < sizeof(measurements); i++) measurements[i] = level; | |
else measurements[oldestMeasurementPointer] = level; | |
/*Calculate expected value*/ | |
for(uint8_t i = 0; i < sizeof(measurements); i++){ | |
ev += measurements[i] >> 2; | |
} | |
/*Calculate Var*/ | |
for(uint8_t i = 0; i < sizeof(measurements); i++){ | |
int16_t temp = measurements[i]; | |
temp -= ev; | |
var += temp*temp; | |
} | |
/* Var small enough for a reliable reading | |
delta big enough to prevent unneccessary updates*/ | |
printf("var: %d, delta: %d, different: %s\n", var, (lastSent < ev ? ev - lastSent : lastSent - ev), lastSent != ev ? "true" : "false"); | |
if ((var < 100 && (lastSent < ev ? ev - lastSent : lastSent - ev) > 6) || initialise) | |
{ | |
lastSent = ev; | |
printf("The dimm level was updated to 0x%X.\n", level); | |
} | |
oldestMeasurementPointer = ++oldestMeasurementPointer >= sizeof(measurements) ? 0 : oldestMeasurementPointer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment