Homework: PWM Controller (2026-03-16)#
Getting#
Update your fork (see Distributed git: Update Your Fork From Upstream)
Massage
googletestsubmodule. This has to be done only once.$ git submodule init $ git submodule update
Exercise (Test Driven Development - TDD)#
New exercise:
/exercises/pwm/_codebergaccountname_/DO NOT MODIFY anything but
pwm.{h,cpp}Test driven development
$ pwd /home/jfasch/My-Builds/FH-STECE2024-x86_64/exercises/pwm/_codebergaccountname_ $ ./pwm-suite._codebergaccountname_ Running main() from /home/jfasch/My-Projects/FH-STECE2024/googletest/googletest/src/gtest_main.cc [==========] Running 9 tests from 1 test suite. [----------] Global test environment set-up. [----------] 9 tests from pwm_test [ RUN ] pwm_test.init_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:52: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.init_ok (0 ms) [ RUN ] pwm_test.init_nok [ OK ] pwm_test.init_nok (0 ms) [ RUN ] pwm_test.set_period_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:67: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.set_period_ok (0 ms) [ RUN ] pwm_test.set_period__no_read_permissions_on_file /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:88: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.set_period__no_read_permissions_on_file (0 ms) [ RUN ] pwm_test.get_period_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:108: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.get_period_ok (0 ms) [ RUN ] pwm_test.set_duty_cycle_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:124: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.set_duty_cycle_ok (0 ms) [ RUN ] pwm_test.get_duty_cycle_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:137: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.get_duty_cycle_ok (0 ms) [ RUN ] pwm_test.activate_deactivate_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:153: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.activate_deactivate_ok (0 ms) [ RUN ] pwm_test.is_active_ok /home/jfasch/My-Projects/FH-STECE2024/exercises/pwm/_codebergaccountname_/test_pwm.cpp:172: Failure Value of: ok Actual: false Expected: true [ FAILED ] pwm_test.is_active_ok (0 ms) [----------] 9 tests from pwm_test (2 ms total) [----------] Global test environment tear-down [==========] 9 tests from 1 test suite ran. (2 ms total) [ PASSED ] 1 test. [ FAILED ] 8 tests, listed below: [ FAILED ] pwm_test.init_ok [ FAILED ] pwm_test.set_period_ok [ FAILED ] pwm_test.set_period__no_read_permissions_on_file [ FAILED ] pwm_test.get_period_ok [ FAILED ] pwm_test.set_duty_cycle_ok [ FAILED ] pwm_test.get_duty_cycle_ok [ FAILED ] pwm_test.activate_deactivate_ok [ FAILED ] pwm_test.is_active_ok 8 FAILED TESTS
Running single tests and not all at once (handy for occasional
stracedebugging sessions)$ strace ./pwm-suite.jfasch --gtest_filter=pwm_test.set_period__no_read_permissions_on_file
Additional Information: PWM On Linux#
See how a PWM controller is handled on Linux: Talk to Chip Using the sysfs Interface
Additional Information: C++#
bool#
boolis a dedicated type, don’t have to misfitint
std::string#
C++ strings (std::string) are convenient
Operators (see Convenient Operators: Addition, Comparison, And Such)
⟶ Concatentation, assignment (see Conversion To Numbers: std::sto*() and Conversion To String: std::to_string())
Drawback: system calls like open are defined in C ⟶ needs explicit conversion back and forth (see Compatibility With C Strings)
std::filesystem::path#
PWM in
sysfsuses paths heavily⟶
std::filesystem::pathConversion to C string:
path.c_str(), just likestd::string