Frank Duignan / Mbed OS NRF52832_ML8511
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UVSensor.h Source File

UVSensor.h

00001 #ifndef __UVSENSOR_H
00002 #define __UVSENSOR_H
00003 #include <mbed.h>
00004 #include <BLE.h>
00005 AnalogIn UVSignalIn(P0_28);
00006 
00007 class UVService {
00008 public:
00009     const static uint16_t UV_SERVICE_UUID = 0xA012;
00010     const static uint16_t UV_CHARACTERISTIC_UUID = 0xA013;
00011     
00012     UVService(BLE &_ble, int16_t initialUVValue) :
00013         ble(_ble), UVValue(UV_CHARACTERISTIC_UUID, &initialUVValue)
00014     {
00015         GattCharacteristic *charTable[] = {&UVValue};
00016         GattService uvservice(UV_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00017         ble.addService(uvservice);      
00018     }
00019 
00020     GattAttribute::Handle_t getValueHandle() const {
00021         return UVValue.getValueHandle();
00022     }
00023     void updateUVValue(uint16_t newValue) {
00024         ble.gattServer().write(UVValue.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
00025     }
00026 
00027     void poll()
00028     {       
00029         uint16_t ADCIn = UVSignalIn.read_u16();
00030         // Need to scale this to mW/cm^2
00031         // To convert to a voltage : * 3.6/4095
00032         // To convert voltage to mW/cm^2 see page 8 of datasheet for ML8511 sensor
00033         // This graph is for 3.0V, but the supply in this example is 3.3V. 
00034         // I'm ASSUMING that the device output is not a function of voltage supply (within reason)
00035         // From Graph : UV power / area is roughly given by:
00036         // Power = (Voltage - 1) * 7; 
00037         // Need to avoid unsigned overflow
00038         // An input of 1V should give an ADC Value of (1/3.6)*4095 = 1137.5 (1138)
00039         if (ADCIn >= 1137 )
00040             ADCIn = ADCIn - 1137;
00041         else
00042             ADCIn = 0;
00043         ADCIn = ADCIn  / 163; // 4095 / (7*3.6) = 163                   
00044         updateUVValue(ADCIn);                
00045     }
00046 private:
00047     BLE &ble;
00048     ReadOnlyGattCharacteristic<int16_t>  UVValue;
00049 
00050 };
00051 #endif