-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMtch112.cpp
More file actions
90 lines (80 loc) · 3.11 KB
/
Copy pathMtch112.cpp
File metadata and controls
90 lines (80 loc) · 3.11 KB
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
/*
MTCH112.h - Test library for Microchip MTCH112
Dual-Channel Proximity/Touch Controller
*/
// include this library's description file
#include "Arduino.h"
#include "Mtch112.h"
// include description files for other libraries used (if any)
#include "Wire.h"
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
MTCH112::MTCH112()
{
Wire.begin();
}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
void MTCH112::Recalibrate() {
// Clear bit 2 of the CONFIG_CALCON0 register to force recalibration.
WriteReg(CONFIG_CALCON0,((0x2)));
}
unsigned char MTCH112::ReadReg(unsigned char register_address)
{
Wire.beginTransmission (MTCH112_I2C_DEFUALT_ADDR); // turn on sending
Wire.write (register_address); // Write to ADXL345 register map address
Wire.endTransmission (); // End to send
Wire.requestFrom (MTCH112_I2C_DEFUALT_ADDR, 1); // Request two bytes for ADXL345
unsigned char val;
if (Wire.available () <= 1) // Get <= 2 data
{
val = Wire.read ();
}
}
void MTCH112::WriteReg(unsigned char register_address, unsigned char value)
{
unsigned char currVal=ReadReg(register_address);
Serial.print("WAS: ");
Serial.print(register_address,HEX);Serial.print("=");Serial.print(currVal);
Serial.print(" changing to ");Serial.println(value);
if (value != currVal){
Wire.beginTransmission(MTCH112_I2C_DEFUALT_ADDR);
Wire.write(WP_BYTE_H);
Wire.write(WP_BYTE_L);
Wire.write(register_address);
Wire.write(value);
Wire.write((WP_BYTE_H ^ WP_BYTE_L ^ value ^ register_address));
Wire.endTransmission(); // stop transmitting
}
else{
Serial.println("Register already set...skipping EEPROM write.");
}
delay(EPROM_WRITE_DELAY);
Serial.print("NOW: ");
Serial.print(register_address,HEX);Serial.print("=");Serial.println(ReadReg(register_address));
}
void MTCH112::Setup(unsigned int timeout_value, unsigned char outcon_value, unsigned char lpcon_value)
{
unsigned char timeout_L_value = (unsigned char) (timeout_value & 0x00ff);
unsigned char timeout_H_value = (unsigned char) (timeout_value & 0xff00 >> 8);
WriteReg(CONFIG_TIMEOUT_L,timeout_L_value); //default = 0xff
WriteReg(CONFIG_TIMEOUT_H, timeout_H_value); //default = 0xff
WriteReg(CONFIG_OUTCON, outcon_value); //default = 0x0C
WriteReg(CONFIG_LPCON, lpcon_value); //default = 0x0C
}
unsigned int MTCH112::ReadData(unsigned char register_address)
{
Wire.beginTransmission (MTCH112_I2C_DEFUALT_ADDR); // turn on sending
Wire.write (register_address); // Write to ADXL345 register map address
Wire.endTransmission (); // End to send
Wire.requestFrom (MTCH112_I2C_DEFUALT_ADDR, 2); // Request two bytes for ADXL345
if (Wire.available () <= 2) // Get <= 2 data
{
unsigned char bytel = Wire.read(); //check byte order
unsigned char byteh = Wire.read();
unsigned int val = (byteh << 8);
val = val | bytel;
// Serial.print(bytel);Serial.print(" ");Serial.println(byteh);
return val;
}
}