MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
ldr.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "ldr.h"
16 
17 #include "utility.h"
18 
19 LDR::LDR(int samples, ADC * adc) : samples_(samples), adc_(adc) {}
20 
22 {
23  // return voltage in v
24  float v[samples_] = {0.f};
25  for (int i = 0; i < samples_; ++i) {
26  v[i] = adc_->read_voltage(ADCChannel::ldr);
27  delay(10);
28  }
29  return median(v, samples_);
30 }
Definition: adc.h:28
float read_voltage(ADCChannel ch)
Read voltage from ADC channel (computes voltage using the set fixed gain)
Definition: adc.cpp:39
float read_voltage()
Read voltage from ADC for LDR. Make sure ADC is initialised before calling this method.
Definition: ldr.cpp:21
LDR(int samples, ADC *adc)
Construct a new LDR object.
Definition: ldr.cpp:19
ADC adc
Definition: main.cpp:28
float median(float *arr, int size)
Calculate median of a float array.
Definition: utility.h:55