TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1#include "utils.h"
2#include "adc.h"
3#include "io.h"
4#include "uart.h"
5#include <stdlib.h>
6#include <util/delay.h>
7
9
10 volatile char *sram_adr = (char *)0x1800;
11 volatile char *adc_adr = (char *)0x1500;
12
13 (void)adc_adr[5];
14 _delay_ms(5000);
15 (void)sram_adr[5];
16 _delay_ms(5000);
17
18 return 0;
19}
20
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}
41
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}
62
63int16_t map(uint8_t x, uint8_t in_min, uint8_t in_max, int16_t out_min,
64 int16_t out_max) {
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}
ADC memory-mapped interface.
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.
uint8_t utils_joystick_print_pos(struct io_joystick_position *pos)
Prints joystick position (x/y) via UART.
Definition utils.c:21
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/func...
Definition utils.c:63
uint8_t utils_joystick_print_raw(struct io_joystick_device *dev)
Prints raw ADC joystick readings via UART.
Definition utils.c:42
Driver for joystick, buttons, and OLED.
struct io_joystick_position pos
Definition main.c:58
Container for ADC measurement results.
Definition adc.h:20
uint8_t channel[4]
Definition adc.h:21
Represents a joystick input device.
Definition io.h:55
unsigned int adc_channel_x
Definition io.h:56
unsigned int adc_channel_y
Definition io.h:57
Represents the position of a joystick relative to center.
Definition io.h:70
UART communication driver.
int utils_test_address_decode(void)
Definition utils.c:8
Utility and helper functions for joystick, LED debugging, and mapping.