-
Notifications
You must be signed in to change notification settings - Fork 160
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
Update Keyboard .cpp to include usable begin and end functions #95
Conversation
implemented Keyboard.begin() and Keyboard.end() with the simple use of a boolean.
Memory usage change @ 44ea612
Click for full report table
Click for full report CSV
|
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.
Thanks for this pull request! I have to admit I do not understand what is its purpose. What problem is this trying to solve? Do you have a use case?
My understanding is that begin()
and end()
were never intended to be “switches” to turn the library on and off. My assumption has always been that they are only meant to future-proof the API: if, at some point in the future, the library needs to do some extra initialization and clean-up, this can be done in begin()
and end()
, without needing to upgrade the library's API.
Given that this library aims at being lightweight, every extra feature should be worth its cost in flash space, even if it is a small cost. I am not yet convinced this feature is worth it.
src/Keyboard.cpp
Outdated
@@ -21,6 +21,7 @@ | |||
|
|||
#include "Keyboard.h" | |||
#include "KeyboardLayout.h" | |||
bool enable = false; |
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.
This should be static
, otherwise it could collide with a user-defined variable. Also, the past participle “enabled” would make more sense than the imperative “enable”: verbs at the imperative are usually used to name functions and methods, not booleans.
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.
done
src/Keyboard.cpp
Outdated
@@ -90,6 +93,7 @@ uint8_t USBPutChar(uint8_t c); | |||
// call release(), releaseAll(), or otherwise clear the report and resend. | |||
size_t Keyboard_::press(uint8_t k) | |||
{ | |||
if(enable == true){ |
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.
For style consistency, there should be a space after the if
, and another one before the opening brace.
src/Keyboard.cpp
Outdated
@@ -90,6 +93,7 @@ uint8_t USBPutChar(uint8_t c); | |||
// call release(), releaseAll(), or otherwise clear the report and resend. | |||
size_t Keyboard_::press(uint8_t k) | |||
{ | |||
if(enable == true){ | |||
uint8_t i; |
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.
Bad indentation. If you want to put everything within an if
block, the whole thing should be indented one level deeper. Although it would be far better to avoid this extra nesting and instead return early, as in:
if (!enable) {
return 0;
}
@@ -133,13 +137,15 @@ size_t Keyboard_::press(uint8_t k) | |||
} | |||
sendReport(&_keyReport); | |||
return 1; | |||
} | |||
} |
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.
GCC tells me:
In member function
size_t Keyboard_::press(uint8_t)
:
warning: control reaches end of non-void function [-Wreturn-type]
Same warning on release()
and the two overload of write()
.
src/Keyboard.cpp
Outdated
} | ||
|
||
size_t Keyboard_::write(uint8_t c) | ||
{ | ||
if(enable == true){ |
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.
Given that write()
only calls press()
and release()
, and these methods are already doing the test, there is no point in doing it also here.
src/Keyboard.cpp
Outdated
} | ||
|
||
size_t Keyboard_::write(const uint8_t *buffer, size_t size) { | ||
if (enable == true){ |
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.
Same as before: redundant test.
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.
Oh ok I thought that the begin and end functions were for enabling/disabling the library's functions.But if that's not the case then i believe they should be named shomething like Keyboard.init().
implemented Keyboard.begin() and Keyboard.end() with the simple use of a boolean. Sincirely, Dragonerboom