C ordening

This commit is contained in:
Rens Pastoor
2025-05-27 23:26:28 +02:00
parent 39269a71a7
commit 517087ccc1
207 changed files with 0 additions and 4278 deletions

View File

@@ -0,0 +1,9 @@
{
"files.associations": {
"bike_measure.h": "c",
"stdint.h": "c",
"bike_store.h": "c",
"stdlib.h": "c",
"unistd.h": "c"
}
}

View 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)

View 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;
}

View 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

View 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);
}

View 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

View 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;
}

View 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

BIN
C/C1 bike computer/main Normal file

Binary file not shown.

51
C/C1 bike computer/main.c Normal file
View 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);
}
}