-
Notifications
You must be signed in to change notification settings - Fork 1
/
Si1153.cpp
124 lines (94 loc) · 3.01 KB
/
Si1153.cpp
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
#include <Arduino.h>
#include "Si1153.h"
Si1153::Si1153() {
//empty constructor
}
/**
* Configures a channel at a given index
*/
void Si1153::config_channel(uint8_t index, uint8_t *conf){
int len = sizeof(conf);
if(len != 4 || index < 0 || index > 5)
return;
int inc = index * len;
param_set(Si1153::ADCCONFIG_0 + inc, conf[0]);
param_set(Si1153::ADCSENS_0 + inc, conf[1]);
param_set(Si1153::ADCPOST_0 + inc, conf[2]);
param_set(Si1153::MEASCONFIG_0 + inc, conf[3]);
}
/**
* Writes data over i2c
*/
void Si1153::write_data(uint8_t addr, uint8_t *data, size_t len){
Wire.beginTransmission(addr);
Wire.write(data, len);
Wire.endTransmission();
}
/**
* Reads data from a register over i2c
*/
int Si1153::read_register(uint8_t addr, uint8_t reg, int bytesOfData){
int val = -1;
Si1153::write_data(addr, ®, sizeof(reg));
Wire.requestFrom(addr, bytesOfData);
if(Wire.available())
val = Wire.read();
return val;
}
/**
* param set as shown in the datasheet
*/
void Si1153::param_set(uint8_t loc, uint8_t val){
uint8_t packet[2];
do {
int cmmnd_ctr = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_0, 1);
packet[0] = Si1153::HOSTIN_0;
packet[1] = val;
Si1153::write_data(Si1153::DEVICE_ADDRESS, packet, sizeof(packet));
packet[0] = Si1153::COMMAND;
packet[1] = loc | (0B10 << 6);
Si1153::write_data(Si1153::DEVICE_ADDRESS, packet, sizeof(packet));
int r = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_0, 1);
} while(r > cmmnd_ctr);
}
/**
* param query as shown in the datasheet
*/
int Si1153::param_query(uint8_t loc){
int result = -1;
uint8_t packet[2];
do {
int cmmnd_ctr = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_0, 1);
packet[0] = Si1153::COMMAND;
packet[1] = loc | (0B01 << 6);
Si1153::write_data(Si1153::DEVICE_ADDRESS, packet, sizeof(packet));
int r = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_0, 1);
} while(r > cmmnd_ctr);
result = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_1, 1);
return result;
}
/**
* Sends command to the command register
*/
void Si1153::send_command(uint8_t code){
uint8_t packet[2];
do {
int cmmnd_ctr = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_0, 1);
packet[0] = Si1153::COMMAND;
packet[1] = code;
Si1153::write_data(Si1153::DEVICE_ADDRESS, packet, sizeof(packet));
int r = Si1153::read_register(Si1153::DEVICE_ADDRESS, Si1153::RESPONSE_0, 1);
} while(r > cmmnd_ctr);
}
/**
* Returns int given a byte array
*/
int Si1153::get_int_from_bytes(uint8_t *data, size_t len){
int result = 0;
int shift = 8 * len;
for(int i = 0; i < len; i++){
shift -= 8;
result |= ((data[i] << shift) & (0xFF << shift));
}
return result;
}