/* LUFA Library Copyright (C) Dean Camera, 2018. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name of the author not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaims all warranties with regard to this software, including all implied warranties of merchantability and fitness. In no event shall the author be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of this software. */ #include #include #include #include "i2cmaster/i2cmaster.h" #define output_low(port,pin) port &= ~(1<Button = 0; MouseReport->Wheel = 0; MouseReport->X = 0; MouseReport->Y = 0; // sensor ready? uint8_t rdy = PINB&(1<<5); if (!rdy) { if (report_lift) { // we still have to end a short click event report_lift = 0; *ReportSize = sizeof(USB_WheelMouseReport_Data_t); return true; } return false; } i2c_start_wait(ADDR_SENSOR|I2C_WRITE); i2c_write(addr1); i2c_write(addr2); i2c_rep_start(ADDR_SENSOR|I2C_READ); for (int i=0; i<8; i++) { buf[i] = i2c_readAck(); } buf[8] = i2c_readNak(); i2c_stop(); int num_fingers = buf[0]; // absolute x and y coordinates are signed 16-bit integers int16_t xpos = ((uint16_t)buf[5]<<8)|((uint16_t)buf[6]); int16_t ypos = ((uint16_t)buf[7]<<8)|((uint16_t)buf[8]); // convert to relative deltas as float float dx = (float)(xpos-lastx); float dy = (float)(ypos-lasty); lastx = xpos; lasty = ypos; // touched? if (num_fingers) { touched_time++; wheeling = 0; if (start_num_fingers < num_fingers) { start_num_fingers = num_fingers; } if (start_num_fingers == 2) { // left mouse button MouseReport->Button |= 1; touched_time++; } else if (start_num_fingers == 3) { // wheel wheeling = 1; } else if (start_num_fingers == 4) { // right mouse button MouseReport->Button |= 2; } // skip dramatic values if (dx=-MOTION_CLIP && dy=-MOTION_CLIP) { // skip the first motion reading(s) immediately after // touchdown because they often have skips if (touched_time > 4) { if (wheeling) { dy/=5; if (dy>0 && dy<1) dy=1; MouseReport->Wheel = -dy; } else { // normal movement MouseReport->X = dx*1.5; MouseReport->Y = dy*1.2; } } } } else { // no (more) touches if (start_num_fingers == 1 && touched_time > 0 && touched_time <= PRESS_TIME) { // tapped for a short time with 1 finger? left click MouseReport->Button |= 1; } else if (start_num_fingers == 3 && touched_time > 0 && touched_time <= PRESS_TIME) { // tapped for a short time with 3 fingers? middle click MouseReport->Button |= 4; } touched_time = 0; start_num_fingers = 0; report_lift = 1; } last_num_fingers = num_fingers; // end cycle i2c_start_wait(ADDR_SENSOR|I2C_WRITE); i2c_write(0xee); i2c_write(0xee); i2c_write(0xff); i2c_stop(); *ReportSize = sizeof(USB_WheelMouseReport_Data_t); return true; } /** HID class driver callback function for the processing of HID reports from the host. * * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced * \param[in] ReportID Report ID of the received report from the host * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature * \param[in] ReportData Pointer to a buffer where the received report has been stored * \param[in] ReportSize Size in bytes of the received HID report */ void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, const uint8_t ReportID, const uint8_t ReportType, const void* ReportData, const uint16_t ReportSize) { // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports } int main(void) { SetupHardware(); GlobalInterruptEnable(); for (;;) { HID_Device_USBTask(&Mouse_HID_Interface); USB_USBTask(); } }