Serial io

Using serial communication to talk to the Arduino

But first:

solutions to the last assignment

Morse tree

The code above sends some special characters to test the tree based decoder

Morse tree

1 struct morse_tree tree_N_with_donald_hair =
2   {"Ñ", NULL, NULL};
3 struct morse_tree tree_funny_hat_G =
4   {"Ĝ", NULL, NULL};
5 struct morse_tree tree_tick =
6   {"'", NULL, NULL};

The code above receives the morse code and decodes it

LED Tree

1 uint32_t now= millis();
2 if(now >= next_tree) {
3   
4   cur_node= random(2) ?
5     cur_node->child_l : cur_node->child_r;
6   digitalWrite(cur_node->pin, HIGH);
7   
8 }

Uses millis() instead of delay() to meet timing requirements

Back to serial io

The serial port

Arduino UART-USB

Pins 0(RX) and 1(TX) are connected to an USB-Serial converter

The serial port

high ─┐ ┌─────┐     ┌────────────┐ ┌───┐           ┌─
      │ │     │     │            │ │   │           │
low   └─┘     └─────┘            └─┘   └───────────┘
bit#   S 0 1 2 3 4 5 6 7 E        S 0 1 2 3 4 5 6 7 E

UART (Serial) is a simple communication protocol

it uses one wire to receive data (RX) and one wire to transmit data (TX)

Serial.read()

We used Serial.print and Serial.println for Arduino ► Computer communication (on the TX line)

Now we will use Serial.read for Computer ► Arduino communication (on the RX line)

Serial.read() returns a single character or -1 if no character was received

Echo

1 void loop() {
2   char buffer[256]= {'\0'};
3   /* TODO */
4   Serial.print(buffer);
5 }

The code linked above contains a read_line function that reads a complete line from the serial port

Echo

use read_line to enable conversations like the following:

Hint: Configure serial monitor to send newline characters

resrever txet

1 void reverse_string(char *orig, char*rev)
2 {
3   size_t orig_len= strlen(orig);
4   size_t rev_idx= 0;
5   size_t fwd_idx= orig_len-1;
6 
7   // TODO
8 }

The code above contains a broken reverse_string function

resrever txet

repair reverse_string to enable conversations like the following:

rot13 pelcgb

1 void rot13_string(char *orig)
2 {
3   for(size_t idx=0 ; orig[idx]; idx++) {
4     char l= orig[idx];
5 
6     if(l >= 'a' && l <= 'z') {
7       // TODO
8     }
9 }

The code above contains a broken rot13 "encryption" function

rot13 pelcgb

In rot13 every letter is replaced by the letter 13 letters later in the alphabet

abcdefghijklmnopqrstuvwxyz
▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾
nopqrstuvwxyzabcdefghijklm

ABCDEFGHIJKLMNOPQRSTUVWXYZ
▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾
NOPQRSTUVWXYZABCDEFGHIJKLM

If applied twice rot13 yields the original text

rot13 pelcgb

Fix the rot13_string function to enable conversations like the following:

Regular expressions

[Mm][Aa]{1,10}(tch|gic)

Maaaagic matching machine

Regular expressions

Michael Corner, @edgesncorners
Dean Thomas, @magic_dean
Sue Li, @sureli
Demelza Robins, dem@magic.com
Susan Bones, sbones@minimagic.gov.uk

The list above contains Names and different kinds of contact informations

Terminal basics

Open a terminal/console/powershell window

Use wget to download the file linked in the previos slide

grep

On Linux/macOS the grep command can be used to find lines in files that match a regular expression

Windows users can try the sls command or a web based regular expression tester

grep

The general syntax is

Linux/macOS: $ grep -E "regular expression" filename

Windows: PS C:\> sls "regular expression" filename

The example above searches for all lines that contain a twitter handle as contact info

grep

Using the lecture slides and other ressources write regular expressions that match on lines that contain …