-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |