Midterm 2 Submission
Part 1:
a) ASensor.h header file:
b) ASensor.c file:
#ifndef ASENSOR_H
#define ASENSOR_H
#include
#define SENSOR_ADDRESS 0x74 //7-bit address 1110100
int GetVersion(I2C i2c);
int GetTotalSt
...
Midterm 2 Submission
Part 1:
a) ASensor.h header file:
b) ASensor.c file:
#ifndef ASENSOR_H
#define ASENSOR_H
#include
#define SENSOR_ADDRESS 0x74 //7-bit address 1110100
int GetVersion(I2C i2c);
int GetTotalStrength(I2C i2c);
int GetEvents(I2C i2c);
#endif
#include
#include “ASensor.h”
const int addr8bit = SENSOR_ADDRESS << 1;
// NOTE: unsigned char casting is used in the following functions because if the first
bit of any of the registers is 1, it might be interpreted as a negative number and sign
extended even though the bit manipulations used don’t edit any of the upper 16 bits of
the returned int.
int GetVersion(I2C i2c) {
char cmd[2] = {0x00, 0x04}; // address for major version
char stop[3] = {0xEE, 0xEE, 0x00}; // address of register to stop I2C with some data
i2c.write(addr8bit, cmd, 2, true); // write register without stop bit
i2c.read(addr8bit, cmd, 2); // repeated start to read
i2c.write(addr8bit, stop, 3); // manually stopping I2C as per the datasheet
// returns major and minor version separated by zeros. If major version was 239 and
minor version was 173, the value returned would be 239000173
return (unsigned char)cmd[0]*1000000 + (unsigned char)cmd[1];
}
int GetTotalStrength(I2C i2c) {
char cmd[2] = {0x00, 0x1A}; // address for touch strength
char stop[3] = {0xEE, 0xEE, 0x00}; // address of register to stop I2C with some data
i2c.write(addr8bit, cmd, 2, true); // send register without stop bit
i2c.read(addr8bit, cmd, 2); // repeated start to read
i2c.write(addr8bit, stop, 3); // manually stopping I2C as per the datasheet
// returns 32-bit int where the lowest 16 bits are cmd[0] and cmd[1] respectively
return ((unsigned char)cmd[0] << 8) | (unsigned char)cmd[1];
}
T -UPDATEDpdf/c & d) Main.cpp file:
Part 2:
int GetEvent(I2C i2c) {
char cmd[2] = {0x00, 0x1A}; // address for event gesture 0
char stop[3] = {0xEE, 0xEE, 0x00}; // address of register to stop I2C with some data
i2c.write(addr8bit, cmd, 2, true); // send register without stop pit
i2c.read(addr8bit, cmd, 2); // repeated start to read
i2c.write(addr8bit, stop, 3); // manually stopping I2C as per the datasheet
// check if the lower 5 bits of “Gesture Events 0” are all zero and that
// the lower 3 bits of “Gesture Events 1” are also all zero
if ((cmd[0] | 0xC0 == 0xC0) && (cmd[1] | 0xF8 == 0xF8)) return 0;
// returns 32-bit int where the lowest 16 bits are cmd[0] (Gesture Events 0) and
cmd[1] (Gesture Events 1) respectively
else return ((unsigned char)cmd[0] << 8) | (unsigned char)cmd[1];
}
#include
#include “ASensor.h”
int main() {
I2C i2c(I2C_SDA, I2C_SCL);
while(1) {
printf(“Event: %d”, GetEvent(i2c));
wait_us(500000); // wait 0.5 seconds
[Show More]