Tizen 2.0 Release
[adaptation/intel_mfld/sensor-plugins-mfld-blackbay.git] / src / ms5607processors.cpp
1 /* Medfield sensor plugins
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; version 2.1.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301USA
16  */
17
18 #include "ms5607processors.h"
19 #include "log.h"
20 #include <fstream>
21 #include <iostream>
22
23
24 const int UNSET = -1;
25 const int BUFSIZE = 128;
26
27 int Ms5607Reader::mCalibdata [] = {UNSET, UNSET,UNSET,UNSET,
28                                    UNSET,UNSET,UNSET,UNSET
29                                   };
30 int Ms5607Reader::mAdc1 = 0;
31 int Ms5607Reader::mAdc2 = 0;
32 long long int Ms5607Reader::mDT = 0;
33 long long int Ms5607Reader::mOFF = 0;
34 long long int Ms5607Reader::mSENS = 0;
35
36 Ms5607Reader::Ms5607Reader()
37 {
38      DbgPrint();
39      find_input_device_by_name("ms5607_pressure");
40      find_device_from_udev("baro");
41      mInputEventCount = 2;
42 }
43
44 bool Ms5607Reader::enable()
45 {
46      if (BaseProcessor::enable()) {
47           const int maxLines = sizeof(mCalibdata)/sizeof(mCalibdata[0]);
48
49           if (mCalibdata[0] == UNSET) {
50                std::string tmp = mSysfsPath;
51                tmp.append("/coeff");
52                std::fstream f(tmp.c_str(),std::ios::in);
53
54                int goodLines = 0;
55                while (f.good() && goodLines < maxLines) {
56                     std::string name;
57                     int value;
58                     f >> name >> value;
59                     if (f.fail())
60                          continue;
61
62                     mCalibdata[goodLines++] = value;
63                }
64
65                if (goodLines != maxLines) {
66                     mCalibdata[0] = UNSET;
67                     disable();
68                     return -1;
69                }
70
71                f.close();
72           }
73
74      }
75
76      for (int i = 0; i < 8; i++)
77           std::cout <<i <<" " <<mCalibdata[i] <<"\n";
78
79      return 0;
80 }
81
82 void Ms5607Reader::process_input_events(const std::vector <input_event *> &events)
83 {
84      BaseProcessor::process_input_events(events);
85      if (events.size() == 2) {
86           mAdc1 = mValues[0];
87           mAdc2 = mValues[1];
88
89           //These calculations are from mail
90           mDT = mAdc2 - (mCalibdata[5] <<8);
91           mOFF = (((long long) mCalibdata[2]) <<17) + ((mCalibdata[4]*mDT)>>6);
92           mSENS = (((long long)mCalibdata[1]) <<16) + ((mCalibdata[3] * mDT) >>7);
93
94      }
95 }
96
97 TemperatureProcessor::TemperatureProcessor()
98 {
99      mName = "Temperature";
100 }
101
102 bool TemperatureProcessor::fill_values(unsigned int type, int &count,
103                                        data_unit_idx_t &unit,
104                                        data_accuracy &accuracy)
105 {
106      count = 1;
107      accuracy = ACCURACY_NORMAL;
108
109      //This calculation was received in mail
110      mValues[0] = (2000 + (mDT * (mCalibdata[6]) >>23)) / 100.0;
111      return true;
112 }
113
114 PressureProcessor::PressureProcessor()
115 {
116      mName = "Pressure";
117 }
118
119 bool PressureProcessor::fill_values(unsigned int type, int &count,
120                                     data_unit_idx_t &unit,
121                                     data_accuracy &accuracy)
122 {
123      count = 1;
124      accuracy = ACCURACY_NORMAL;
125
126      //This calculation was received in mail
127      mValues[0] = (((mAdc1 * (mSENS >>21)) - mOFF) >> 15) / 100000.0;
128      return true;
129 }