
Simple example of using getchar to read raw bytes from a Serial Terminal. You can use this example to look at the characters that PuTTY sends. That way, you might be able to build your own keyboard routines.
main.cpp@0:230a31b32cc1, 2016-04-29 (annotated)
- Committer:
- noutram
- Date:
- Fri Apr 29 07:49:43 2016 +0000
- Revision:
- 0:230a31b32cc1
Simple example of using getchar()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:230a31b32cc1 | 1 | #include "mbed.h" |
noutram | 0:230a31b32cc1 | 2 | #include "rtos.h" |
noutram | 0:230a31b32cc1 | 3 | |
noutram | 0:230a31b32cc1 | 4 | unsigned int delayms = 1000; |
noutram | 0:230a31b32cc1 | 5 | |
noutram | 0:230a31b32cc1 | 6 | void print_char(char c = '*') |
noutram | 0:230a31b32cc1 | 7 | { |
noutram | 0:230a31b32cc1 | 8 | printf("%c", c); |
noutram | 0:230a31b32cc1 | 9 | fflush(stdout); |
noutram | 0:230a31b32cc1 | 10 | } |
noutram | 0:230a31b32cc1 | 11 | |
noutram | 0:230a31b32cc1 | 12 | DigitalOut led1(LED1); |
noutram | 0:230a31b32cc1 | 13 | |
noutram | 0:230a31b32cc1 | 14 | void print_thread(void const *argument) |
noutram | 0:230a31b32cc1 | 15 | { |
noutram | 0:230a31b32cc1 | 16 | while (true) { |
noutram | 0:230a31b32cc1 | 17 | Thread::wait(delayms); |
noutram | 0:230a31b32cc1 | 18 | led1 = !led1; |
noutram | 0:230a31b32cc1 | 19 | } |
noutram | 0:230a31b32cc1 | 20 | } |
noutram | 0:230a31b32cc1 | 21 | |
noutram | 0:230a31b32cc1 | 22 | void read_keyboard(void const *argument) |
noutram | 0:230a31b32cc1 | 23 | { |
noutram | 0:230a31b32cc1 | 24 | char c; |
noutram | 0:230a31b32cc1 | 25 | while (true) { |
noutram | 0:230a31b32cc1 | 26 | |
noutram | 0:230a31b32cc1 | 27 | c = getchar(); |
noutram | 0:230a31b32cc1 | 28 | printf("\t%X\n", c); |
noutram | 0:230a31b32cc1 | 29 | } |
noutram | 0:230a31b32cc1 | 30 | } |
noutram | 0:230a31b32cc1 | 31 | |
noutram | 0:230a31b32cc1 | 32 | |
noutram | 0:230a31b32cc1 | 33 | int main() |
noutram | 0:230a31b32cc1 | 34 | { |
noutram | 0:230a31b32cc1 | 35 | printf("\n\n*** RTOS basic example ***\n"); |
noutram | 0:230a31b32cc1 | 36 | Thread t1(print_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); |
noutram | 0:230a31b32cc1 | 37 | Thread t2(read_keyboard); |
noutram | 0:230a31b32cc1 | 38 | |
noutram | 0:230a31b32cc1 | 39 | //Sleep the main thread |
noutram | 0:230a31b32cc1 | 40 | Thread::wait(osWaitForever); |
noutram | 0:230a31b32cc1 | 41 | } |