sync
This commit is contained in:
9
C/C1/.vscode/settings.json
vendored
Normal file
9
C/C1/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"bike_measure.h": "c",
|
||||
"stdint.h": "c",
|
||||
"bike_store.h": "c",
|
||||
"stdlib.h": "c",
|
||||
"unistd.h": "c"
|
||||
}
|
||||
}
|
||||
21
C/C1/Makefile
Normal file
21
C/C1/Makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
NAME=main
|
||||
|
||||
FILES = $(wildcard *.c)
|
||||
|
||||
CC=gcc
|
||||
|
||||
SYMBOLS=-g -O0 -std=c99 -Wall -Werror -Wextra -pedantic -Wno-unused-parameter
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
all: product
|
||||
|
||||
product: Makefile $(FILES)
|
||||
$(CC) $(INC_DIRS) $(SYMBOLS) $(FILES) -o $(NAME)
|
||||
|
||||
run: product
|
||||
./$(NAME)
|
||||
|
||||
clean:
|
||||
rm -f $(NAME)
|
||||
|
||||
69
C/C1/bike_math.c
Normal file
69
C/C1/bike_math.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "bike_math.h"
|
||||
#include "bike_store.h"
|
||||
|
||||
uint16_t bikeMathGetValueForDataType(bikeStoreMeasurement measurement, bikeDataType data_type) {
|
||||
uint16_t value = 0;
|
||||
|
||||
if (data_type == BIKECADENCE) {
|
||||
value = measurement.cadence;
|
||||
} else if (data_type == BIKESPEED) {
|
||||
value = measurement.speed;
|
||||
} else if (data_type == BIKEHEARTRATE) {
|
||||
value = measurement.heartRate;
|
||||
} else if (data_type == BIKEPOWER) {
|
||||
value = measurement.power;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint16_t bikeMathCalculateMinValue(bikeDataType data_type) {
|
||||
uint16_t number_of_measurements = bikeStoreGetNumberOfMeasurementsPresent();
|
||||
|
||||
uint16_t min_value = UINT16_MAX;
|
||||
|
||||
for (uint16_t index_position = 0; index_position < number_of_measurements; index_position++) {
|
||||
bikeStoreMeasurement measurement = bikeStoreGetMeasurement(index_position);
|
||||
uint16_t value = bikeMathGetValueForDataType(measurement, data_type);
|
||||
if (value < min_value) {
|
||||
min_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
return min_value;
|
||||
}
|
||||
|
||||
uint16_t bikeMathCalculateMaxValue(bikeDataType data_type) {
|
||||
uint16_t number_of_measurements = bikeStoreGetNumberOfMeasurementsPresent();
|
||||
|
||||
uint16_t max_value = 0;
|
||||
|
||||
for (uint16_t index_position = 0; index_position < number_of_measurements; index_position++) {
|
||||
bikeStoreMeasurement measurement = bikeStoreGetMeasurement(index_position);
|
||||
uint16_t value = bikeMathGetValueForDataType(measurement, data_type);
|
||||
if (value > max_value) {
|
||||
max_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
return max_value;
|
||||
}
|
||||
|
||||
uint16_t bikeMathCalculateAverageValue(bikeDataType data_type) {
|
||||
uint16_t number_of_measurements = bikeStoreGetNumberOfMeasurementsPresent();
|
||||
|
||||
if (number_of_measurements == 0) return 0;
|
||||
|
||||
uint16_t average = 0;
|
||||
uint32_t sum = 0;
|
||||
|
||||
for (uint16_t index_position = 0; index_position < number_of_measurements; index_position++) {
|
||||
bikeStoreMeasurement measurement = bikeStoreGetMeasurement(index_position);
|
||||
uint16_t value = bikeMathGetValueForDataType(measurement, data_type);
|
||||
sum += value;
|
||||
}
|
||||
|
||||
average = sum / number_of_measurements;
|
||||
|
||||
return average;
|
||||
}
|
||||
19
C/C1/bike_math.h
Normal file
19
C/C1/bike_math.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef BIKE_MATH_H
|
||||
#define BIKE_MATH_H
|
||||
|
||||
#include <stdint.h>
|
||||
//#include "bike_store.h"
|
||||
|
||||
typedef enum {
|
||||
BIKESPEED,
|
||||
BIKEHEARTRATE,
|
||||
BIKECADENCE,
|
||||
BIKEPOWER
|
||||
} bikeDataType;
|
||||
|
||||
uint16_t bikeMathCalculateMinValue(bikeDataType dataType);
|
||||
uint16_t bikeMathCalculateMaxValue(bikeDataType dataType);
|
||||
uint16_t bikeMathCalculateAverageValue(bikeDataType dataType);
|
||||
//uint16_t bikeMathGetValueForDataType(bikeStoreMeasurement measurement, bikeDataType dataType);
|
||||
|
||||
#endif
|
||||
47
C/C1/bike_measure.c
Normal file
47
C/C1/bike_measure.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "bike_measure.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MIN_SPEED (27)
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MAX_SPEED (30)
|
||||
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MIN_POWER (150)
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MAX_POWER (200)
|
||||
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MIN_HEARTRATE (130)
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MAX_HEARTRATE (140)
|
||||
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MIN_CADENCE (88)
|
||||
#define BIKE_COMPUTER_SIMULATOR_VALUE_MAX_CADENCE (98)
|
||||
|
||||
static uint16_t bikeComputerSimulatorGetRandomValue(uint16_t minRange, uint16_t maxRange) {
|
||||
uint16_t range = (maxRange - minRange);
|
||||
|
||||
uint16_t randomValue = minRange + (rand() % range);
|
||||
|
||||
return randomValue;
|
||||
}
|
||||
|
||||
uint16_t bikeMeasureSpeedInKmh() {
|
||||
return bikeComputerSimulatorGetRandomValue(
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MIN_SPEED,
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MAX_SPEED);
|
||||
}
|
||||
|
||||
uint16_t bikeMeasurePowerInWatt() {
|
||||
return bikeComputerSimulatorGetRandomValue(
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MIN_POWER,
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MAX_POWER);
|
||||
}
|
||||
|
||||
uint16_t bikeMeasureCadenceInRpm() {
|
||||
return bikeComputerSimulatorGetRandomValue(
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MIN_CADENCE,
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MAX_CADENCE);
|
||||
}
|
||||
|
||||
uint16_t bikeMeasureHeartRateInBpm() {
|
||||
return bikeComputerSimulatorGetRandomValue(
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MIN_HEARTRATE,
|
||||
BIKE_COMPUTER_SIMULATOR_VALUE_MAX_HEARTRATE);
|
||||
}
|
||||
11
C/C1/bike_measure.h
Normal file
11
C/C1/bike_measure.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef BIKE_MEASURE_H
|
||||
#define BIKE_MEASURE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint16_t bikeMeasureSpeedInKmh();
|
||||
uint16_t bikeMeasurePowerInWatt();
|
||||
uint16_t bikeMeasureCadenceInRpm();
|
||||
uint16_t bikeMeasureHeartRateInBpm();
|
||||
|
||||
#endif
|
||||
31
C/C1/bike_store.c
Normal file
31
C/C1/bike_store.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "bike_store.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define BIKE_STORE_MAX_NUMBER_MEASUREMENTS (32)
|
||||
|
||||
static uint16_t bikeStoreGetMaximumBikeStoreSize() {
|
||||
return BIKE_STORE_MAX_NUMBER_MEASUREMENTS;
|
||||
}
|
||||
|
||||
static bikeStoreMeasurement bikeStoreArray[BIKE_STORE_MAX_NUMBER_MEASUREMENTS] = {{ 0, },};
|
||||
|
||||
static uint16_t bikeStoreNumberOfMeasurementsPresent = 0;
|
||||
|
||||
void bikeStoreAddMeasurement(bikeStoreMeasurement value)
|
||||
{
|
||||
if (bikeStoreNumberOfMeasurementsPresent >= bikeStoreGetMaximumBikeStoreSize()) {
|
||||
bikeStoreNumberOfMeasurementsPresent = 0;
|
||||
}
|
||||
bikeStoreArray[bikeStoreNumberOfMeasurementsPresent] = value;
|
||||
bikeStoreNumberOfMeasurementsPresent++;
|
||||
}
|
||||
|
||||
uint16_t bikeStoreGetNumberOfMeasurementsPresent() {
|
||||
return bikeStoreNumberOfMeasurementsPresent;
|
||||
}
|
||||
|
||||
bikeStoreMeasurement bikeStoreGetMeasurement(uint16_t indexPosition) {
|
||||
bikeStoreMeasurement value = bikeStoreArray[indexPosition];
|
||||
|
||||
return value;
|
||||
}
|
||||
18
C/C1/bike_store.h
Normal file
18
C/C1/bike_store.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef BIKE_STORE_H
|
||||
#define BIKE_STORE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint16_t speed;
|
||||
uint16_t heartRate;
|
||||
uint16_t cadence;
|
||||
uint16_t power;
|
||||
} bikeStoreMeasurement;
|
||||
|
||||
void bikeStoreAddMeasurement(bikeStoreMeasurement value);
|
||||
uint16_t bikeStoreGetNumberOfMeasurementsPresent();
|
||||
bikeStoreMeasurement bikeStoreGetMeasurement(uint16_t indexPosition);
|
||||
//uint16_t bikeStoreGetMaximumBikeStoreSize();
|
||||
|
||||
#endif
|
||||
51
C/C1/main.c
Normal file
51
C/C1/main.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bike_store.h"
|
||||
#include "bike_math.h"
|
||||
#include "bike_measure.h"
|
||||
|
||||
#define True (1)
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
bikeStoreMeasurement measurement;
|
||||
uint16_t min = 0, max = 0, average = 0;
|
||||
bikeDataType dataType;
|
||||
|
||||
while (True) {
|
||||
measurement.speed = bikeMeasureSpeedInKmh();
|
||||
measurement.cadence = bikeMeasureCadenceInRpm();
|
||||
measurement.heartRate = bikeMeasureHeartRateInBpm();
|
||||
measurement.power = bikeMeasurePowerInWatt();
|
||||
|
||||
bikeStoreAddMeasurement(measurement);
|
||||
|
||||
dataType = BIKESPEED;
|
||||
min = bikeMathCalculateMinValue(dataType);
|
||||
max = bikeMathCalculateMaxValue(dataType);
|
||||
average = bikeMathCalculateAverageValue(dataType);
|
||||
printf("SPEED:\t%d, average = %d, min = %d, max = %d [km/h]\n", measurement.speed, average, min, max);
|
||||
|
||||
dataType = BIKECADENCE;
|
||||
min = bikeMathCalculateMinValue(dataType);
|
||||
max = bikeMathCalculateMaxValue(dataType);
|
||||
average = bikeMathCalculateAverageValue(dataType);
|
||||
printf("CADENCE:\t%d, average = %d, min = %d, max = %d [rpm]\n", measurement.cadence, average, min, max);
|
||||
|
||||
dataType = BIKEHEARTRATE;
|
||||
min = bikeMathCalculateMinValue(dataType);
|
||||
max = bikeMathCalculateMaxValue(dataType);
|
||||
average = bikeMathCalculateAverageValue(dataType);
|
||||
printf("HEART-RATE:\t%d, average = %d, min = %d, max = %d [hrm]\n", measurement.heartRate, average, min, max);
|
||||
|
||||
dataType = BIKEPOWER;
|
||||
min = bikeMathCalculateMinValue(dataType);
|
||||
max = bikeMathCalculateMaxValue(dataType);
|
||||
average = bikeMathCalculateAverageValue(dataType);
|
||||
printf("POWER:\t%d, average = %d, min = %d, max = %d [watt]\n", measurement.power, average, min, max);
|
||||
printf("\n");
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user