Project

General

Profile

Raspberry PI GPIO Scol plugin
rpii2c.cpp
1#include "rpii2c.h"
2#include "plugin.h"
3
4#include <string>
5#include <algorithm>
6#include <iostream>
7
8#ifdef RPI
9#include <wiringPiI2C.h>
10#endif
11
12//
13//Class RpiI2C
14//
15
16RpiI2C::RpiI2C()
17{
18}
19
20RpiI2C::RpiI2C(int deviceId): mDeviceId(deviceId)
21{
22 mFileHandler = 0;
23#ifdef RPI
24 mFileHandler = wiringPiI2CSetup(deviceId);
25#endif
26}
27
28RpiI2C::~RpiI2C()
29{
30#ifdef RPI
31 if (mFileHandler != 0)
32 {
33 //hmm nothing to do ?!
34 }
35#endif
36}
37
38int RpiI2C::Read()
39{
40#ifdef RPI
41 if (mFileHandler != 0)
42 {
43 return wiringPiI2CRead(mFileHandler);
44 }
45#endif
46 return 0;
47}
48
49void RpiI2C::Write(int value)
50{
51#ifdef RPI
52 if (mFileHandler != 0)
53 {
54 wiringPiI2CWrite(mFileHandler, value);
55 }
56#endif
57}
58
59void RpiI2C::WriteReg8(int reg, int value)
60{
61#ifdef RPI
62 if (mFileHandler != 0)
63 {
64 wiringPiI2CWriteReg8(mFileHandler, reg, value);
65 }
66#endif
67}
68
69int RpiI2C::ReadReg8(int reg)
70{
71#ifdef RPI
72 if (mFileHandler != 0)
73 {
74 return wiringPiI2CReadReg8(mFileHandler, reg);
75 }
76#endif
77 return 0;
78}
79
80void RpiI2C::WriteReg16(int reg, int value)
81{
82#ifdef RPI
83 if (mFileHandler != 0)
84 {
85 wiringPiI2CWriteReg16(mFileHandler, reg, value);
86 }
87#endif
88}
89
90int RpiI2C::ReadReg16(int reg)
91{
92#ifdef RPI
93 if (mFileHandler != 0)
94 {
95 return wiringPiI2CReadReg16(mFileHandler, reg);
96 }
97#endif
98 return 0;
99}