Lab 5 - Serial Parser, String Manipulation & Libraries

Similar documents
Lab 4 - Asynchronous Serial Communications

Lab 2 - Powering the Fubarino, Intro to Serial, Functions and Variables

Lab 2 - Powering the Fubarino. Fubarino,, Intro to Serial, Functions and Variables

Lesson 8: Digital Input, If Else

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Lecture 7. Processing Development Environment (or PDE)

The Big Idea: Background: About Serial

Arduino - DigitalReadSerial

Rotary Encoder Basics

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

CTEC 1802 Embedded Programming Labs

C:\Users\Jacob Christ\Documents\MtSAC\ELEC74 Mt SAC - chipkit\homework Sheets.docx

Exen Mini. Setup Guide - V1. nerdonic.com

4Serial SIK BINDER //77

Exen Mini. Setup Guide - V2. nerdonic.com

Physics 364, Fall 2012, Lab #9 (Introduction to microprocessor programming with the Arduino) Lab for Monday, November 5

Introduction to Arduino

Overview. Multiplexor. cs281: Introduction to Computer Systems Lab02 Basic Combinational Circuits: The Mux and the Adder

Specification. 1.Power Supply direct from Microcontroller Board. 2.The circuit can be used with Microcontroller Board such as Arduino UNO R3.

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail

MP3 Trigger v2 User Guide

Blinking an LED 1 PARTS: Circuit 2 LED. Wire. 330Ω Resistor

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

Building your own special-purpose embedded system gadget.

FTDI SmartBasic Hookup Guide

Binary Representations, and the Teensy 3.5

REQUIRED MATERIALS Epiphany-DAQ board Wire Jumpers Switch LED Resistors Breadboard Multimeter (if needed)

This tutorial will show you how to take temperature readings using the Freetronics temperature sensor and an Arduino Uno.

Magic 8 Ball. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

Laboratory 10. Programming a PIC Microcontroller - Part II

Button Input: On/off state change

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Grove - Thumb Joystick

FUNCTIONS USED IN CODING pinmode()

VLSI Design Automation Final Project Due: June 26 th, Project: A Router

Procedure: Determine the polarity of the LED. Use the following image to help:

CSC 258 lab notes, Fall 2003

CSCE 436/836: Embedded Systems Lab 1b: Hoverboard Programming Introduction

Robotics and Electronics Unit 5

Basic Input/Output Operations

Arduino Prof. Dr. Magdy M. Abdelhameed

Fall Harris & Harris

MICROPROCESSOR B.Tech. th ECE

Digispark DIY: the Smallest USB Arduino

DS18B20+ Digital Temperature Sensor

How to Restore a chipkit Bootloader

Connecting Arduino to Processing a

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

3.The circuit board is composed of 4 sets which are 16x2 LCD Shield, 3 pieces of Switch, 2

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

Sten-SLATE ESP Kit. Description and Programming

Dual rocket altimeter using the ATmega 328 microcontroller. The AltiDuo

Arduino Workshop. Overview. What is an Arduino? Why Arduino? Setting up your Arduino Environment. Get an Arduino based board and usb cable

Manual. Specifications. Contents. Options

Arduino notes 3: Digital input via buttons

Fubarino Mini Reference Manual

Review of the syntax and use of Arduino functions, with special attention to the setup and loop functions.

Name. CPTR246 Spring '17 (100 total points) Exam 2

Counter & LED (LED Blink)

ZX Distance and Gesture Sensor SMD Hookup Guide

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

Mini Panel. Accy and Macro Controller. Build the kind of control panels you ve always wanted without complicated wiring!

CPU Temperature Logging with CircuitPython

Lab 8. Communications between Arduino and Android via Bluetooth

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

Experiment 2 Fall 2012

IAS0430 MICROPROCESSOR SYSTEMS

Lab 02 Arduino 數位感測訊號處理, SPI I2C 介面實驗. More Arduino Digital Signal Process

IME-100 Interdisciplinary Design and Manufacturing

Adafruit DS3231 Precision RTC Breakout

Forth for Education - 4E4th and 4E4th IDE

Schedule. Sanford Bernhardt, Sangster, Kumfer, Michalaka. 3:10-5:00 Workshop: Build a speedometer 5:15-7:30 Dinner and Symposium: Group 2

UTC Lupton Renaissance Fund Podium Users Manual

Thumb Joystick Retail. Tools and parts you'll need. Things you'll want to know. How does it work? Skill Level: Beginner. by MikeGrusin March 22, 2011

EARTH PEOPLE TECHNOLOGY. VISIPORT 2 USB To Serial Adapter User Manual

PM2.5 Air Quality Sensor

keyestudio Keyestudio MEGA 2560 R3 Board

Connecting Arduino to Processing

Seeeduino LoRaWAN. Description

1 Getting started with Processing

You can examine the contents of a single memory location by typing a single address followed by a Return.

ECE 354 Computer Systems Lab II. Memory and Indirect Addressing

Control Tone with IR Remote

Welcome to Lab! You do not need to keep the same partner from last lab. We will come around checking your prelabs after we introduce the lab

Freeduino USB 1.0. Arduino Compatible Development Board Starter Guide. 1. Overview

Connecting to Director. Before setting up Director for the first time run through the check list below:

USER MANUAL ARDUINO I/O EXPANSION SHIELD

Introduction to Arduino. Wilson Wingston Sharon

BV4615. Dual Interface Zero Keypad. Product specification. Dec 2009 V0.a. ByVac Page 1 of 11

Input Shield For Arduino SKU: DFR0008

Laboratory 5 Communication Interfaces

Manual 601: : USB/RS232. Specifications. Contents. Options

Adding Peripherals to DCS

EECE 2411/2211-Introduction to Electrical and Computer Engineering Lab. Lab 3

Arduino Platform Part I

Grove - Thumb Joystick

ipot Phase 1 1

Proper Debugging of ATSAMD21 Processors

char errormsg[100]; //This is a generic array used for sprintf of error messages

Transcription:

Lab 5 - Serial Parser, String Manipulation & Libraries At the end of the previous lab you got a taste of adding a parser to your project. A parser is simply a program that looks for (parses) information from a stream of data (in our case strings of data coming from the serial port). In this lab we are going to expand our skills with the parser to allow complex bidirectional communications to occur that will enable us to make a program that executes code on our command as well as tell us about what has just happened using the written word and a serial terminal. The general term in computer science for extracting information from a more complex set of data is called a parser and indeed to parse means to extract information from. Are you ready to learn about parsers? Then don't miss this lab! Part 1 Software Reset NOTE: This functionality only works on chipkit boards. Arduino boards do not support software reset in this manner. There are workarounds that are available for Arduino but at this writing there is no standardized support in the Arduino wiring libraries. If you are attempting this class with an Arduino borrow a board to evaluate and get this section of the lab checked off. In a previous lab we looked at the functionality of the reset button. As a reminder, when the reset button is pressed on the Fubarino SD the PIC32 chip is reset to its power on state and starts running the bootloader program. On a Fubarino SD, and other chipkit board that have a program input, the bootloader looks at the state of this input. If the input is asserted the bootloader enters a mode where it waits for a new program to be sent over the serial port to be programmed in to the memory of the chip. If the program input is not asserted, then the bootloader tries to run a previously loaded program. The chipkit-core library includes a function called executesoftreset that allows us, in software, to reset the board. The function takes a single parameter that allows us to choose the functionality after reset. Either we can enter the bootloader to accept a new program or we can run a sketch that was previously programmed. The two ways to call the function are shown below: // Run previously loaded sketch after executing the soft reset executesoftreset(run_sketch_on_boot); // Enter the bootloader after executing the soft reset executesoftreset(enter_bootloader_on_boot); Some chipkit boards that have an external USB chip and typically will not support the ENTER_BOOTLOADER_ON_BOOT option. If this is the case the function will return a false value and your program will continue execution. // ITES LAB3-1 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Hook a pullup resistor up to pin 22 of your board and try this program: void setup() { pinmode(pin_led1, OUTPUT); pinmode(22, INPUT); digitalwrite(pin_led1, HIGH); delay(2000); digitalwrite(pin_led1, LOW); delay(2000); void loop() { digitalwrite(pin_led1, HIGH); delay(300); digitalwrite(pin_led1, LOW); delay(50); if(digitalread(22) == LOW) { executesoftreset(run_sketch_on_boot); If you pull the input low with a switch this program reset the board! Modify the program to read a second input on pin 12 that allows the board to enter the bootloader. Hint: don t forget to set the pin mode for pin 12 and just add a second if statement below the first to check pin 12. When both pin 12 and pin 22 will reset the board, call over the instructor to demonstrate your work. // ITES LAB3-2 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Part 2 - Parse, Execute, Respond and Reset From now on, most labs will be building on functionality of previous labs. From time to time a simple example will be shown to illustrate a point in isolation for clarity. Start off by opening the code from the last part of the previous lab and save it with a new name for this lab (maybe lab5_part2 might be a good name). At the end of the setup() add a 3 second delay. If you don t remember how to use the delay function, refer to lab 2. After the delay transmit the string "Command Parser" out the serial port so that we know the program is starting. Note: This function is specific to the bootloader on the chipkit boards and will not work on Arudino. Fubarino SD and won't function correctly on other boards. Create a function called reset() that takes no parameters and returns a void type to the end of your program: void reset(void) { Serial.println("Close serial terminal, resetting board in..."); uint8_t sec = 5; while (sec >= 1) { Serial.print(sec, DEC); Serial.println(" seconds..."); delay(1000); sec--; executesoftreset(enter_bootloader_on_boot); Create a new function called blink() that is called when the command "blink" is received by the parser. The blink() should turn on the PIN_LED1 on and off four times using a while loop. Parse the follow commands "reset" and "blink" in loop() utilizing the structure from the previous lab. Test the code by running the program and typing the blink and reset commands into the terminal. Analysis The reset function accomplishes the same task as holding the PRG button while pushing the RESET button on the Fubarino SD. It puts the board into the bootloader so you can load a new program, without physically pushing the buttons. Call over the instructor to check your work. Be prepared to explain why there is a countdown before calling the executesoftreset function // ITES LAB3-3 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Part 3 - More commands In this section add and test each command before progressing to the next. Add a command to the board called bye that prints out leaving so soon? when entered. Add a command called toggle that toggles the state of PIN_LED1 each time it is run. Command lightshow will make the LEDs flash a pattern, like that we did in Lab 2, for a few seconds. Command help or? prints outs a list of serial commands the board recognizes. Call over the instructor to check your work. Be prepared to demonstrate all commands. Part 4 - Buffer Size Modification In this section, you test each section independently and try to understand how the changes are affecting the program operation. Buffer Size Decrease the size of the command[] to 5. Try sending some commands to the board. Does this affect program functionality, and if so how? Reset Behavior Comment out the delay in the reset command in the parser and issue the reset command and notice how the Serial device is not recognized properly after the reset. Why is this happening? Call the instructor over to discuss this and to get checked off. Reset command[] to 0x20 and put the delay back in reset and continue to the next section. Part 5 - Count Up / Count Down Add the command count up to the parser. The count up command should use a loop to print out the numbers 1 to 15 on each on their own line with a quarter second delay between each number. Also display the number on the connected LED s. Add the command count down to the parser. The count down command should use a loop to print out the numbers 15 down to 0 on a single line with a space between each number with a quarter second delay between each number. Also display the output on LED s. // ITES LAB3-4 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Call the instructor over when both commands are working. Part 6 parse s In this section add and test each command before progressing to the next. In this section we will add a command called parse s that will iterate through the command buffer and display the character in each position in the array. void parse(char* s) { uint8_t i = 0; while(i < strlen(s) ) { Serial.print("s["); Serial.print(i, DEC); Serial.print("]='"); Serial.print(s[i]); Serial.print("' (0x"); Serial.print((uint8_t)s[i], HEX); Serial.print(")"); Serial.println(""); i++; To use call this function from our parser we will need to utilize the strncpy function to search for a match in the first n character of the string. This is accomplished by adding the following to your command parser: else if (strncmp(command, "parse", 5) == 0) { parse(command); Analysis The parse() accepts a pointer to a string named s as its only parameter. We utilize the strlen function to look for length of the string and iterate through all indices in the array. We then print out the index, character and ASCII value for each index in the array. Call over the instructor to check your work. Be prepared to demonstrate all commands. // ITES LAB3-5 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Part 7 flip s Command flip s where s is a string, returns a flip cased string of all characters received after flip. So if the string flip hello is sent to the board the string HeLLo will be returned. NOTE: This should only print out HeLLo and not FLIP HeLLo Call over the instructor to check your work. Be prepared to demonstrate all commands. Part 8 - LED State Machine Use parser to control an array of LED patterns. Command forward shows the next pattern and previous shows the last pattern, if you are at the first position do nothing when the previous command is entered. When you are at the last position do nothing when the forward command is entered. Hint: You will need to create a variable to keep track of the position in the LED pattern sequence. Also you will need to test for array bounds so that you don t exceed the positions in the array. uint8_t pattern[] = { 0b000000001, 0b000000011, 0b000000111, 0b000001111, 0b000001110, 0b000001100, 0b000001000, 0b000000000 uint8_t pattern_position = 0; Call the instructor over when working. // ITES LAB3-6 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Part 9 - Pointers to uint8_t Create a new sketch called lab5-bonus and try this mini program. uint8_t pattern1 = 0b0110; uint8_t pattern2 = 0b1001; void setup() { nibbleinit(0, 1, 2, 3, OUTPUT); void loop() { nibbletopins(pattern1, 0, 1, 2, 3); swapchars(&pattern1, &pattern2); delay(200); void swapchars(uint8_t* a_ptr, uint8_t* b_ptr) { uint8_t temp; temp = *a_ptr; *a_ptr = *b_ptr; *b_ptr = temp; Spend some time to understanding each line of code. Call the instructor over to demonstrate a working program and explain how the swapchars function works. Part 10 - Pointers to us8 Modified Write a program that uses pointers to switch between 3 patterns. Call the instructor over to demonstrate when working. // ITES LAB3-7 Copyright 2013-16 ProLinear/PONTECH, Inc. //

Homework Extra Credit Receive one lab credit (1/10 of a lab) extra credit for each of the two items completed. LED State Machine - Advanced Change the program so that the when at the last position the forward command cycles back to the first value and when you are at the first position the previous command cycles back to the last value. Adventure Use a parser to write a little adventure game. You can print out the response through the terminal, or using buttons and LEDs on your board. Keep track of a 10x10 space using an array. You can navigate through the space using commands north, south, east and west. Use the LED s to indicate items present in the space, LED0 indicates treasure, LED1 indicates a weapon, LED2 indicates food, LED3 indicates monster. As you move thought the space use the LED s and serial terminal to indicate what is present in the room. // ITES LAB3-8 Copyright 2013-16 ProLinear/PONTECH, Inc. //