1
0

Mouse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #include <avr/pgmspace.h>
  27. #include <avr/io.h>
  28. #include <stdlib.h>
  29. #include "i2cmaster/i2cmaster.h"
  30. #define output_low(port,pin) port &= ~(1<<pin)
  31. #define output_high(port,pin) port |= (1<<pin)
  32. #define set_input(portdir,pin) portdir &= ~(1<<pin)
  33. #define set_output(portdir,pin) portdir |= (1<<pin)
  34. // Registers
  35. #define Product_ID 0x00
  36. #define Revision_ID 0x01
  37. #define Motion 0x02
  38. #define Delta_X_L 0x03
  39. #define Delta_X_H 0x04
  40. #define Delta_Y_L 0x05
  41. #define Delta_Y_H 0x06
  42. #define SQUAL 0x07
  43. #define Raw_Data_Sum 0x08
  44. #define Maximum_Raw_data 0x09
  45. #define Minimum_Raw_data 0x0A
  46. #define Shutter_Lower 0x0B
  47. #define Shutter_Upper 0x0C
  48. #define Control 0x0D
  49. #define Config1 0x0F
  50. #define Config2 0x10
  51. #define Angle_Tune 0x11
  52. #define Frame_Capture 0x12
  53. #define SROM_Enable 0x13
  54. #define Run_Downshift 0x14
  55. #define Rest1_Rate_Lower 0x15
  56. #define Rest1_Rate_Upper 0x16
  57. #define Rest1_Downshift 0x17
  58. #define Rest2_Rate_Lower 0x18
  59. #define Rest2_Rate_Upper 0x19
  60. #define Rest2_Downshift 0x1A
  61. #define Rest3_Rate_Lower 0x1B
  62. #define Rest3_Rate_Upper 0x1C
  63. #define Observation 0x24
  64. #define Data_Out_Lower 0x25
  65. #define Data_Out_Upper 0x26
  66. #define Raw_Data_Dump 0x29
  67. #define SROM_ID 0x2A
  68. #define Min_SQ_Run 0x2B
  69. #define Raw_Data_Threshold 0x2C
  70. #define Config5 0x2F
  71. #define Power_Up_Reset 0x3A
  72. #define Shutdown 0x3B
  73. #define Inverse_Product_ID 0x3F
  74. #define LiftCutoff_Tune3 0x41
  75. #define Angle_Snap 0x42
  76. #define LiftCutoff_Tune1 0x4A
  77. #define Motion_Burst 0x50
  78. #define LiftCutoff_Tune_Timeout 0x58
  79. #define LiftCutoff_Tune_Min_Length 0x5A
  80. #define SROM_Load_Burst 0x62
  81. #define Lift_Config 0x63
  82. #define Raw_Data_Burst 0x64
  83. #define LiftCutoff_Tune2 0x65
  84. #include "Mouse.h"
  85. /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
  86. static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_WheelMouseReport_Data_t)];
  87. /** LUFA HID Class driver interface configuration and state information. This structure is
  88. * passed to all HID Class driver functions, so that multiple instances of the same class
  89. * within a device can be differentiated from one another.
  90. */
  91. USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
  92. {
  93. .Config =
  94. {
  95. .InterfaceNumber = INTERFACE_ID_Mouse,
  96. .ReportINEndpoint =
  97. {
  98. .Address = MOUSE_EPADDR,
  99. .Size = MOUSE_EPSIZE,
  100. .Banks = 1,
  101. },
  102. .PrevReportINBuffer = PrevMouseHIDReportBuffer,
  103. .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
  104. },
  105. };
  106. // LM PD0 input pullup
  107. // RM PD1 input pullup
  108. #define ADDR_SENSOR (0x74<<1)
  109. uint8_t twi_write_reg[1];
  110. uint8_t twi_write_buf[10];
  111. uint8_t twi_read_buf[10];
  112. void SetupHardware(void)
  113. {
  114. /* Disable watchdog if enabled by bootloader/fuses */
  115. MCUSR &= ~(1 << WDRF);
  116. wdt_disable();
  117. // Disable clock division
  118. // this should yield 8Mhz with internal osc
  119. clock_prescale_set(clock_div_1);
  120. DDRD = 0b00000000;
  121. DDRB = 0b00000100;
  122. DDRC = 0b00000000;
  123. //output_high(PORTC, 5);
  124. // no jtag plox
  125. //MCUCR |=(1<<JTD);
  126. //MCUCR |=(1<<JTD);
  127. i2c_init();
  128. USB_Init();
  129. Delay_MS(100);
  130. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  131. i2c_write(0x04);
  132. i2c_write(0x32);
  133. i2c_write(2); // reset
  134. i2c_stop();
  135. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  136. i2c_write(0x04);
  137. i2c_write(0x32);
  138. i2c_write(0); // reset
  139. i2c_stop();
  140. Delay_MS(100);
  141. }
  142. /** Event handler for the library USB Connection event. */
  143. void EVENT_USB_Device_Connect(void)
  144. {
  145. }
  146. /** Event handler for the library USB Disconnection event. */
  147. void EVENT_USB_Device_Disconnect(void)
  148. {
  149. }
  150. /** Event handler for the library USB Configuration Changed event. */
  151. void EVENT_USB_Device_ConfigurationChanged(void)
  152. {
  153. bool ConfigSuccess = true;
  154. ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
  155. USB_Device_EnableSOFEvents();
  156. }
  157. /** Event handler for the library USB Control Request reception event. */
  158. void EVENT_USB_Device_ControlRequest(void)
  159. {
  160. HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
  161. }
  162. /** Event handler for the USB device Start Of Frame event. */
  163. void EVENT_USB_Device_StartOfFrame(void)
  164. {
  165. HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
  166. }
  167. uint8_t addr1 = 0x00;
  168. uint8_t addr2 = 0x11;
  169. uint8_t buf[80];
  170. int16_t lastx = 0, lasty = 0;
  171. unsigned int cycle = 0;
  172. int wheeling = 0;
  173. int touched_time = 0;
  174. int last_num_fingers = 0;
  175. int start_num_fingers = 0;
  176. int report_lift = 0;
  177. #define PRESS_TIME 6
  178. #define MOTION_CLIP 80
  179. /** HID class driver callback function for the creation of HID reports to the host.
  180. *
  181. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  182. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  183. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  184. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  185. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  186. *
  187. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  188. */
  189. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  190. uint8_t* const ReportID,
  191. const uint8_t ReportType,
  192. void* ReportData,
  193. uint16_t* const ReportSize)
  194. {
  195. if (ReportType==HID_REPORT_ITEM_Feature) return false;
  196. USB_WheelMouseReport_Data_t* MouseReport = (USB_WheelMouseReport_Data_t*)ReportData;
  197. MouseReport->Button = 0;
  198. MouseReport->Wheel = 0;
  199. MouseReport->X = 0;
  200. MouseReport->Y = 0;
  201. // sensor ready?
  202. uint8_t rdy = PINB&(1<<5);
  203. if (!rdy) {
  204. if (report_lift) {
  205. // we still have to end a short click event
  206. report_lift = 0;
  207. *ReportSize = sizeof(USB_WheelMouseReport_Data_t);
  208. return true;
  209. }
  210. return false;
  211. }
  212. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  213. i2c_write(addr1);
  214. i2c_write(addr2);
  215. i2c_rep_start(ADDR_SENSOR|I2C_READ);
  216. for (int i=0; i<8; i++) {
  217. buf[i] = i2c_readAck();
  218. }
  219. buf[8] = i2c_readNak();
  220. i2c_stop();
  221. int num_fingers = buf[0];
  222. // absolute x and y coordinates are signed 16-bit integers
  223. int16_t xpos = ((uint16_t)buf[5]<<8)|((uint16_t)buf[6]);
  224. int16_t ypos = ((uint16_t)buf[7]<<8)|((uint16_t)buf[8]);
  225. // convert to relative deltas as float
  226. float dx = (float)(xpos-lastx);
  227. float dy = (float)(ypos-lasty);
  228. lastx = xpos;
  229. lasty = ypos;
  230. // touched?
  231. if (num_fingers) {
  232. touched_time++;
  233. wheeling = 0;
  234. if (start_num_fingers < num_fingers) {
  235. start_num_fingers = num_fingers;
  236. }
  237. if (start_num_fingers == 2) {
  238. // left mouse button
  239. MouseReport->Button |= 1;
  240. touched_time++;
  241. } else if (start_num_fingers == 3) {
  242. // wheel
  243. wheeling = 1;
  244. } else if (start_num_fingers == 4) {
  245. // right mouse button
  246. MouseReport->Button |= 2;
  247. }
  248. // skip dramatic values
  249. if (dx<MOTION_CLIP && dx>=-MOTION_CLIP && dy<MOTION_CLIP && dy>=-MOTION_CLIP) {
  250. // skip the first motion reading(s) immediately after
  251. // touchdown because they often have skips
  252. if (touched_time > 4) {
  253. if (wheeling) {
  254. dy/=5;
  255. if (dy>0 && dy<1) dy=1;
  256. MouseReport->Wheel = -dy;
  257. } else {
  258. // normal movement
  259. MouseReport->X = dx*1.5;
  260. MouseReport->Y = dy*1.2;
  261. }
  262. }
  263. }
  264. } else {
  265. // no (more) touches
  266. if (start_num_fingers == 1 && touched_time > 0 && touched_time <= PRESS_TIME) {
  267. // tapped for a short time with 1 finger? left click
  268. MouseReport->Button |= 1;
  269. } else if (start_num_fingers == 3 && touched_time > 0 && touched_time <= PRESS_TIME) {
  270. // tapped for a short time with 3 fingers? middle click
  271. MouseReport->Button |= 4;
  272. }
  273. touched_time = 0;
  274. start_num_fingers = 0;
  275. report_lift = 1;
  276. }
  277. last_num_fingers = num_fingers;
  278. // end cycle
  279. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  280. i2c_write(0xee);
  281. i2c_write(0xee);
  282. i2c_write(0xff);
  283. i2c_stop();
  284. *ReportSize = sizeof(USB_WheelMouseReport_Data_t);
  285. return true;
  286. }
  287. /** HID class driver callback function for the processing of HID reports from the host.
  288. *
  289. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  290. * \param[in] ReportID Report ID of the received report from the host
  291. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  292. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  293. * \param[in] ReportSize Size in bytes of the received HID report
  294. */
  295. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  296. const uint8_t ReportID,
  297. const uint8_t ReportType,
  298. const void* ReportData,
  299. const uint16_t ReportSize)
  300. {
  301. // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
  302. }
  303. int main(void)
  304. {
  305. SetupHardware();
  306. GlobalInterruptEnable();
  307. for (;;)
  308. {
  309. HID_Device_USBTask(&Mouse_HID_Interface);
  310. USB_USBTask();
  311. }
  312. }