






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These lab notes provide a brief survey of background information relevant to understanding the purpose and context for the lab. The field of computer engineering is at the interface between hardware and software and seeks to balance the tension between application requirements and technology constraints. In this lab, you will explore the field of computer engineering from a software perspective by incrementally programming a microcontroller in C++ to implement an IoT “smart light” system.
Typology: Lecture notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Prof. Christopher Batten School of Electrical and Computer Engineering Cornell University
The field of computer engineering is at the interface between hardware and software and seeks to balance the tension between application requirements and technology constraints. In Lab 1, you explored the field of computer engineering from a hardware perspective by assembling basic logic gates to implement a simple “calculator” for adding small binary numbers. In this lab, you will ex- plore the field of computer engineering from a software perspective by incrementally programming a microcontroller in C++ to implement an IoT “smart light” system. These lab notes provide a brief survey of background information relevant to understanding the purpose and context for the lab.
As illustrated in Figure 1, computer systems can be viewed as a stack of abstraction and implementa- tion layers from applications at the highest layer to technology at the lowest layer. In these lab notes we will briefly discuss the application, algorithm, programming language, operating system, com- piler, and instruction set layers as they relate to our “smart light” system. In the actual lab session, you will have an opportunity to put what you have learned into practice. We will focus on some layers more than others, but by the end of this lab you should have a good understanding of how computer engineers can leverage these layers to design software for future computing systems.
Figure 2 illustrates an example “smart light” system from a company called Brilliant. An internet- connected light switch enables turning on lights anywhere in the home, but this same light switch can
Computer Engineering
Smart Light Flowchart
C++
ARM Machine Instructions
CURIE Lab 2
Ripple Carry Adder
NOT, AND, OR, XOR
Resistors, LEDs, Transistors
CURIE Lab 1
Inverter
Particle OS Particle Development Environment
Figure 1: Computer Systems Stack
Figure 2: Example Commercial “Smart Light” System from Brilliant
IoT Output Device
IoT Input Device
IoT Cloud
LED Output Module
Button Input Module
Figure 3: Diagram of Simple “Smart Light” System
WiFi Atenna
Analog Port A
Digital Port D
Digital Port D
I2C Port 2 I2C Port 1
UART Port
Analog Port A
Analog Port A
USB Port
Blue LED on Pin D Power Connector for LiPo Battery
Reset Button
Status LED
Mode Button
Figure 4: Particle Argon
Setup IoT Device
Read Button State
Button Pressed?
Send "on" Msg Send "off" Msg
Wait 1 Second
(a) Flowchart for IoT Input Device
Setup IoT Device
Wait for Msg
Msg is "on"?
Turn LED On Turn LED Off
(b) Flowchart for IoT Output Device
yes
no no
yes
Figure 5: Flowchart for “Smart Light” Algorithm
(^1) int a; // declaration 2 a = 2; // assignment (^3) int b = 3; // initialization 4 (^5) int c; 6 c = a + b; (a)
1 // function to add two integers (^2) int add( int a, int b ) 3 { (^4) int sum; 5 sum = a + b; (^6) return sum; 7 } (b)
(^1) int c; 2 c = add( 2, 3 ); (c) Figure 6: Example C++ Code Snippets
of the message. If the message is “on”, then the algorithm turns the light on. If the message is “off”, then the algorithm turns the light off. Then the algorithm waits for another message.
Now that we have refined our application into an algorithm, we can implement this algorithm as a program in a specific programming language. There are many programming languages each with different advantages and disadvantages, but for this lab we will be using the C++ programming language. Figure 6 shows three example C++ code snippets. A program consists of a sequence of statements; these statements are executed one at a time by the computer to ultimately execute the program. Each statement is like a sentence in the English language; we read each sentence one at a time to understand a paragraph. In C++, a statement ends with a semicolon (;), while in the English language, a sentence ends with a period.
Figure 6(a) illustrates the most basic statements in the C++ programming language. The statement on line 1 is a variable declaration statement. We can think of a variable as a named “box” where we can store values. So in this example, we have a “box” named a. Each variable also has a type that indicates what kind of values can be stored in the box. In this case, the variable sum can only store values of type int which means the variable can store integer values (i.e., whole numbers). The statement on line 2 is a variable assignment statement. The value of the expression on the right-hand side of the equals operator (=) is stored in the variable on the left-hand side of the equals operator.
(^1) int button_state; 2 button_state = read_button_state( button_pin ); 3 4 if ( button_state == 1 ) { (^5) // send "on" msg 6 } (^7) else { 8 // send "off" msg (^9) } 10 (^11) // wait 1 second (a) Sketch of IoT Input Device Program
(^1) void receive_msg( msg ) 2 { (^3) if ( msg == "on" ) { 4 // turn light on (^5) } 6 else { (^7) // turn light off 8 } (^9) } (b) Sketch of IoT Output Device Program Figure 7: Sketch of C++ Programs for Smart Light
In this case, we store the value 2 into the variable a (i.e., we put the value 2 into the box named a). The statement on line 3 is a variable initialization statement which simply does a variable declaration and assignment in a single step. Once the computer has finished executing lines 1–3, the value 2 is stored in the variable a and the value 3 is stored in the variable b. Note that comments start with two forward slashes (i.e., //) and are ignored by the computer. You can feel free to exclude them although comments are important part of effective software engineering. Line 5 is another variable declaration statement. Line 6 is a variable assignment statement where the right-hand side is more complicated than just a single number. Here the computer must first determine the value stored in the variable a, the value stored in the variable b, add these two values together, and store the result (i.g., the value 5) in the variable c.
Figure 6(b) illustrates how to define a function. A function is a parameterized sequence of statements. Line 2 declares the function’s interface including the function’s name (add), the functions parameters (a,b), the type of each parameter (int), and the function’s return type (int). Lines 3–7 are called the function body. In this example, the function first adds the values stored in the two parameters and stores the result in a variable named sum. The sum is then returned using a return statement on Line 6. Figure 6(c) illustrates how to call a function. We use the function’s name and include a list of variables which will be passed in as the parameters to the function. So the example in Figure 6(c) will first declare a variable named c, then call the function add, which adds the values 2 and 3 before returning the result 5, which is finally stored in the variable c.
Figure 7 is a sketch of an initial C++ program for both the IoT input device and the IoT output device which corresponds to the algorithms shown in Figure 5. Lines 4–9 are a conditional statement. First, the conditional expression within the parenthesis on line 4 is evaluated (i.e., does the variable button_state contain the value 1?). If this conditional expression is true, then the statement on line 5 is executed. If this conditional expression is false, then the statement on line 8 is executed. A conditional statement can be used to implement decision steps in our flowchart. However, the C++ program in Figure 7 is not complete; it is just a sketch, since how our program reads the button state, sends messages, receives messages, and turns on/off LEDs depends on the operating system which is discussed in the next section.
If you would like to learn more about basic C++ programming consider reviewing this gentle intro- duction which is part of an intermediate programming course taught at Cornell University:
(^1) // Global constants for pin assignments and global variables 2 (^3) int led_pin = D7; 4 (^5) int x = 2; 6 int y = 3; (^7) int z = 0; 8 (^9) // Helper functions 10 (^11) int add( int a, int b ) 12 { (^13) int sum; 14 sum = a + b; (^15) return sum; 16 } 17 18 // The setup routine runs once when you press reset 19 20 void setup() (^21) { 22 // Configure led_pin as digital output (^23) pinMode( led_pin, OUTPUT ); 24 } 25 26 // The loop routine runs over and over again 27 28 void loop() (^29) { 30 // Do the addition (^31) z = add( x, y ); 32 (^33) // Blink LED z times 34 for ( int i = 0; i < z; i++ ) { (^35) digitalWrite( led_pin, HIGH ); // Turn on the LED 36 delay(500); // Wait 0.5 seconds (^37) digitalWrite( led_pin, LOW ); // Turn off the LED 38 delay(500); // Wait 0.5 seconds (^39) } 40 (^41) // Wait four seconds 42 delay(4000); (^43) } Figure 8: Complete Example C++ Program
1 int button_pin = D4; (^2) int button_state = -1; 3 (^4) void setup() 5 { (^6) pinMode( button_pin, INPUT ); 7 } 8 9 void loop() (^10) { 11 button_state = digitalRead( button_pin ); (^12) if ( button_state == 1 ) { 13 Particle.publish( "button_state", "on" ); (^14) } 15 else { (^16) Particle.publish( "button_state", "off" ); (^17) } (^18) delay(1000); (^19) }
(a) IoT Input Device Program
(^1) int led_pin = D4; 2 (^3) void receive_msg( const char* event, const char* msg ) 4 { (^5) if ( strcmp( msg, "on" ) == 0 ) { 6 digitalWrite( led_pin, HIGH ); (^7) } 8 else { (^9) digitalWrite( led_pin, LOW ); 10 } (^11) } 12 (^13) void setup() 14 { (^15) pinMode( led_pin, OUTPUT ); 16 Particle.subscribe( "button_state", receive_msg ); (^17) } 18 (^19) void loop() (^20) { (^21) // empty (^22) } (b) IoT Output Device Program Figure 9: Complete C++ Programs for Smart Light
Now that we have refined our algorithm into a programming language with operating system sup- port, we can use a compiler to translate the high-level program statements into the low-level instruc- tions that the machine can actually execute. In this lab, we will be using the online Particle develop- ment environment to compile our programs. You can start the Particle development environment by going to this URL:
The most important icon is the flash (lightning) icon in the upper-left hand corner. Clicking this icon will cause the Particle IDE to compile your program into machine instructions and then to upload the resulting machine instructions to the Particle Argon for execution. Figure 10 illustrates what happens
Figure 11: Particle Console Device Page
Figure 12: Particle Console Event Page