Skip to content

Commit f7569d9

Browse files
committed
Added Lab4a project explaining the CMock API usage examples.
1 parent 925a2af commit f7569d9

13 files changed

Lines changed: 638 additions & 0 deletions

File tree

Lab4a/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CEEDLING = ceedling
2+
3+
.PHONY: test coverage clean generate_mocks
4+
5+
test:
6+
$(CEEDLING) test:all
7+
8+
coverage:
9+
$(CEEDLING) gcov:all
10+
11+
clean:
12+
rm -rf build
13+
14+
generate_mocks:
15+
$(CEEDLING) test:all

Lab4a/inc/adc_drv.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef ADC_DRV_H
2+
#define ADC_DRV_H
3+
4+
#include <stdint.h>
5+
6+
/**
7+
* @brief Read battery voltage.
8+
*
9+
* Reads the battery voltage from the ADC driver.
10+
*
11+
* @return Battery voltage in millivolts.
12+
*/
13+
uint16_t adc_read_battery_mv(void);
14+
15+
#endif /* ADC_DRV_H */

Lab4a/inc/battery.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef BATTERY_H
2+
#define BATTERY_H
3+
4+
#include <stdint.h>
5+
6+
/**
7+
* @brief Battery health status.
8+
*
9+
* Represents the current health condition of the battery
10+
* based on measured battery voltage.
11+
*/
12+
typedef enum
13+
{
14+
BATTERY_HEALTH_GOOD = 0, /**< Battery voltage is healthy */
15+
BATTERY_HEALTH_LOW, /**< Battery voltage is low */
16+
BATTERY_HEALTH_CRITICAL /**< Battery voltage is critical */
17+
} battery_health_t;
18+
19+
/**
20+
* @brief Get battery percentage.
21+
*
22+
* Reads the battery voltage and converts it to a
23+
* percentage value between 0 and 100.
24+
*
25+
* @return Battery percentage.
26+
*/
27+
uint8_t battery_get_percentage(void);
28+
29+
/**
30+
* @brief Determine battery health state.
31+
*
32+
* Evaluates the battery voltage and returns the
33+
* corresponding health status.
34+
*
35+
* @return Battery health status.
36+
*/
37+
battery_health_t battery_get_health(void);
38+
39+
/**
40+
* @brief Update battery warning LED state.
41+
*
42+
* Turns the warning LED ON when battery level is low
43+
* and OFF when battery level is healthy.
44+
*/
45+
void battery_update_led(void);
46+
47+
/**
48+
* @brief Send battery telemetry packet.
49+
*
50+
* Creates and transmits a telemetry packet containing
51+
* battery information over UART.
52+
*/
53+
void battery_send_telemetry(void);
54+
55+
/**
56+
* @brief Get battery temperature.
57+
*
58+
* Reads the battery temperature from the temperature
59+
* sensor driver.
60+
*
61+
* @return Battery temperature in degrees Celsius.
62+
*/
63+
int16_t battery_get_temperature(void);
64+
65+
/**
66+
* @brief Execute battery monitoring task.
67+
*
68+
* Periodically checks battery status and performs
69+
* required actions such as updating LEDs and sending
70+
* telemetry.
71+
*/
72+
void battery_monitor_task(void);
73+
74+
#endif /* BATTERY_H */

Lab4a/inc/led_drv.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef LED_DRV_H
2+
#define LED_DRV_H
3+
4+
/**
5+
* @brief Turn warning LED ON.
6+
*
7+
* Indicates low battery condition.
8+
*/
9+
void led_warning_on(void);
10+
11+
/**
12+
* @brief Turn warning LED OFF.
13+
*
14+
* Indicates battery condition is healthy.
15+
*/
16+
void led_warning_off(void);
17+
18+
#endif /* LED_DRV_H */

Lab4a/inc/sensor_drv.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef SENSOR_DRV_H
2+
#define SENSOR_DRV_H
3+
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
7+
/**
8+
* @brief Read temperature sensor.
9+
*
10+
* Reads the current temperature and stores the result
11+
* in the supplied output parameter.
12+
*
13+
* @param temperature Pointer to temperature value.
14+
*
15+
* @return true if reading succeeded.
16+
* @return false if reading failed.
17+
*/
18+
bool sensor_read_temperature(
19+
int16_t *temperature);
20+
21+
#endif /* SENSOR_DRV_H */

Lab4a/inc/uart_drv.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef UART_DRV_H
2+
#define UART_DRV_H
3+
4+
#include <stdint.h>
5+
6+
/**
7+
* @brief Send data over UART.
8+
*
9+
* Transmits a data buffer over UART.
10+
*
11+
* @param data Pointer to transmit buffer.
12+
* @param length Number of bytes to transmit.
13+
*/
14+
void uart_send(
15+
uint8_t *data,
16+
uint16_t length);
17+
18+
#endif /* UART_DRV_H */

Lab4a/project.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:project:
2+
:build_root: build
3+
:test_file_prefix: test_
4+
5+
:paths:
6+
:source:
7+
- src/**
8+
:include:
9+
- inc/**
10+
:test:
11+
- test/**
12+
13+
:plugins:
14+
:enabled:
15+
- module_generator
16+
- gcov
17+
18+
:gcov:
19+
:reports:
20+
- HtmlDetailed
21+
22+
:cmock:
23+
:mock_prefix: Mock
24+
:plugins:
25+
- callback
26+
- array
27+
- return_thru_ptr
28+
- ignore_arg

Lab4a/src/adc_drv.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @file adc_drv.c
3+
* @brief ADC driver implementation.
4+
*/
5+
6+
#include "adc_drv.h"
7+
8+
uint16_t adc_read_battery_mv(void)
9+
{
10+
return 3700U;
11+
}

Lab4a/src/battery.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "battery.h"
2+
3+
#include "adc_drv.h"
4+
#include "led_drv.h"
5+
#include "uart_drv.h"
6+
#include "sensor_drv.h"
7+
8+
#define BATTERY_MIN_MV (3200U)
9+
#define BATTERY_MAX_MV (4200U)
10+
11+
uint8_t battery_get_percentage(void)
12+
{
13+
uint16_t mv = adc_read_battery_mv();
14+
15+
if (mv <= BATTERY_MIN_MV)
16+
{
17+
return 0;
18+
}
19+
20+
if (mv >= BATTERY_MAX_MV)
21+
{
22+
return 100;
23+
}
24+
25+
return (uint8_t)(
26+
((uint32_t)(mv - BATTERY_MIN_MV) * 100U) /
27+
(BATTERY_MAX_MV - BATTERY_MIN_MV));
28+
}
29+
30+
battery_health_t battery_get_health(void)
31+
{
32+
uint16_t mv = adc_read_battery_mv();
33+
34+
if (mv <= 3300U)
35+
{
36+
return BATTERY_HEALTH_CRITICAL;
37+
}
38+
39+
if (mv <= 3600U)
40+
{
41+
return BATTERY_HEALTH_LOW;
42+
}
43+
44+
return BATTERY_HEALTH_GOOD;
45+
}
46+
47+
void battery_update_led(void)
48+
{
49+
if (battery_get_percentage() < 20U)
50+
{
51+
led_warning_on();
52+
}
53+
else
54+
{
55+
led_warning_off();
56+
}
57+
}
58+
59+
void battery_send_telemetry(void)
60+
{
61+
uint8_t packet[3];
62+
63+
packet[0] = 0xAA;
64+
packet[1] = battery_get_percentage();
65+
packet[2] = 0x55;
66+
67+
uart_send(
68+
packet,
69+
sizeof(packet));
70+
}
71+
72+
int16_t battery_get_temperature(void)
73+
{
74+
int16_t temperature = 0;
75+
76+
(void)sensor_read_temperature(
77+
&temperature);
78+
79+
return temperature;
80+
}
81+
82+
void battery_monitor_task(void)
83+
{
84+
if (battery_get_percentage() < 20U)
85+
{
86+
led_warning_on();
87+
}
88+
}

Lab4a/src/led_drv.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @file led_drv.c
3+
* @brief LED driver implementation.
4+
*/
5+
6+
#include "led_drv.h"
7+
8+
void led_warning_on(void)
9+
{
10+
}
11+
12+
void led_warning_off(void)
13+
{
14+
}

0 commit comments

Comments
 (0)