STM32-P103: add usbmouse preset
[bertos.git] / boards / stm32-p103 / examples / usbmouse / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief BeRTOS Virtual USB Mouse.
36  *
37  * This project implements a HID USB device using the Olimex STM32-P103
38  * evaluation board. The virtual mouse can be connected to the USB port of a
39  * normal PC.
40  *
41  * The WAKE_UP button on the board can be pressed to send to the host the mouse
42  * movement to draw a rectangle shape (taking the left button pressed).
43  *
44  * Example (mouse detection on Linux):
45  *
46  * $ dmesg | tail
47  * [25361.710435] usb 1-8.1.3: new full speed USB device using ehci_hcd and address 33
48  * [25361.943901] usb 1-8.1.3: configuration #1 chosen from 1 choice
49  * [25362.004002] input: BeRTOS USB Mouse as /devices/pci0000:00/0000:00:1d.7/usb1/1-8/1-8.1/1-8.1.3/1-8.1.3:1.0/input/input19
50  * [25362.004738] generic-usb 0003:FFFF:0000.0008: input,hidraw3: USB HID v1.10 Mouse [BeRTOS USB Mouse] on usb-0000:00:1d.7-8.1.3/input0
51  *
52  */
53
54 #include "hw/hw_led.h"
55
56 #include <cfg/debug.h>
57
58 #include <cpu/irq.h>
59
60 #include <drv/kbd.h>
61 #include <drv/timer.h>
62 #include <drv/usbmouse.h>
63
64 static void init(void)
65 {
66         /* Enable all the interrupts */
67         IRQ_ENABLE;
68
69         /* Initialize debugging module (allow kprintf(), etc.) */
70         kdbg_init();
71         /* Initialize system timer */
72         timer_init();
73         /* Initialize LED driver */
74         LED_INIT();
75         /* Enable the WAKE_UP button */
76         kbd_init();
77         /* Initialize the USB mouse device */
78         usbmouse_init(0);
79 }
80
81 int main(void)
82 {
83         int i;
84
85         /* Hardware initialization */
86         init();
87
88         /* Put your code here... */
89         kprintf("USB HID Mouse configured\n");
90         kbd_setRepeatMask(K_WAKEUP);
91         while (1)
92         {
93                 /* Wait until WAKE_UP is pressed */
94                 kbd_get();
95
96                 /* Left button pressed down */
97                 usbmouse_sendEvent(0, 0, BV(0));
98                 timer_delay(100);
99
100                 /* Move left */
101                 for (i = 0; i < 25; i++)
102                 {
103                         usbmouse_sendEvent(-2, 0, BV(0));
104                         timer_delay(10);
105                 }
106                 /* Move down */
107                 for (i = 0; i < 25; i++)
108                 {
109                         usbmouse_sendEvent(0, 2, BV(0));
110                         timer_delay(10);
111                 }
112                 /* Move right */
113                 for (i = 0; i < 25; i++)
114                 {
115                         usbmouse_sendEvent(2, 0, BV(0));
116                         timer_delay(10);
117                 }
118                 /* Move up */
119                 for (i = 0; i < 25; i++)
120                 {
121                         usbmouse_sendEvent(0, -2, BV(0));
122                         timer_delay(10);
123                 }
124                 /* Left button released */
125                 usbmouse_sendEvent(0, 0, 0);
126         }
127 }