0:18
everyone welcome to controllers tech
0:23
today in this video i will show you guys
0:26
how to emulate stm32f103
0:29
ak a blue pill as a mouse to control the
0:32
cursor on your computer
0:34
this is more like a gravity mouse as it
0:37
involves the accelerometer
0:39
from which we get the accelerations in x
0:43
the mouse movement will be based on
0:45
these acceleration values
0:48
let's start by creating a new project in
1:12
give some name to the project and click
1:20
here is our cube mx first of all
1:24
i am selecting external crystal for the
1:28
next select serial wire debug
1:31
now i am selecting the i2c1 to connect
1:34
the adxl345 accelerometer
1:46
next select usb and enable device fs
1:51
i am leaving everything to default here
1:54
in the usb device select the class as
2:02
you can leave everything default
2:05
i am changing the name to show that it
2:10
this is the name for my device and this
2:12
will pop up in the computer
2:14
after i connect the usb
2:27
let's enable the on-board led also
2:33
we will use it as an indication
2:36
select the pin pa7 as the xd pin
2:39
as i will connect the button to it let's
2:48
pa7 is the external interrupt pin
2:51
where the mouse button will be connected
2:56
this is how i am going to connect the
3:00
when the button is pressed the 3.3 volts
3:04
pa7 and the mcu will detect a high in
3:09
i know the current will sink into the
3:12
but as we are only using the 3.3 volts
3:16
this current won't be that much
3:23
i am changing the trigger to falling
3:25
edge you can use the rising edge also
3:31
i am using pull down here because i want
3:33
this pin to act as a ground for the
3:37
also change the output of pc13 to high
3:41
so that the led is off in default state
4:06
i am setting the max clock for the
4:12
let's check if everything is covered we
4:19
i forgot to enable the interrupt for the
4:23
now all is okay so let's save this to
4:32
by the time we will look into the
4:41
4 5 adxl345 connected via the i2c with
4:45
and b7 it is powered with 3.3 volts from
5:07
here we have our main file
5:13
let's first include the usb hid.h
5:32
open the usb device.c and copy the usb
5:35
handle typedef from here
5:47
i have created a structure for mouse
5:49
data which includes button
5:51
x value y value and the wheel
5:55
it should be created in the same order
6:02
i am initializing everything with the
6:20
let's create the adxl related functions
6:25
i have already covered these in the
6:29
you can check it on the top right corner
6:37
before proceeding further let's declare
6:41
that we are going to use these are the
6:45
and maximum values for x and y
6:48
these will change after we calibrate the
6:52
button flag will be use in the interrupt
6:56
here is the function to calibrate the
6:59
values i am reading the adxl raw values
7:05
after taking 50 samples this function
7:09
and new minimum values this whole
7:14
seconds and once completed the led on
7:17
board will light up to indicate the
7:33
now let's write for the external
7:36
we need the xd callback function for
7:47
if the interrupt is triggered by the pin
7:50
then set the button flag to 1.
7:53
rest of the processing for the button
7:55
will be done in the while loop
8:02
let's move to the main function now
8:05
initialize the adxl calibrate it
8:13
now in the while loop we will get new x
8:27
if the x value is less than the minimum
8:31
we have our new x value which is the
8:33
difference between them
8:42
and if the x value is greater than the
8:46
then the new x value will be the
8:48
difference between them
8:53
this is basically to make sure that the
8:55
little movement of the sensor doesn't
8:56
get updated as the mouse movement
8:59
we do the similar things for the y
9:14
let's give a delay of 10 milliseconds
9:17
first we will test this part of the code
9:20
to make sure the data is correct
9:22
and as per our requirement let's build
9:33
right now the sensor is calibrating for
9:38
green light is on so the calibration is
9:42
check the values here we have new max
9:45
and new minimum values note that when i
9:50
the y values are increasing in positive
9:57
tilting towards left and y values are
10:05
tilting towards the front x becomes
10:08
and towards back x becomes positive
10:31
in this diagram the red color is
10:34
and green color is our values
10:37
as you can see in the right direction
10:40
computer have positive x axis
10:42
and we have positive y values and
10:45
in the upward direction computer have
10:49
and we have negative x values so
10:53
all we need to do is send the sensors y
10:55
values to the x coordinate of the mouse
10:58
and sensors x values to the y coordinate
11:10
if the variation in the x values is more
11:14
we will send the value to the
11:15
y-coordinate of the mouse
11:28
and the same for the x-coordinate of the
11:32
here i chose 20 and you can choose any
11:36
based on how sensitive you want the
11:40
if the values are less than 20 there
11:42
will be no changes in the coordinates
11:50
now our data is ready to be sent to the
11:56
send report function is used to send the
12:08
let's build this we have a warning but
12:13
let's debug our code now
12:32
i will directly run this
12:38
let's open the device manager
12:45
or better open the devices in the
12:49
i am opening the devices to show you how
12:52
the mouse will be detected
12:54
let's connect the usb now
13:02
as you can see stm32f103 mouse
13:06
gets detected here it's being detected
13:09
in the mouse or keyboard category
13:12
also windows is detecting it as a mouse
13:16
as i lift it up you can see the cursor
13:20
but let's reset it once so the code can
13:23
calibrate sensor for this orientation
13:31
led is on so the calibration is done
13:35
you can see the movement is as per the
13:37
tilting of the device
13:39
but this acceleration is too fast
13:43
i want to slow this down
13:51
to do so i need to reduce these values
13:54
so i am dividing them by three you can
13:57
choose any other divisor based on how
13:59
much acceleration you want
14:02
we also need to add the button to the
14:06
if you remember in the interrupt
14:09
we set the button flag now
14:12
if the button flag is 1 we will write
14:14
value 1 to the button of the mouse
14:17
value 1 indicates the left click
14:21
if you want to emulate the right click
14:25
after writing the value send the report
14:28
to indicate the click
14:30
we also need to send the button value as
14:33
to indicate the button was released
14:36
let's add a delay of 50 milliseconds
14:38
between these reports
14:41
so first click and then released
14:44
and in the end set the button flag to
14:48
let's build and run now
15:01
again we will do the calibration first
15:21
you can see movement is pretty smooth
15:24
the button works too i have found out
15:27
that this button works better
15:29
if we set the rising edge trigger to the
15:32
so you can try that if you have some
15:43
anyway single click and double click
15:46
works all right here also
16:11
this is it for this video i hope you
16:14
understood the process
16:16
you can use any other accelerometer also
16:20
gy 500 or something similar
16:25
in the next video i will cover the
16:28
where i will try to emulate stm32f103 as
16:34
you can download the code from the link
16:38
keep watching be safe and have a nice