FlexBook / Mbed 2 deprecated FlexBook171204a

Dependencies:   SDFileSystem app epson mbed msp430 pl tests

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pagesensor.cpp Source File

pagesensor.cpp

00001 //
00002 // Filename: pagesensor.cpp
00003 //
00004 // Flexbook page for page A1.
00005 //
00006 
00007 #include "pagesensor.h"
00008 #include "mcp23s17.h"
00009 #include "hal.h"
00010 
00011 
00012 #include "log.h"
00013 
00014 #include <iostream>
00015 
00016 namespace Flexbook {
00017 
00018 // Ticker sensorpoller;
00019 // const int POLL_PERIOD = 0.1;
00020 
00021 bool operator!=(const SensorData &lhs, const SensorData &rhs)
00022 {
00023     return lhs.temperature != rhs.temperature || lhs.pressure != rhs.pressure;
00024 }
00025 
00026 PageSensor::PageSensor()
00027 : pageoled(),
00028   sensordata()
00029 {
00030     Log("Creating PageSensor");
00031 
00032     //sensorpoller.attach(callback(this, &Flexbook::PageSensor::SensorPoll), POLL_PERIOD);
00033 }
00034 
00035 PageSensor::~PageSensor()
00036 {
00037     Log("Deleting PageSensor");
00038 
00039     //sensorpoller.detach();
00040 }
00041 
00042 void PageSensor::HandlePageActions()
00043 {
00044     //uint8_t dicenumber = PageDice::DiceValue();
00045     SensorPoll();
00046     //HandleDice();
00047     /*pagedice.HandlePageActions();
00048 
00049     if(sensordata != lastsensordata)
00050     {
00051         //pagedice.SensorPoll(sensordata);
00052         //pageoled.SensorPoll(sensordata);
00053         lastsensordata = sensordata;
00054     }
00055     
00056     pagedice.HandlePageActions();
00057         //pageoled.HandlePageActions();
00058     */
00059 
00060     pageoled.SensorPoll(sensordata);
00061     //pagedice.HandlePageActions();
00062 
00063 }
00064 
00065 
00066 
00067 // The BinTemp function takes the raw 16-bit AnalogIn data for the temperature sensor
00068 // and compares it to 7 binning values. It returns the bin number 0..6 which is the
00069 // number of segments of the OLED bar graph to light up
00070 uint8_t PageSensor::BinTemp(uint16_t TempSensorReading)
00071 {
00072     // The 7 values in this array are the lowest values of each "bin"
00073     // The values must be sorted from low to high, between 0x0000 and 0xFFFF
00074     const uint16_t TempBinArray[7] = {53500, 53600, 53700, 53800, 53900, 54000, 54100};
00075     uint8_t RetValue = 0;
00076     for (int i=0; i<7; i++)
00077     {
00078         if (TempSensorReading >= TempBinArray[i])
00079         {
00080             RetValue = (7-i);
00081         }
00082     }
00083     return(RetValue);
00084 }
00085 
00086 // The BinPres function takes the raw 16-bit AnalogIn data for the presssure sensor
00087 // and compares it to 7 binning values. It returns the bin number 0..6 which is the
00088 // number of segments of the OLED bar graph to light up
00089 uint8_t PageSensor::BinPres(uint16_t PresSensorReading)
00090 {
00091     // The 7 values in this array are the lowest values of each "bin"
00092     // The values must be sorted from low to high, between 0x0000 and 0xFFFF
00093     const uint16_t PresBinArray[7] = {0x0800, 0x0B00, 0x1000, 0x1400, 0x1800, 0x1B00, 0x2000};
00094     uint8_t RetValue = 0;
00095     for (int i=0; i<7; i++)
00096     {
00097         if (PresSensorReading >= PresBinArray[i])
00098         {
00099             RetValue = (7-i);
00100         }
00101     }
00102     return(RetValue);
00103 }
00104     
00105 void PageSensor::SensorPoll()
00106 {
00107     //sensorpoller.detach();
00108     
00109     AnalogIn RawTemp(p15);
00110     AnalogIn RawPres(p16);
00111     //printf("%i  %i  \n", RawTemp, RawPres);
00112     sensordata.temperature = BinTemp(RawTemp.read_u16());
00113     sensordata.pressure = BinPres(RawPres.read_u16());
00114        
00115     //sensorpoller.attach(callback(this, &Flexbook::PageSensor::SensorPoll), POLL_PERIOD);
00116 }
00117 
00118 } // End Flexbook namespace.
00119 
00120 
00121 
00122