Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
ArduinoHAL.h
Go to the documentation of this file.
1#ifndef ARDUINOHAL_H
2#define ARDUINOHAL_H
3
4#ifdef ARDUINO
5#include "Arduino.h"
6
7#include <Adafruit_SPIFlash.h>
8#include <Adafruit_Sensor.h>
9#include <SdFat.h>
10#include <SdFatConfig.h>
11#include <SPI.h>
12
13#else // Everything below will only be compiled if we are not on an Arduino
14
15#include <chrono>
16#include <cstring>
17#include <iostream>
18#include <string>
19#include <thread>
20
22#include "serial_mock.h"
23#include "spi_mock.h"
24
25using String = std::string;
26
27#define OUTPUT 1
28#define INPUT 0
29#define HIGH 1
30#define LOW 0
31
32inline void pinMode(int pin, int mode) { //NOLINT
33 // Do nothing
34}
35
36inline void digitalWrite(int pin, int value) { //NOLINT
37 // Do nothing
38}
39
40inline void analogReadResolution(int bits) {
41 // Do nothing, we will just return a 12-bit value in analogRead
42}
43
44#define HAL_HIGH_VOLTAGE_ADC_PIN 192
45#define HAL_MID_VOLTAGE_ADC_PIN 193
46#define HAL_LOW_VOLTAGE_ADC_PIN 194
47
48inline uint32_t analogRead(int pin) {
49 // Return a dummy 12 bit value for the voltage pin, and 0 for other pins.
50 // This allows us to test the battery voltage reading functionality without needing a real ADC.
51 if (pin == HAL_HIGH_VOLTAGE_ADC_PIN) {
52 return static_cast<uint32_t>((1 << 12) - 1); // Return full-scale value for a 12-bit ADC (4095)
53 }
54 if (pin == HAL_MID_VOLTAGE_ADC_PIN) {
55 return static_cast<uint32_t>((1 << 12) - 1) / 2; // Return mid-scale value for a 12-bit ADC (2047)
56 }
57 if (pin == HAL_LOW_VOLTAGE_ADC_PIN) {
58 return static_cast<uint32_t>(1 << 12) / 10; // Return a very low value (4095 / 10 = 409)
59 }
60 return 0; // Default dummy value for other pins
61}
62
63// millis mock which still gives us the time since the program started in milliseconds
64static auto program_start = std::chrono::high_resolution_clock::now();
65inline unsigned long millis() {
66 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - program_start).count();
67}
68
69inline void delay(unsigned long ms) { // NOLINT
70 // Wait using the real time clock
71 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
72}
73
74
75#endif // ARDUINO
76#endif // ARDUINOHAL_H