File: HddSensor.cpp

package info (click to toggle)
libexadrums 0.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: cpp: 6,970; ansic: 220; makefile: 160; sh: 12
file content (80 lines) | stat: -rw-r--r-- 1,125 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Sensor.cpp
 *
 *  Created on: 7 May 2015
 *      Author: jeremy
 */

#include "HddSensor.h"

#include <fstream>
#include <iostream>
#include <ctime>

namespace IO
{


	const std::vector<std::string> HddSensor::dataFiles({"out.raw", "out.raw", "out.raw"});


	HddSensor::HddSensor(const std::string& dataFolder, size_t chan) : path(dataFolder), index(0)
	{
		this->channel = chan;
		return;
	}


	short HddSensor::GetData()
	{

		if(data.empty())
		{
			ReadData();
		}

		// Wait for a few microseconds
		{
			clock_t endwait = clock() + (5.0f * (double)CLOCKS_PER_SEC) / 1000000.0f ;
			while (clock() < endwait);
		}

		short val = data[++index];

		if(index == data.size())
		{
			index = 0;
		}

		return val;
	}

	// Private Methods

	void HddSensor::ReadData()
	{

		std::string fileName = dataFiles[channel % dataFiles.size()];
		std::string fileLoc(path + fileName);

		std::ifstream file(fileLoc, std::ifstream::binary);

		if(!file.good())
		{
			throw - 1;
		}

		short val;

		while(file.read(reinterpret_cast<char*>(&val), sizeof(short)))
		{
			data.push_back(val);
		}

		file.close();


		return;
	}

}