-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/srm mains power control #123
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
base: develop
Are you sure you want to change the base?
Changes from all commits
ce8cf62
4cb3601
f454477
51d42c5
5fc68a6
6063ebe
61a59c4
70ab2da
f4ce23e
a8a2817
7b57b54
4c265fb
31bb422
e1e2383
95c2189
5bf67a0
12cd7e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,13 +10,18 @@ | |
| */ | ||
|
|
||
| #include "CDebugController.h" | ||
| #include "../etl/to_string.h" | ||
|
|
||
| CDebugController::CDebugController(etl::string<MAX_STRING_SIZE> name, | ||
| CDebugController::CDebugController(IHardwareMap *p_hardware_map, | ||
| etl::string<MAX_STRING_SIZE> name, | ||
| uint32_t run_period_ms) | ||
| : CController(name, run_period_ms), | ||
| m_breather_light(BREATHING_GPIO_Port, BREATHING_Pin) | ||
| mp_hw(p_hardware_map) | ||
| { | ||
| // TODO Auto-generated constructor stub | ||
| if (mp_hw == nullptr) | ||
| { | ||
| Error_Handler(); | ||
| } | ||
| } | ||
|
|
||
| CDebugController::~CDebugController() | ||
|
|
@@ -26,14 +31,114 @@ CDebugController::~CDebugController() | |
|
|
||
| void CDebugController::run() | ||
| { | ||
| m_breather_light.toggle(); | ||
| static float breathing_light_intensity = 0; | ||
| mp_hw->setBreathingLight(breathing_light_intensity); | ||
| breathing_light_intensity = breathing_light_intensity ? 0 : 100; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understood correctly, this function sets the duty cycle to 0 or 100 to turn the LED on/off every time
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. This is really just a plug for when we get around to beautifying it. Ideally, we would want CDebugController to be able to switch this light from slow flash for "Everything is OK" to other types of flashing for when something is the matter and might require attention. But this is frills and cannot distract us right now. |
||
| } | ||
|
|
||
| bool CDebugController::newCommand(ICommand *p_command, | ||
| IComChannel *p_comchannel) | ||
| { | ||
| return CController::newCommand(p_command, | ||
| p_comchannel); // todo: temporary plug | ||
| bool b_command_recognised = false; | ||
| ICommand::command_error_code_t error_code; | ||
| /** | ||
| * Set mains power output. | ||
| * | ||
| * mains(channel, power); | ||
| */ | ||
| if (p_command->getName()->compare("mains") == 0) | ||
| { | ||
| error_code = setMainsPower(p_command); | ||
| b_command_recognised = true; | ||
| } | ||
| else if (p_command->getName()->compare("?mains") == 0) | ||
| { | ||
| float mains_power; | ||
| error_code = getMainsPower(p_command, &mains_power); | ||
| if (error_code == ICommand::COMMAND_OK) | ||
| { | ||
| CController::s_scratch_pad = "Mains channel "; | ||
| etl::to_string((*p_command)[0], | ||
| CController::s_scratch_pad, | ||
| etl::format_spec(), | ||
| true); | ||
| CController::s_scratch_pad += ": "; | ||
| etl::to_string(mains_power, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great to have an overloaded "addToString()" function that adds floats, ints, and string to the buffer before calling send(). I might have a look this weekend.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'd be awesome. Like c# and java do.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What might be a good thing to do is create a child class based on |
||
| CController::s_scratch_pad, | ||
| etl::format_spec().precision(2), | ||
| true); | ||
| p_comchannel->send(CController::s_scratch_pad); | ||
| } | ||
| b_command_recognised = true; | ||
| } | ||
| if (b_command_recognised) | ||
| { | ||
| sendResultMessage(error_code, p_comchannel); | ||
| } | ||
| return (b_command_recognised); | ||
| } | ||
|
|
||
| void CDebugController::reset() {} | ||
|
|
||
| ICommand::command_error_code_t CDebugController::setMainsPower( | ||
| ICommand *p_command) | ||
| { | ||
| uint8_t mains_channel; | ||
| float mains_power; | ||
| // extract arguments from command. | ||
| if (p_command->getArgumentCount() == 1) | ||
| { | ||
| mains_channel = 0; | ||
| mains_power = (*p_command)[0]; | ||
| } | ||
| else if (p_command->getArgumentCount() == 2) | ||
| { | ||
| mains_channel = (uint8_t)(*p_command)[0]; | ||
| if (mains_channel != (*p_command)[0]) | ||
| { | ||
| return (ICommand::ERROR_TYPE_MISMATCH); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this ever be true? How can
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit of an awkward way of seeing if an argument is a
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One option I can think of is to simply cast whatever is in the first argument into unsigned int and then range check. If it was sent as float, then we ignore the decimal part and use it as if it was an integer. This will not cause failures, but it'd be possible to request channel 2.5 to switch to 100% power, which will switch channel 2 or will bounce back with error depending on how casting works. |
||
| } | ||
| mains_power = (*p_command)[1]; | ||
| } | ||
| else | ||
| { | ||
| return (ICommand::ERROR_ARG_COUNT); | ||
| } | ||
| // range check arguments. | ||
| if ((mains_channel > 2) || (mains_power < 0) || (mains_power > 100)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to fix this because if
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's because I forgot final |
||
| { | ||
| return (ICommand::ERROR_OUT_OF_BOUNDS); | ||
| } | ||
| // set channels to requested power | ||
| if (mains_channel) | ||
| { | ||
| mp_hw->setMainsPwm(mains_channel, mains_power); | ||
| } | ||
| else | ||
| { | ||
| mp_hw->setMainsPwm(1, mains_power); | ||
| mp_hw->setMainsPwm(2, mains_power); | ||
| } | ||
| return (ICommand::COMMAND_OK); | ||
| } | ||
|
|
||
| ICommand::command_error_code_t CDebugController::getMainsPower( | ||
| ICommand *p_command, | ||
| float *power) | ||
| { | ||
| *power = 0; | ||
| if (p_command->getArgumentCount() != 1) | ||
| { | ||
| return (ICommand::ERROR_ARG_COUNT); | ||
| } | ||
| else if (((*p_command)[0] != 1) && ((*p_command)[0] != 2)) | ||
| { | ||
| return (ICommand::ERROR_OUT_OF_BOUNDS); | ||
| } | ||
| else | ||
| { | ||
| uint8_t channel = (*p_command)[0]; | ||
| *power = m_mains_power[channel]; | ||
| } | ||
| return (ICommand::COMMAND_OK); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should check that this is not a
nullptrsomewhere.