TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
Utilities

Macros

#define UTILS_DEBUG_LED_PIN   PB0
#define TRASHCAN   ((void *)0)
#define STATUS_ASSERT(status)
 Macro to assert a status and flash a debug LED.

Functions

uint8_t utils_joystick_print_pos (struct io_joystick_position *pos)
 Prints joystick position (x/y) via UART.
uint8_t utils_joystick_print_raw (struct io_joystick_device *dev)
 Prints raw ADC joystick readings via UART.
int16_t map (uint8_t x, uint8_t in_min, uint8_t in_max, int16_t out_min, int16_t out_max)
 Maps a value from one range to another (taken from https://docs.arduino.cc/language-reference/en/functions/math/map/).

Detailed Description

Macro Definition Documentation

◆ STATUS_ASSERT

#define STATUS_ASSERT ( status)

#include <utils.h>

Value:
{ \
DDRB |= (1 << UTILS_DEBUG_LED_PIN); \
\
if (status) { \
PORTB |= (1 << UTILS_DEBUG_LED_PIN); \
} \
while (status > 0) { \
PORTB ^= (1 << UTILS_DEBUG_LED_PIN); \
_delay_ms(500); \
} \
}
#define UTILS_DEBUG_LED_PIN
Definition utils.h:17

Macro to assert a status and flash a debug LED.

Parameters
statusNegative sets LED; positive triggers LED flashing; zero does nothing

Definition at line 26 of file utils.h.

26#define STATUS_ASSERT(status) \
27 { \
28 DDRB |= (1 << UTILS_DEBUG_LED_PIN); \
29 \
30 if (status) { \
31 PORTB |= (1 << UTILS_DEBUG_LED_PIN); \
32 } \
33 while (status > 0) { \
34 PORTB ^= (1 << UTILS_DEBUG_LED_PIN); \
35 _delay_ms(500); \
36 } \
37 }

Referenced by main().

◆ TRASHCAN

#define TRASHCAN   ((void *)0)

◆ UTILS_DEBUG_LED_PIN

#define UTILS_DEBUG_LED_PIN   PB0

#include <utils.h>

Debug LED pin for error status

Definition at line 17 of file utils.h.

Function Documentation

◆ map()

int16_t map ( uint8_t x,
uint8_t in_min,
uint8_t in_max,
int16_t out_min,
int16_t out_max )

#include <utils.h>

Maps a value from one range to another (taken from https://docs.arduino.cc/language-reference/en/functions/math/map/).

Parameters
xInput value
in_minInput range minimum
in_maxInput range maximum
out_minOutput range minimum
out_maxOutput range maximum
Returns
Mapped value in output range

Definition at line 63 of file utils.c.

64 {
65 // do all math in 32-bit to avoid overflow
66 int32_t num = (int32_t)(x - in_min) * (out_max - out_min);
67 int32_t den = (int32_t)(in_max - in_min);
68 int32_t result = num / den + out_min;
69 return (int16_t)result;
70}

Referenced by io_joystick_read_position().

◆ utils_joystick_print_pos()

uint8_t utils_joystick_print_pos ( struct io_joystick_position * pos)

#include <utils.h>

Prints joystick position (x/y) via UART.

Parameters
posPointer to joystick position struct
Returns
0 on success

Definition at line 21 of file utils.c.

21 {
22 struct ADC_meas data;
23
24 ADC_read_all(&data);
25
26 char raw_x_str[5] = "";
27 char raw_y_str[5] = "";
28 itoa(pos->x, raw_x_str, 10);
29 itoa(pos->y, raw_y_str, 10);
30
31 USART_SendString("Joystick X: ");
32 USART_SendString(raw_x_str);
33 USART_SendString("\t");
34
35 USART_SendString("Joystick Y: ");
36 USART_SendString(raw_y_str);
37 USART_SendString("\n");
38
39 return 0;
40}
void ADC_read_all(struct ADC_meas *output)
Perform a full four-channel ADC conversion and return all results.
Definition adc.c:36
int USART_SendString(char data[])
Send a zero-terminated string over USART0.
struct io_joystick_position pos
Definition main.c:58
Container for ADC measurement results.
Definition adc.h:20

◆ utils_joystick_print_raw()

uint8_t utils_joystick_print_raw ( struct io_joystick_device * dev)

#include <utils.h>

Prints raw ADC joystick readings via UART.

Parameters
devPointer to joystick device
Returns
0 on success

Definition at line 42 of file utils.c.

42 {
43 struct ADC_meas data;
44
45 ADC_read_all(&data);
46
47 char raw_x_str[5] = "";
48 char raw_y_str[5] = "";
49 itoa(data.channel[dev->adc_channel_x], raw_x_str, 10);
50 itoa(data.channel[dev->adc_channel_y], raw_y_str, 10);
51
52 USART_SendString("Joystick X: ");
53 USART_SendString(raw_x_str);
54 USART_SendString("\t");
55
56 USART_SendString("Joystick Y: ");
57 USART_SendString(raw_y_str);
58 USART_SendString("\n");
59
60 return 0;
61}
unsigned int adc_channel_x
Definition io.h:56
unsigned int adc_channel_y
Definition io.h:57