Skip to content

Commit

Permalink
Merge pull request #1 from russkel/master
Browse files Browse the repository at this point in the history
fix: set exception bits on ofstreams
  • Loading branch information
russkel authored Oct 25, 2024
2 parents 92482ea + 4a38817 commit daf9684
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/pwmlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace std::chrono_literals;

void PWMPort::set_period(int32_t period) {
std::ofstream ofs(channel_path / "period");
ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
if (!ofs.is_open())
throw std::runtime_error(fmt::format("Cannot open {}. Likely insufficient permissions", (channel_path / "period").string()));
ofs << period;
Expand All @@ -35,6 +36,7 @@ int32_t PWMPort::get_period() {

void PWMPort::set_enabled(bool enable) {
std::ofstream ofs(channel_path / "enable");
ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
if (!ofs.is_open())
throw std::runtime_error(fmt::format("Cannot open {}. Likely insufficient permissions", (channel_path / "enable").string()));
ofs << enable;
Expand All @@ -43,6 +45,7 @@ void PWMPort::set_enabled(bool enable) {

void PWMPort::set_polarity() {
std::ofstream ofs(channel_path / "polarity");
ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
if (!ofs.is_open())
throw std::runtime_error(fmt::format("Cannot open {}. Likely insufficient permissions", (channel_path / "polarity").string()));
ofs << "normal";
Expand All @@ -61,6 +64,7 @@ std::string PWMPort::get_polarity() {

void PWMPort::set_duty_direct(int32_t duty) {
duty_fs = std::ofstream(duty_cycle_path);
duty_fs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
if (!duty_fs.is_open())
throw std::runtime_error("duty_cycle fstream not open, cannot set duty cycle");
duty_fs << duty;
Expand Down

0 comments on commit daf9684

Please sign in to comment.