Programming > CODING SOLUTION > ECE-GY 6483 - New York University. Midterm 2 Submission. UPDATED (All)
Midterm 2 Submission Part 1: a) ASensor.h header file: b) ASensor.c file: #ifndef ASENSOR_H #define ASENSOR_H #include <mbed.h> #define SENSOR_ADDRESS 0x74 //7-bit address 1110100 int GetVersi... on(I2C i2c); int GetTotalStrength(I2C i2c); int GetEvents(I2C i2c); #endif #include <mbed.h> #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 <mbed.h> #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]
Last updated: 1 year ago
Preview 1 out of 5 pages
Instant download
Buy this document to get the full access instantly
Instant Download Access after purchase
Add to cartInstant download
Connected school, study & course
About the document
Uploaded On
Apr 29, 2023
Number of pages
5
Written in
This document has been written for:
Uploaded
Apr 29, 2023
Downloads
0
Views
39
In Browsegrades, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Browsegrades · High quality services·