Skip to content

Commit

Permalink
merge conflict fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sokosam committed Jan 11, 2025
2 parents 40b053f + 2bbb959 commit 71166ad
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
31 changes: 31 additions & 0 deletions firmware/mcal/cli/periph/analog_input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// @author Samuel Shi
/// @date 2024-11-17

#pragma once

#include <iostream>
#include "shared/periph/analog_input.h"

namespace mcal::cli::periph{

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

void Start() override {
std::cout << "Reading ADC " << name_ << "..." << std::endl;
}

float Read() override {
Start();
float adc_val;
std::cout << " | Enter an unsigned 32-bit value: ";
std::cin >> adc_val;
std::cout << " | Obtained value " << (adc_val/4095.0f * 3.3f) << std::endl;
return adc_val/4095.0f * 3.3f;
}
private:
std::string name_;
};

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


#pragma once

#include <cstdint>

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

namespace mcal::stm32f767::periph {

class AnalogInput : public shared::periph::AnalogInput {
private:
ADC_HandleTypeDef* hadc_;
uint32_t adc_channel_;

public:
AnalogInput(ADC_HandleTypeDef* hadc, uint32_t adc_channel)
: hadc_(hadc), adc_channel_(adc_channel){};

void Start() override {
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 Read() override {
/// @todo should there be a standard output range?
/// if so, the conversion would require knowing the resolution and
/// alignment settings

/// @todo change this to be non-blocking, currently just for demo
Start();
HAL_ADC_PollForConversion(hadc_, 1000);
uint32_t adc_value = HAL_ADC_GetValue(hadc_);
HAL_ADC_Stop(hadc_);

return adc_value/4095.0f * 3.3f;
}
};

} // namespace mcal::stm32f767::periph
11 changes: 11 additions & 0 deletions firmware/projects/Demo/AnalogInput/bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "shared/periph/analog_input.h"

namespace bindings {

extern shared::periph::AnalogInput& analog_input;

extern void Init();

} // namespace bindings
3 changes: 1 addition & 2 deletions firmware/projects/Demo/AnalogInput/main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "bindings.hpp"


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

while (true) {
Expand Down
4 changes: 3 additions & 1 deletion firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "../../bindings.hpp"

#include <iostream>

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


namespace mcal {

cli::periph::AnalogInput analog_input{"analog input"};
Expand Down
17 changes: 17 additions & 0 deletions firmware/shared/periph/analog_input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// @author Samuel Shi
/// @date 2024-11-17
/// Modified from adc.h

#pragma once

#include "shared/util/peripheral.h"

namespace shared::periph {

class AnalogInput : public util::Peripheral {
public:
virtual void Start() = 0;
virtual float Read() = 0;
};

} // namespace shared::periph

0 comments on commit 71166ad

Please sign in to comment.