バッテリーの残量を見られたらいいなあと思って作りました

Committer:
Gaku0606
Date:
Sun Mar 05 15:53:00 2017 +0000
Revision:
0:c5746f241457
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gaku0606 0:c5746f241457 1 #ifndef BATTERY_MONITORING_HPP
Gaku0606 0:c5746f241457 2 #define BATTERY_MONITORING_HPP
Gaku0606 0:c5746f241457 3 /**
Gaku0606 0:c5746f241457 4 * @bref バッテリー電圧クラス
Gaku0606 0:c5746f241457 5 */
Gaku0606 0:c5746f241457 6 class battery_monitoring{
Gaku0606 0:c5746f241457 7
Gaku0606 0:c5746f241457 8 public:
Gaku0606 0:c5746f241457 9
Gaku0606 0:c5746f241457 10 battery_monitoring(float minV, float maxV, PinName pin);
Gaku0606 0:c5746f241457 11 private:
Gaku0606 0:c5746f241457 12 float _minV;
Gaku0606 0:c5746f241457 13 float _maxV;
Gaku0606 0:c5746f241457 14 AnalogIn _analog;
Gaku0606 0:c5746f241457 15 public:
Gaku0606 0:c5746f241457 16 float batteryLevel();
Gaku0606 0:c5746f241457 17 float debug();
Gaku0606 0:c5746f241457 18 };
Gaku0606 0:c5746f241457 19
Gaku0606 0:c5746f241457 20 battery_monitoring::battery_monitoring(float minV, float maxV, PinName pin) : _analog(pin){
Gaku0606 0:c5746f241457 21 _minV = minV;
Gaku0606 0:c5746f241457 22 _maxV = maxV;
Gaku0606 0:c5746f241457 23 }
Gaku0606 0:c5746f241457 24
Gaku0606 0:c5746f241457 25 float battery_monitoring::batteryLevel(){
Gaku0606 0:c5746f241457 26 float v = 0;
Gaku0606 0:c5746f241457 27 v = _analog.read();
Gaku0606 0:c5746f241457 28 for(int i = 0; i < 1000; i++){
Gaku0606 0:c5746f241457 29 v = (v + _analog.read()) / 2.0f;
Gaku0606 0:c5746f241457 30 }
Gaku0606 0:c5746f241457 31 v = 3.3f * v;
Gaku0606 0:c5746f241457 32 return (v - _minV) / ( _maxV - _minV);
Gaku0606 0:c5746f241457 33 }
Gaku0606 0:c5746f241457 34
Gaku0606 0:c5746f241457 35 float battery_monitoring::debug(){
Gaku0606 0:c5746f241457 36 return _analog.read();
Gaku0606 0:c5746f241457 37 }
Gaku0606 0:c5746f241457 38 #endif