MNT Reform: Open Source Portable Computer https://mntre.com/reform
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
11KB

  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. uint8_t adns_init_complete=0;
  85. volatile int xydat[2];
  86. #include "Mouse.h"
  87. /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
  88. static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_WheelMouseReport_Data_t)];
  89. /** LUFA HID Class driver interface configuration and state information. This structure is
  90. * passed to all HID Class driver functions, so that multiple instances of the same class
  91. * within a device can be differentiated from one another.
  92. */
  93. USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
  94. {
  95. .Config =
  96. {
  97. .InterfaceNumber = INTERFACE_ID_Mouse,
  98. .ReportINEndpoint =
  99. {
  100. .Address = MOUSE_EPADDR,
  101. .Size = MOUSE_EPSIZE,
  102. .Banks = 1,
  103. },
  104. .PrevReportINBuffer = PrevMouseHIDReportBuffer,
  105. .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
  106. },
  107. };
  108. void led_error(void) {
  109. DDRC = 0b11110100;
  110. }
  111. void led_ok(void) {
  112. DDRC = 0;
  113. }
  114. // FIXME QUESTIONABLE
  115. int convTwosComp(int b) {
  116. //Convert from 2's complement
  117. if (b & 0x80) {
  118. b = -1 * ((b ^ 0xff) + 1);
  119. }
  120. return b;
  121. }
  122. // LM PD0 input pullup
  123. // RM PD1 input pullup
  124. #define ADDR_SENSOR (0x74<<1)
  125. uint8_t twi_write_reg[1];
  126. uint8_t twi_write_buf[10];
  127. uint8_t twi_read_buf[10];
  128. void SetupHardware(void)
  129. {
  130. /* Disable watchdog if enabled by bootloader/fuses */
  131. MCUSR &= ~(1 << WDRF);
  132. wdt_disable();
  133. // Disable clock division
  134. // this should yield 8Mhz with internal osc
  135. clock_prescale_set(clock_div_1);
  136. DDRD = 0b00000000;
  137. DDRB = 0b00000100;
  138. DDRC = 0b00000000;
  139. //output_high(PORTC, 5);
  140. // no jtag plox
  141. //MCUCR |=(1<<JTD);
  142. //MCUCR |=(1<<JTD);
  143. i2c_init();
  144. USB_Init();
  145. Delay_MS(100);
  146. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  147. i2c_write(0x04);
  148. i2c_write(0x32);
  149. i2c_write(2); // reset
  150. i2c_stop();
  151. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  152. i2c_write(0x04);
  153. i2c_write(0x32);
  154. i2c_write(0); // reset
  155. i2c_stop();
  156. Delay_MS(100);
  157. }
  158. /** Event handler for the library USB Connection event. */
  159. void EVENT_USB_Device_Connect(void)
  160. {
  161. }
  162. /** Event handler for the library USB Disconnection event. */
  163. void EVENT_USB_Device_Disconnect(void)
  164. {
  165. }
  166. /** Event handler for the library USB Configuration Changed event. */
  167. void EVENT_USB_Device_ConfigurationChanged(void)
  168. {
  169. bool ConfigSuccess = true;
  170. ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
  171. USB_Device_EnableSOFEvents();
  172. }
  173. /** Event handler for the library USB Control Request reception event. */
  174. void EVENT_USB_Device_ControlRequest(void)
  175. {
  176. HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
  177. }
  178. /** Event handler for the USB device Start Of Frame event. */
  179. void EVENT_USB_Device_StartOfFrame(void)
  180. {
  181. HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
  182. }
  183. uint8_t addr1 = 0x00;
  184. uint8_t addr2 = 0x11;
  185. uint8_t buf[80];
  186. int16_t lastx = 0, lasty = 0;
  187. uint8_t ignore_next = 1;
  188. int pressed_time = 0;
  189. int moved_while_pressed = 0;
  190. int pressed_button = 0;
  191. unsigned int cycle = 0;
  192. unsigned int clicked_in_cycle = 0;
  193. int wheeling = 0;
  194. #define MOVE_THRS 3
  195. #define PRESS_TIME 6
  196. /** HID class driver callback function for the creation of HID reports to the host.
  197. *
  198. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  199. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  200. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  201. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  202. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  203. *
  204. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  205. */
  206. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  207. uint8_t* const ReportID,
  208. const uint8_t ReportType,
  209. void* ReportData,
  210. uint16_t* const ReportSize)
  211. {
  212. if (ReportType==HID_REPORT_ITEM_Feature) return false;
  213. int8_t nx = 0;
  214. int8_t ny = 0;
  215. uint8_t rdy = PINB&(1<<5);
  216. if (!rdy) {
  217. return false;
  218. }
  219. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  220. i2c_write(addr1);
  221. i2c_write(addr2);
  222. i2c_rep_start(ADDR_SENSOR|I2C_READ);
  223. for (int i=0; i<8; i++) {
  224. buf[i] = i2c_readAck();
  225. }
  226. buf[8] = i2c_readNak();
  227. i2c_stop();
  228. USB_WheelMouseReport_Data_t* MouseReport = (USB_WheelMouseReport_Data_t*)ReportData;
  229. MouseReport->Wheel = 0;
  230. MouseReport->X = 0;
  231. MouseReport->Y = 0;
  232. if (buf[0]) {
  233. int16_t xpos = ((uint16_t)buf[5]<<8)|((uint16_t)buf[6]);
  234. int16_t ypos = ((uint16_t)buf[7]<<8)|((uint16_t)buf[8]);
  235. pressed_time++;
  236. float dx = xpos-lastx;
  237. float dy = ypos-lasty;
  238. if (!wheeling) {
  239. if (!pressed_button && buf[0]==1) {
  240. pressed_button = 1;
  241. }
  242. if (buf[0]==2 && pressed_button==1) {
  243. MouseReport->Button |= 1;
  244. pressed_button = 2;
  245. }
  246. }
  247. if (!pressed_button && buf[0]==3) {
  248. MouseReport->Button |= 2;
  249. pressed_button = 3;
  250. wheeling = 0;
  251. }
  252. if (!ignore_next) {
  253. if (dx>MOVE_THRS || dx<-MOVE_THRS || dy>MOVE_THRS || dy<-MOVE_THRS) {
  254. moved_while_pressed = 1;
  255. }
  256. if (buf[0]==2 && !pressed_button) {
  257. if (dy>127) dy = 127;
  258. if (dy<-127) dy = -127;
  259. dy/=5;
  260. if (dy>0 && dy<1) dy=1;
  261. MouseReport->Wheel = -dy;
  262. wheeling = 1;
  263. } else if (!wheeling) {
  264. dx=dx*1.6;
  265. dy=dy*1.2;
  266. if (dx<100 && dx>=-100 && dy<100 && dy>=-100) {
  267. MouseReport->X = dx;
  268. MouseReport->Y = dy;
  269. }
  270. }
  271. }
  272. ignore_next = 0;
  273. lastx = xpos;
  274. lasty = ypos;
  275. } else {
  276. ignore_next = 1;
  277. if (!wheeling && pressed_button==1 && pressed_time>0 && pressed_time<PRESS_TIME && !moved_while_pressed) {
  278. // fixme click
  279. MouseReport->Button = 1;
  280. clicked_in_cycle = cycle;
  281. }
  282. if (pressed_button==2) {
  283. MouseReport->Button &= ~1;
  284. }
  285. if (pressed_button==3) {
  286. MouseReport->Button &= ~2;
  287. }
  288. pressed_time = 0;
  289. pressed_button = 0;
  290. moved_while_pressed = 0;
  291. wheeling = 0;
  292. }
  293. // end cycle
  294. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  295. i2c_write(0xee);
  296. i2c_write(0xee);
  297. i2c_write(0xff);
  298. i2c_stop();
  299. *ReportSize = sizeof(USB_WheelMouseReport_Data_t);
  300. return true;
  301. }
  302. /** HID class driver callback function for the processing of HID reports from the host.
  303. *
  304. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  305. * \param[in] ReportID Report ID of the received report from the host
  306. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  307. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  308. * \param[in] ReportSize Size in bytes of the received HID report
  309. */
  310. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  311. const uint8_t ReportID,
  312. const uint8_t ReportType,
  313. const void* ReportData,
  314. const uint16_t ReportSize)
  315. {
  316. // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
  317. }
  318. int main(void)
  319. {
  320. SetupHardware();
  321. GlobalInterruptEnable();
  322. for (;;)
  323. {
  324. HID_Device_USBTask(&Mouse_HID_Interface);
  325. USB_USBTask();
  326. }
  327. }