Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 convert adc input to analoginput #348

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
35 changes: 0 additions & 35 deletions firmware/mcal/cli/periph/adc.hpp

This file was deleted.

31 changes: 31 additions & 0 deletions firmware/mcal/cli/periph/analog_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// @author Samuel Shi
/// @date 2024-11-17

#pragma once

#include <format>
#include <iostream>

#include "shared/periph/analog_input.hpp"

namespace mcal::cli::periph {

class AnalogInput : public shared::periph::AnalogInput {
public:
AnalogInput(std::string name) : name_(name) {}

float ReadVoltage() override {
std::cout << std::format("Reading ADC {} ...", name_) << std::endl;
std::cout << " | Enter a voltage level: ";
float voltage;
std::cin >> voltage;
std::cout << std::format(" | Obtained value {:.3f} V", voltage)
<< std::endl;
return voltage;
}

private:
std::string name_;
};

} // namespace mcal::cli::periph
2 changes: 1 addition & 1 deletion firmware/mcal/linux/periph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
target_sources(mcal-linux
PRIVATE
adc.cc
analog_input.cc
can.cc
digital_input.cc
digital_output.cc
Expand Down
26 changes: 0 additions & 26 deletions firmware/mcal/linux/periph/adc.cc

This file was deleted.

21 changes: 0 additions & 21 deletions firmware/mcal/linux/periph/adc.hpp

This file was deleted.

24 changes: 24 additions & 0 deletions firmware/mcal/linux/periph/analog_input.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// @author Samuel Shi
/// @date 2025-1-10

#include <format>
#include <iostream>

#include "analog_input.hpp"


namespace mcal::lnx::periph {

AnalogInput::AnalogInput(std::string name) : name_(name) {}

float AnalogInput::ReadVoltage() {
std::cout << std::format("Reading ADC {} ...", name_) << std::endl;
std::cout << " | Enter a voltage level: ";
float voltage;
std::cin >> voltage;
std::cout << std::format(" | Obtained value {:.3f} V", voltage)
<< std::endl;
return voltage;
}

}; // namespace mcal::lnx::periph
22 changes: 22 additions & 0 deletions firmware/mcal/linux/periph/analog_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// @author Samuel Shi
/// @date 2025-1-10

#pragma once

#include <iostream>

#include "shared/periph/analog_input.hpp"

namespace mcal::lnx::periph {

class AnalogInput : public shared::periph::AnalogInput {
public:
AnalogInput(std::string name);

float ReadVoltage() override;

private:
std::string name_;
};

} // namespace mcal::lnx::periph
48 changes: 0 additions & 48 deletions firmware/mcal/stm32f767/periph/adc.hpp

This file was deleted.

62 changes: 62 additions & 0 deletions firmware/mcal/stm32f767/periph/analog_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@


#pragma once

#include <cstdint>

#include "shared/periph/analog_input.hpp"
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_adc.h"

namespace mcal::stm32f767::periph {

class AnalogInput : public shared::periph::AnalogInput {
public:
AnalogInput(ADC_HandleTypeDef* hadc, uint32_t adc_channel,
float system_volts = 3.3f)
: hadc_(hadc),
adc_channel_(adc_channel),
system_volts_(system_volts) {};

float ReadVoltage() override {
Start();
HAL_ADC_PollForConversion(hadc_, 1000);
uint32_t adc_value = HAL_ADC_GetValue(hadc_);
HAL_ADC_Stop(hadc_);

return adc_value / GetDivisor() * system_volts_;
}

private:
ADC_HandleTypeDef* hadc_;
uint32_t adc_channel_;
const float system_volts_;

void Start() {
ADC_ChannelConfTypeDef adc_config = {
.Channel = adc_channel_,
.Rank = ADC_REGULAR_RANK_1,
.SamplingTime = ADC_SAMPLETIME_28CYCLES,
.Offset = 0};

HAL_ADC_ConfigChannel(hadc_, &adc_config);
HAL_ADC_Start(hadc_);
}

float GetDivisor() {
switch (ADC_GET_RESOLUTION(hadc_)) {
case ADC_RESOLUTION_6B:
return 63.0f;
case ADC_RESOLUTION_8B:
return 255.0f;
case ADC_RESOLUTION_10B:
return 1023.0f;
case ADC_RESOLUTION_12B:
return 4095.0f;
default:
return 4095.0f;
}
};
};

} // namespace mcal::stm32f767::periph
1 change: 1 addition & 0 deletions firmware/projects/Demo/AnalogInput/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources(main PUBLIC main.cc)
16 changes: 16 additions & 0 deletions firmware/projects/Demo/AnalogInput/bindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "shared/periph/analog_input.hpp"
#include "shared/periph/gpio.hpp"

namespace bindings {

extern shared::periph::AnalogInput& analog_input;

extern shared::periph::DigitalOutput& red_led;
extern shared::periph::DigitalOutput& blue_led;
extern shared::periph::DigitalOutput& green_led;

extern void Init();

} // namespace bindings
26 changes: 26 additions & 0 deletions firmware/projects/Demo/AnalogInput/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "bindings.hpp"

int main() {
bindings::Init();

BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved
while (true) {
float volts = bindings::analog_input.ReadVoltage();
bool red = false, green = false, blue = false;

if (volts <= 1) {
red = true;
} else if (volts <= 2) {
blue = true;
} else if (volts <= 3) {
green = true;
} else {
red = true;
blue = true;
green = true;
}
bindings::red_led.Set(red);
bindings::blue_led.Set(blue);
bindings::green_led.Set(green);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target_sources(main PRIVATE bindings.cc)
target_link_libraries(main PUBLIC mcal-cli)
29 changes: 29 additions & 0 deletions firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>

#include "../../bindings.hpp"
#include "mcal/cli/periph/analog_input.hpp"
#include "mcal/cli/periph/gpio.hpp"
#include "shared/periph/analog_input.hpp"
#include "shared/periph/gpio.hpp"

namespace mcal {

cli::periph::AnalogInput analog_input{"analog input"};
cli::periph::DigitalOutput green_led{"green_led"};
cli::periph::DigitalOutput red_led{"red_led"};
cli::periph::DigitalOutput blue_led{"blue_led"};

} // namespace mcal

namespace bindings {

shared::periph::AnalogInput& analog_input = mcal::analog_input;
shared::periph::DigitalOutput& green_led = mcal::green_led;
shared::periph::DigitalOutput& red_led = mcal::red_led;
shared::periph::DigitalOutput& blue_led = mcal::blue_led;

void Init() {
std::cout << "Initializing the CLI..." << std::endl;
}

} // namespace bindings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(MCAL cli)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target_sources(main PRIVATE bindings.cc)
target_link_libraries(main PRIVATE mcal-linux)
Loading