0:20
everyone welcome to controllers tech
0:25
i have already covered how to use the
0:27
blue pill as a mouse for your computer
0:30
today in this video i will show you guys
0:33
how can we use it as a keyboard
0:36
this video is divided into two halves
0:40
first half will show some simple usage
0:44
and in the second half i will show some
0:48
actual keypad so let's start by creating
0:56
fast forwarding this part as it is a
0:58
very common thing at this point
1:03
now the basic setup is complete
1:06
select the usb and choose device fs
1:11
leave everything to default here now in
1:16
select the class as hid class
1:19
you can leave everything default here
1:21
too but i am going to make this small
1:25
this way computer will recognize the
1:27
device as stm32 keyboard
1:31
that's all here let's go to the clock
1:47
it have sorted out everything but i will
1:50
run the mcu at 72 megahertz
1:53
let's click save to generate the project
2:14
here is our main file this theme looks a
2:18
so i am going to change it
2:22
it looks better now first of all
2:26
open the usbd hid.c file
2:30
we need to make some changes there
2:33
scroll down to hid configuration
2:43
here is the line we need to change
2:47
as you can see 2 is for the mouse one
2:52
by default the code will always generate
2:55
the setup for the mouse
2:56
so we need to change this to one to use
3:01
make sure that you make the changes in
3:04
fs configuration only
3:05
and not in any other configurations
3:13
now scroll down to mouse report
3:22
this here is the descriptor for the
3:24
mouse and we need to change it with the
3:34
i have the keyboard descriptor
3:36
downloaded right here
3:38
you can google it or i will leave the
3:40
link to it in the description
3:43
copy these descriptors and replace them
3:46
with the mouse descriptors
3:48
we will leave the function name to mouse
3:50
itself because other functions might be
3:53
and we don't want to mess things up also
4:08
looks like i need to build it first
4:19
okay now i can access it change the size
4:25
that's all for this setup remember that
4:29
whenever you generate the project from
4:32
you need to do this all over because
4:34
cube mx will generate for the mouse
4:37
let's go back to our main file
4:40
first of all include the usbd hid.h
5:03
we need to declare the usb device handle
5:15
make sure you declare it as an extern
5:25
now before we create a structure for the
5:28
let's see the keyboard protocol
5:34
here in case of keyboard we need to send
5:39
this data contains the modifier keys
5:42
second byte is reserved
5:50
the modifier keys can be all these keys
6:03
let's create a structure to store these
6:08
i will call it keyboard hid
6:13
let's initialize the structure with all
6:24
looks like 1-0 is missing
6:30
now let's see the key codes
6:38
here is the list of all the key codes
6:42
like 0 cross 0 4 for a 5 for b
6:53
it's a huge list and i will leave the
6:55
link for this pdf in the description
7:01
i will just send an a for now
7:09
let's build it once before going forward
7:16
looks like i spelled keyboard wrong here
7:27
now everything is fine
7:30
i will send the value to the key code 1.
7:35
i am sending an a here this basically
7:40
is pressed and now send the report
7:50
wait for 50 milliseconds and now send
7:56
to indicate that the key was released
8:08
and i want this process to repeat every
8:17
we have some warning about the structure
8:21
let's flash it to the mcu now
8:34
let me open the device list also
8:45
now i am connecting the usb
8:54
seems like some descriptor issue
9:03
you can see here the device has been
9:05
recognized as the sdm 32 keyboard
9:09
and it's ending in a every one second
9:24
you can see wherever i click it's
9:27
printing every second
9:28
just like as if it is a keyboard
9:45
things are working all right now let's
9:48
try some more combinations
9:59
this time i am going to use the left
10:04
this will print the upper case letters
10:07
to use the left shift the bit 1 must be
10:10
so the modifier value will be 0 cross 0
10:22
let's write the zero cross zero 2 to the
10:31
i will leave the key code 1 to print a
10:34
but this time it will print up a case a
10:37
as we press the left shift we need to
10:47
let's use one more key code
10:50
we can use six key codes at the same
10:53
time so the keyboard can print six keys
10:57
i will just print two keys together
11:03
let's build now and flash
11:17
the keyboard is connected and it's
11:20
printing up a case a
11:21
and b at the same time
11:43
so everything is working perfect
11:46
like i said we can send six key codes at
11:50
so it can print six letters or numbers
11:54
this is it for the basic tutorial
11:57
next part of this video will cover an
11:59
actual application of keyboard
12:01
and here i am going to use a 4x4 keypad
12:04
as a keyboard i have already covered how
12:08
to interface the keypad with stm32
12:12
you can check out the video on the top
12:16
i am going to use the same code for this
12:19
also first of all i need to add pins for
12:30
select four input pins for the columns
12:33
output pins for the rows
12:39
use the pull-up resistance for the input
12:43
i have already covered all this in the
12:46
watch that if you don't understand the
12:49
click save to generate the new project
12:58
we need to modify the hid.c file again
13:03
i will fast forward this part of the
13:27
so this is the main file from that
13:32
i am going to use these functions as it
14:09
in the while loop we will read the key
14:12
which was pressed on the keypad
14:15
if the key pressed is a one we need to
14:17
send the key code for one
14:28
if we look in the pdf the hex value for
14:43
so we will put that in the key code one
14:47
similarly write the values for other
14:50
too here is the final code for numbers
14:56
now if the key pressed is capital a we
14:59
need to first press the shift
15:14
similarly we will write for other
15:18
because this keypad have a b c
15:21
and d so we need to write for them too
15:30
we also need to write for the star and
15:49
star is basically shift pressed with the
15:53
and hashes shift with the number three
16:28
that's all now we need to send this
16:40
wait for 50 milliseconds
16:50
send the key released report also
16:58
and i am going to comment out this code
17:12
we don't have any errors during testing
17:15
i found out that the key value is always
17:19
so i am adding this part that the
17:21
keyboard part will only work
17:23
if the value is not zero cross zero one
17:27
this is not that necessary as we do have
17:29
conditions for the keys pressed
17:32
let's flash it to the controller now
17:47
i have attached to the keypad this time
17:50
the keyboard gets detected on connecting
17:54
let's try pressing these keys now
18:05
you can see the values are printing just
18:22
and we can type them anywhere we want
18:38
this is it for this video i hope you
18:41
understood it you can use any other
18:45
or any other device for this purpose
18:49
like you are to receive data and then
18:51
transmit to the computer as a keyboard
18:55
well i will leave the application up to
18:59
you can download the code the keyboard
19:03
and the pdf from the links in the
19:06
keep watching and have a nice day ahead