Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Include core modules */
- #include "stm32f4xx.h"
- /* Include my libraries here */
- #include "defines.h"
- #include "tm_stm32f4_usb_hid_device.h"
- #include "tm_stm32f4_delay.h"
- #include "tm_stm32f4_disco.h"
- void Uart3_Init();
- TM_USB_HIDDEVICE_Mouse_t Mouse;
- int main(void) {
- uint8_t already = 0;
- /* Initialize system */
- SystemInit();
- /* Initialize leds */
- TM_DISCO_LedInit();
- /* Initialize button */
- TM_DISCO_ButtonInit();
- /* Initialize delay */
- TM_DELAY_Init();
- /* Initialize USB HID Device */
- TM_USB_HIDDEVICE_Init();
- /*Initialize USART3*/
- Uart3_Init();
- /* Set default values for mouse struct */
- TM_USB_HIDDEVICE_MouseStructInit(&Mouse);
- while (1) {
- /* If we are connected and drivers are OK */
- /* If you pressed button right now and was not already pressed */
- if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */
- already = 1;
- } else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */
- already = 0;
- /*
- Send a signals for dragging cursor.
- */
- Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;
- Mouse.XAxis = 0;
- Mouse.YAxis = 0;
- TM_USB_HIDDEVICE_MouseSend(&Mouse);
- Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;
- Mouse.XAxis = 50;
- Mouse.YAxis = 50;
- TM_USB_HIDDEVICE_MouseSend(&Mouse);
- Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Released;
- Mouse.XAxis = 0;
- Mouse.YAxis = 0;
- TM_USB_HIDDEVICE_MouseSend(&Mouse);
- }
- else {
- /* Turn off green LED */
- TM_DISCO_LedOff(LED_GREEN);
- }
- }
- }
- void Uart3_Init(){
- /******** ?? USART?GPIO ??? ********/
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- /******** ?? GPIOC?USART3 ? RCC ?? ********/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- /******** ? PC10?PC11 ??? USART3 ********/
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
- /******** ?? PC10 ? Tx ?? ********/
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /******** ?? PC11 ? Rx ?? ********/
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /******** USART ?????? ********/
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART3, &USART_InitStructure);
- /******** ?? USART3 ********/
- USART_Cmd(USART3, ENABLE);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- /* ?? USART3 ?? */
- NVIC_EnableIRQ(USART3_IRQn);
- }
- uint8_t MouseDataBUFF[5];
- int buffIndex = 0;
- void USART3_IRQHandler(){
- uint8_t mouse_signal;
- while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET){}
- /* read 1 byte via usart3 it */
- mouse_signal = USART_ReceiveData(USART3);
- /*MouseDataBuff spec
- these signals be read from usart3
- index 0 -> Does left button be clicked? 0 is click, 1 is release
- index 1 -> Does right button be clicked? 0 is click, 1 is release
- index 2 -> Does middle button be clicked? 0 is click, 1 is release
- index 3 -> X's movement
- index 4 -> Y's movement
- */
- MouseDataBUFF[buffIndex++] = mouse_signal;
- /*
- When uart3 get 5 bytes mouse signals, I can decide a set of Mouse value.
- */
- if(buffIndex == 5)
- {
- if(MouseDataBUFF[0] == 0)
- {
- Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;
- }
- else
- {
- Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Released;
- }
- Mouse.XAxis = MouseDataBUFF[3];
- Mouse.YAxis = MouseDataBUFF[4];
- buffIndex=0;
- TM_USB_HIDDEVICE_MouseSend(&Mouse);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement