Solution Efficiency and Robustness - JawaharT/Best-Practices-On-Azure-Sphere GitHub Wiki
#include <stdio.h>
int end_integer_input(){
static int number;
// Use this for testing purposes
// printf("Enter a Number for Fibonacci Sequence: ");
// scanf("%d", number);
// For embedded systems, equivalent to keypad entry here
number = 10;
return (int)number;
}
void generate_sequence(int end_pos){
int fib_sequence[end_pos];
fib_sequence[0] = 0;
printf("%d ", 0);
if (end_pos >= 2){
fib_sequence[1] = 1;
printf("%d ", 1);
for(int index = 0; index < end_pos-2; index++){
int next_number = fib_sequence[index] + fib_sequence[index+1];
printf("%d ", next_number);
fib_sequence[index+2] = next_number;
}
}
}
int main(){
generate_sequence(end_integer_input());
return 0;
}
This is an overview of the issues that have been solved:
generate_sequence:
- end_pos argument and fib_sequence array declarations contain unnecessary pointers
- end_pos should be greater than or equal to 2 to start the addition process
- The first two elements inside fib_sequence is always constant at zero and one, so the loop should only count the size of fib_sequence-2
- Add the next_number of the sequence to the end of the loop, which is defined as two places from the current index number
This is the output displayed once the program begins with stdin as 3 and 10 respectively on a desktop console. However, on an embedded system it would be ideal to display for example on an LED scrolling display.
Output when given 3 as input:
0 1 1
Output when given 10 as input:
0 1 1 2 3 5 8 13 21 34
Of course, this is not a complete solution as data is completely validated, this will come in another section soon.
do {
// Read the calibration values
lsm6dso_gy_flag_data_ready_get(&dev_ctx, ®);
if (reg){
// Task 1: Read angular rate field data to use for calibration
memset(data_raw_angular_rate.u8bit, 0x00, 3 * sizeof(int16_t));
lsm6dso_angular_rate_raw_get(&dev_ctx, raw_angular_rate_calibration.u8bit);
}
// Read the angular data rate again and verify that after applying the calibration
lsm6dso_gy_flag_data_ready_get(&dev_ctx, ®);
if (reg){
// Read angular rate
memset(data_raw_angular_rate.u8bit, 0x00, 3 * sizeof(int16_t));
lsm6dso_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate.u8bit);
// Before we store the mdps values subtract the calibration data.
angular_rate_dps[0] = lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[0] - raw_angular_rate_calibration.i16bit[0]);
angular_rate_dps[1] = lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[1] - raw_angular_rate_calibration.i16bit[1]);
angular_rate_dps[2] = lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[2] - raw_angular_rate_calibration.i16bit[2]);
}
// Task 2: If the angular values after applying the offset are not 0.7 or less, then do perform again!
} while ((angular_rate_dps[0] >= 0.7f) && (angular_rate_dps[1] >= 0.7f) && (angular_rate_dps[2] >= 0.7f));
This is an overview of the issues that have been solved:
- The first task of reading the angular rate involves using memset, to set a section of memory with the size of three times int16 to store the raw data from the onboard gyroscope, the next line returns the sensor data in 16-bit word in two’s complement which is then stored in the previous memset for calibration purposes
- The second task involves completing the while case, making sure to correctly validate all three axes of rotation and that 0.7 is the minimum, if not then the angular rates are calculated again, in a later section you will understand the importance of data validation as purpose of this task
#include <stdio.h>
int addLocal(int x, int y, int z){
// Task 1: Locally store the addition of x and y inside z
if (x < 0 || x > 100 || y < 0 || y > 100){
return -1;
}
z = x + y;
printf("Local Z = %d\n", z);
return 0;
}
int addUsingPointer(int x, int y, int* z){
// Task 2: Use a pointer to add x and y and store it inside z
*z = x + y;
printf("Pointer Z = %d\n", *z);
return 0;
}
void main(){
int x = 5;
int y = 7;
int z = -2;
printf("x: %d\n", x);
printf("y: %d\n", y);
printf("z: %d\n\n", z);
printf("addLocal\n");
addLocal(x, y, z);
printf("z in main: %d\n\n", z);
printf("addUsingPointer\n");
AddUsingPointer(x, y, &z);
printf("z in main: %d\n", z);
}
This is an overview of the issues that have been solved:
- The first task involves completing addLocal function, the if statement is to ensure x and y are below 100 and above 0 but it will still produce a valid addition even if not present. However z itself is not updated in main, hence z remains as -2 even after addlocal is called
- The second task involves using z as a pointer to store the value of the addition. But after its been called, z continues to be 12 unlike the first task. However the same if statement as in task one could also be applied as long as the sum of x and y are not too beyond the limits of the int data type