summaryrefslogtreecommitdiffstats
path: root/Master/Real-Time Systems/mki/test/mkidemo.cpp
blob: e5f8729bd99aa87310a16dd060a070df8e3828e1 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//============================================================================
// Name:
//   mkidemo.cpp
//
// Summary:
//   Extremly simple demo program that shows some values
//   on the mercedes combi instrument. needs libcanio to work.
//
//   Default interface is can0, to change it start the program like this:
//     mkidemo -f=can1
//
// Created on:
//   Okt 1, 2010
//
// Author:
//   Christian Steiger
//============================================================================


// includes
#include "../src/mki.h"

// Function prototypes
const char* const fetchinterface (const int argc, const char* const argv[]);

// Main function
int main(int argc, char *argv[])
{
	int mph =0;
	int temperatur=40;
	int rpm =0;
	// get the interface data from the command line
	const char* const interface = fetchinterface(argc, argv);

	// create mki object
	mki m;

	// connect to interface, stop if we cant connect
	if (m.connect(interface) < 0)
		return -1;

	//m.getInterface()->disableLoopBack();

	// set informations we want to display
	//m.setLamps(MKI_WARN | MKI_FUEL_LOW | MKI_WARN_BLINK | MKI_ABS);

	//m.setDisplay(MKI_LIMIT | MKI_PERMANENT | MKI_MPH_BLINK );
	m.setDisplay(MKI_LIMIT | MKI_PERMANENT );
	// m.setAlert( MKI_TIRE_PRESSURE );

	// send the data in an endless loop
	// every 50ms or so should be enough,
	// use pthreads if you want it out of the way
	while (1) {
		mph++;
		temperatur++;
		rpm = rpm + 100;

		m.setMph(mph);
		m.setDisplayMph(mph);
		m.setRpm(rpm);
		m.setCWTemp(temperatur);

		m.sendData();
		usleep(100000);
		
		if(temperatur >= 130)
			temperatur = 40;
			
		if(mph > 160  )
			mph=0;
		
		if(rpm>6000)
			m.setLamps( MKI_WARN_BLINK );
			
		if(rpm > 7000)
		{
			rpm = 0;
			m.setLampsOff();
		}
		
	}

	// everything ok
	return 0;
}

// quick and dirty hack with code from PeakCanLogger
// to allow the interface to be set via commandline.
const char* const fetchinterface (const int argc, const char* const argv[])
{
	int i, len;
	char* interface = new char(5);
	strcpy( interface, "can0" );

	// go through the argument, ignore the first one
	for( i = 1; i < argc; i++ )
	{
		// check the arguments first and third char, is it a config parameter?
		if ( argv[i][0] == '-' )
		{
			// check which config parameter it is
			switch( argv[i][1] )
			{
				// set can-device
			case 'd':
			case 'f':
				// delete the actual char string
				delete interface;

				// get the lenght of the new string
				len = strlen(&argv[i][3]);

				// create a new one
				interface = new char( len );

				// set string with zeros
				memset( interface, 0, len );

				// and copy it
				strcpy( interface, &argv[i][3] );

				default:
					break;
			}
		}
	}

	return interface;
}