+/rtos
git clone https://git.julia.blue/+/rtos
ee76735
master
1#include "mouse.h" 2#include "OS.h" 3#include "printf.h" 4#include "std.h" 5#include "tivaware/gpio.h" 6#include "tivaware/hw_memmap.h" 7#include "tivaware/rom.h" 8#include "tivaware/sysctl.h" 9#include "usblib/device/usbdhidmouse.h" 10#include "usblib/usblib.h" 11 12const uint8_t LangDescriptor []= {4 ,USB_DTYPE_STRING , 13USBShort (USB_LANG_EN_US )}; 14const uint8_t ManufacturerString []= { 15 (4 + 1 )* 2 ,USB_DTYPE_STRING ,'R' ,0 ,'T' ,0 ,'O' ,0 ,'S' ,0 }; 16const uint8_t ProductString []= { 17 (4 + 1 )* 2 ,USB_DTYPE_STRING ,'L' ,0 ,'a' ,0 ,'b' ,0 ,'7' ,0 }; 18const uint8_t SerialNumberString []= {(8 + 1 )* 2 ,USB_DTYPE_STRING , 19'1' ,0 , 20'2' ,0 , 21'3' ,0 , 22'4' ,0 , 23'5' ,0 , 24'6' ,0 , 25'7' ,0 , 26'8' ,0 }; 27const uint8_t HIDInterfaceString []= 28"(\x03H\0I\0D\0 \0M\0o\0u\0s\0e\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" ; 29 30const uint8_t ConfigString []= 31"0\x03H\0I\0D\0 \0M\0o\0u\0s\0e\0 \0C\0o\0n\0f\0i\0g\0u\0r\0a\0t\0i\0o\0n" ; 32 33#define NUM_STRING_DESCRIPTORS (sizeof(StringDescriptors) / sizeof(uint8_t*)) 34 35const uint8_t * const StringDescriptors []= { 36LangDescriptor ,ManufacturerString ,ProductString , 37SerialNumberString ,HIDInterfaceString ,ConfigString }; 38 39static Sema4 mouse_ready ; 40uint32_t mouse_handler (void * pvCBData ,uint32_t ui32Event ,uint32_t ui32MsgData , 41void * pvMsgData ) { 42switch (ui32Event ) { 43case USB_EVENT_CONNECTED : { 44printf ("\nHost Connected...\r\n" ); 45OS_Signal (& mouse_ready ); 46break ; 47 } 48case USB_EVENT_DISCONNECTED : { 49printf ("\nHost Disconnected...\r\n" ); 50break ; 51 } 52case USB_EVENT_TX_COMPLETE : { 53// printf("TX complete\n\r"); 54OS_Signal (& mouse_ready ); 55break ; 56 } 57case USB_EVENT_SUSPEND : { 58OS_Signal (& mouse_ready ); 59printf ("\nBus Suspended\n\r" ); 60break ; 61 } 62case USB_EVENT_RESUME : { 63OS_Signal (& mouse_ready ); 64printf ("\nBus Resume\n\r" ); 65break ; 66 } 67// We ignore all other events. 68default : { 69break ; 70 } 71 } 72return 0 ; 73} 74 75tUSBDHIDMouseDevice mouse_dev = { 76// The Vendor ID you have been assigned by USB-IF. 77 .ui16VID = 0xFFFF , 78// The product ID you have assigned for this device. 79 .ui16PID = 0xFFFF , 80// The power consumption of your device in milliamps. 81 .ui16MaxPowermA = 0 , 82 .ui8PwrAttributes = USB_CONF_ATTR_SELF_PWR , 83// A pointer to your mouse callback event handler. 84 .pfnCallback = mouse_handler , 85// A value that you want passed to the callback alongside every event. 86 .pvCBData = (void * )& mouse_dev , 87// A pointer to your string table. 88 .ppui8StringDescriptors = StringDescriptors , 89// The number of entries in your string table. This must equal 90// (1 + (5 * (num languages))). 91 .ui32NumStringDescriptors = NUM_STRING_DESCRIPTORS }; 92 93typedef enum {LEFT = 1 ,RIGHT ,MIDDLE }MouseButton ; 94 95static bool held_down [3 ]= {false, false, false}; 96void mouse_toggle (MouseButton button ) { 97-- button ; 98OS_Wait (& mouse_ready ); 99USBDHIDMouseStateChange (& mouse_dev ,0 ,0 , 100held_down [button ] ?0 :1 <<button ); 101held_down [button ]= !held_down [button ]; 102} 103 104void mouse_click (MouseButton button ) { 105mouse_toggle (button ),mouse_toggle (button ); 106} 107 108void mouse_move (int8_t x ,int8_t y ) { 109OS_Wait (& mouse_ready ); 110USBDHIDMouseStateChange (& mouse_dev ,x ,y ,0 ); 111} 112 113static char continuous_dir = ' ' ; 114static int8_t speed = 5 ; 115static bool circle = false; 116 117void mouse_continuous (void ) { 118while (true) { 119OS_Sleep (hz (60 )); 120switch (continuous_dir ) { 121case 'q' :mouse_move (- speed ,- speed );break ; 122case 'w' :mouse_move (0 ,- speed );break ; 123case 'e' :mouse_move (speed ,- speed );break ; 124case 'a' :mouse_move (- speed ,0 );break ; 125case 'd' :mouse_move (speed ,0 );break ; 126case 'z' :mouse_move (- speed ,speed );break ; 127case 'x' :mouse_move (0 ,speed );break ; 128case 'c' :mouse_move (speed ,speed );break ; 129 } 130 } 131} 132 133void mouse_circle (void ) { 134const float f = 1 ;// in radians per second 135float t = 0 ; 136while (circle ) { 137mouse_move (speed * cos (t ),speed * sin (t )); 138t += f /60 ; 139if (t > PI ) { 140t -= 2 * PI ; 141 } 142OS_Sleep (hz (60 )); 143 } 144} 145 146void mouse_center (void ) { 147continuous_dir = ' ' ; 148circle = false; 149const uint16_t width = 1920 ; 150const uint16_t height = 1080 ; 151for (int i = 0 ;i < width /100 ;++ i ) {mouse_move (-100 ,0 ); } 152for (int i = 0 ;i < width /100 /2 ;++ i ) {mouse_move (100 ,0 ); } 153for (int i = 0 ;i < height /100 ;++ i ) {mouse_move (0 ,-100 ); } 154for (int i = 0 ;i < height /100 /2 ;++ i ) {mouse_move (0 ,100 ); } 155for (int i = 0 ;i < 3 ;++ i ) { 156if (held_down [i ]) { 157mouse_toggle (i + 1 ); 158 } 159 } 160} 161 162bool mouse_cmd (char c ) { 163const int8_t s = 10 ; 164switch (c ) { 165case 127 :return false; 166case '=' : 167case '+' :++ speed ; gotoshow ; 168case '-' : 169case '_' : 170-- speed ; 171show : 172printf ("\x1b[K\x1b[GSpeed %d" ,speed ); 173break ; 174case ',' :mouse_click (LEFT );break ; 175case '.' :mouse_click (MIDDLE );break ; 176case '/' :mouse_click (RIGHT );break ; 177case '<' :mouse_toggle (LEFT );break ; 178case '>' :mouse_toggle (MIDDLE );break ; 179case '?' :mouse_toggle (RIGHT );break ; 180case ' ' :mouse_center ();break ; 181case 'Q' :mouse_move (- s ,- s );break ; 182case 'W' :mouse_move (0 ,- s );break ; 183case 'E' :mouse_move (s ,- s );break ; 184case 'A' :mouse_move (- s ,0 );break ; 185case 'D' :mouse_move (s ,0 );break ; 186case 'Z' :mouse_move (- s ,s );break ; 187case 'X' :mouse_move (0 ,s );break ; 188case 'C' :mouse_move (s ,s );break ; 189case 'S' : 190case 's' : 191case 'q' : 192case 'w' : 193case 'e' : 194case 'a' : 195case 'd' : 196case 'z' : 197case 'x' : 198case 'c' :continuous_dir = c ;break ; 199case 'O' : 200case 'o' : 201if (circle ) { 202circle = false; 203 }else { 204circle = true; 205OS_AddThread (mouse_circle ,"spin" ,256 ,2 ); 206 } 207break ; 208 } 209return true; 210} 211 212void mouse_init (void ) { 213OS_InitSemaphore (& mouse_ready ,-1 ); 214ROM_GPIOPinTypeUSBAnalog (GPIO_PORTD_BASE ,GPIO_PIN_4 |GPIO_PIN_5 ); 215USBStackModeSet (0 ,eUSBModeForceDevice ,0 ); 216 217if (!USBDHIDMouseInit (0 ,& mouse_dev )) { 218printf ("error: hidmouseinit\n\r" ); 219 } 220OS_Wait (& mouse_ready ); 221OS_AddThread (mouse_continuous ,"Continuous mouse movement" ,512 ,2 ); 222OS_Sleep (seconds (1.5f )); 223}