dichotemy.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "dichotemy.h"
  2. #include "pointing_device.h"
  3. #include "report.h"
  4. void uart_init(void) {
  5. SERIAL_UART_INIT();
  6. }
  7. void pointing_device_task(void){
  8. report_mouse_t currentReport = {};
  9. SERIAL_UART_INIT();
  10. uint32_t timeout = 0;
  11. //the m character requests the RF slave to send the mouse report
  12. SERIAL_UART_DATA = 'm';
  13. //trust the external inputs completely, erase old data
  14. uint8_t uart_data[5] = {0};
  15. //there are 10 bytes corresponding to 10 columns, and an end byte
  16. for (uint8_t i = 0; i < 5; i++) {
  17. //wait for the serial data, timeout if it's been too long
  18. //this only happened in testing with a loose wire, but does no
  19. //harm to leave it in here
  20. while(!SERIAL_UART_RXD_PRESENT){
  21. timeout++;
  22. if (timeout > 10000){
  23. break;
  24. }
  25. }
  26. uart_data[i] = SERIAL_UART_DATA;
  27. }
  28. //check for the end packet, bits 1-4 are movement and scroll
  29. //but bit 5 has bits 0-3 for the scroll button state
  30. //(1000 if pressed, 0000 if not) and bits 4-7 are always 1
  31. //We can use this to verify the report sent properly.
  32. if (uart_data[4] == 0x0F || uart_data[4] == 0x8F)
  33. {
  34. currentReport = pointing_device_get_report();
  35. //shifting and transferring the info to the mouse report varaible
  36. //mouseReport.x = 127 max -127 min
  37. currentReport.x = uart_data[0];
  38. //mouseReport.y = 127 max -127 min
  39. currentReport.y = uart_data[1];
  40. //mouseReport.v = 127 max -127 min (scroll vertical)
  41. currentReport.v = uart_data[2];
  42. //mouseReport.h = 127 max -127 min (scroll horizontal)
  43. currentReport.h = uart_data[3];
  44. //mouseReport.buttons = 0x31 max (bitmask for mouse buttons 1-5) 0x00 min
  45. //mouse buttons 1 and 2 are handled by the keymap, but not 3
  46. if (uart_data[4] == 0x0F) { //then 3 is not pressed
  47. currentReport.buttons &= ~MOUSE_BTN3; //MOUSE_BTN3 is def in report.h
  48. } else { //3 must be pressed
  49. currentReport.buttons |= MOUSE_BTN3;
  50. }
  51. pointing_device_set_report(currentReport);
  52. }
  53. pointing_device_send();
  54. }
  55. void led_init(void) {
  56. DDRD |= (1<<1);
  57. PORTD |= (1<<1);
  58. DDRF |= (1<<4) | (1<<5);
  59. PORTF |= (1<<4) | (1<<5);
  60. }
  61. void matrix_init_kb(void) {
  62. // put your keyboard start-up code here
  63. // runs once when the firmware starts up
  64. matrix_init_user();
  65. uart_init();
  66. led_init();
  67. }
  68. void matrix_scan_kb(void) {
  69. // put your looping keyboard code here
  70. // runs every cycle (a lot)
  71. matrix_scan_user();
  72. }
  73. void led_set_kb(uint8_t usb_led) {
  74. }