Solution Performance - JawaharT/Best-Practices-On-Azure-Sphere GitHub Wiki
These are simple exercises through the use of built-in or Azure Sphere driver functions such as isalnum and CreateTimerFdAndAddToEpoll respectively. To show you can take advantage of the vast libraries, available to prevent reinventing the wheel and creating unnecessary complexity to your applications for Azure Sphere (which will benefit performance). The purpose of this first exercise is to show how you should validate text before displaying on screen to make sure it can be viewed as intended. However, the second exercise is designed for polling buttons on the AVNET Starter Kit Board.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
// Equivalent input from an embedded system, max 8 chars
char display_text[8] = "chj2";
int current_text_index = 0;
while(current_text_index < strlen(display_text)){
char current_char = display_text[current_text_index ];
if(!isalnum(current_char)){
printf("This text is not alphanumeric and cannot be displayed on screen.");
return 0;
}
current_text_index++;
}
printf("This text is alphanumeric and can be displayed.");
return 0;
}
Output for input (chj2):
This text is alphanumeric and can be displayed.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
// Equivalent input from an embedded system, max 8 chars
char display_text[8] = "ch%j2";
int current_text_index = 0;
while(current_text_index < strlen(display_text)){
char current_char = display_text[current_text_index ];
if(!isalnum(current_char)){
printf("This text is not alphanumeric and cannot be displayed on screen.");
return 0;
}
current_text_index++;
}
printf("This text is alphanumeric and can be displayed.");
return 0;
}
Output for input (ch%j2):
This text is not alphanumeric and cannot be displayed on screen.
In short, a character is alphanumeric if it is either a letter between A-Z (lower case included) or a positive real integer. This program takes the input string with eight maximum characters and uses a loop to check if each individual character within the string is alphanumeric. As long as each character within the string is alphanumeric, the final output to the console is "This text is alphanumeric and can be displayed." otherwise "This text is not alphanumeric and cannot be displayed on screen." is displayed.
int epollFd = -1;
static int buttonPollTimerFd = -1;
static int buttonAGpioFd = -1;
static int buttonBGpioFd = -1;
// Will return 0 on success or -1 on failure
static int InitPeripheralsAndHandlers(void){
// Task 1: Create Instance of CreateEpollFd
epollFd = CreateEpollFd();
if (epollFd < 0) {
return -1;
}
// Task 2: Open button A GPIO as input
Log_Debug("Opening Button A as input.\n");
buttonAGpioFd = GPIO_OpenAsInput(AVNET_MT3620_SK_USER_BUTTON_A);
if (buttonAGpioFd < 0) {
Log_Debug("ERROR: Could not open button A GPIO.\n");
return -1;
}
// Task 3: Set up a timer to poll the buttons (in seconds)
Log_Debug("Poll opened for buttons. Press A for Sensor Readings.\n");
struct timespec buttonPressCheckPeriod = {0, 1};
buttonPollTimerFd = CreateTimerFdAndAddToEpoll(epollFd, &buttonPressCheckPeriod, &buttonEventData, EPOLLIN);
if (buttonPollTimerFd < 0) {
return -1;
}
return 0;
}
This is an overview of the issues that have been solved:
-
Task one and two involves creating an instance of Epoll and the output is checked if the poll creating us successful before use
-
Task two involves opening input to button A from the board similarly to task one check if the connection was successful before use
-
Task three involves creation of the timer to poll for button A. The buttonPressCheckPeriod defines how long the interval is in the poll instance. The buttonPollTimerFd is an instance of the timer created using buttonPressCheckPeriod and the ButtonTimerEventHandler which will be created in a later exercise. The timer instance is then similarly checked if it was created successfully before use.
Both exercises use the process of validation:
- Exercise 1: Use of isalnum in an if statement
- Exercise 2: Check if the instance of EpollFd returns –1 (I.e, failed to create an instance, otherwise cannot continue application)
This is an important aspect which is best to keep in mind and will be discussed in more detail in an upcoming section.