Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

dragonerboom
Copy link

implemented Keyboard.begin() and Keyboard.end() with the simple use of a boolean. Sincirely, Dragonerboom

implemented Keyboard.begin() and Keyboard.end() with the simple use of a boolean.
@CLAassistant
Copy link

CLAassistant commented Jun 25, 2024

CLA assistant check
All committers have signed the CLA.

@dragonerboom dragonerboom changed the title Update Keyboard.cpp Update Keyboard .cpp to include usable begin and end functions Jun 25, 2024
Copy link

Memory usage change @ 44ea612

Board flash % RAM for global variables %
arduino:avr:leonardo 🔺 +12 - +12 +0.04 - +0.04 🔺 +1 - +1 +0.04 - +0.04
arduino:sam:arduino_due_x_dbg 🔺 +80 - +80 +0.02 - +0.02 N/A N/A
arduino:samd:mkrzero 🔺 +64 - +64 +0.02 - +0.02 🔺 +4 - +4 +0.01 - +0.01
Click for full report table
Board examples/Serial
flash
% examples/Serial
RAM for global variables
%
arduino:avr:leonardo 12 0.04 1 0.04
arduino:sam:arduino_due_x_dbg 80 0.02 N/A N/A
arduino:samd:mkrzero 64 0.02 4 0.01
Click for full report CSV
Board,examples/Serial<br>flash,%,examples/Serial<br>RAM for global variables,%
arduino:avr:leonardo,12,0.04,1,0.04
arduino:sam:arduino_due_x_dbg,80,0.02,N/A,N/A
arduino:samd:mkrzero,64,0.02,4,0.01

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Jun 25, 2024
Copy link
Collaborator

@edgar-bonet edgar-bonet left a 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;
Copy link
Collaborator

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.

Copy link
Author

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){
Copy link
Collaborator

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;
Copy link
Collaborator

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;
}
}
Copy link
Collaborator

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){
Copy link
Collaborator

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){
Copy link
Collaborator

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.

Copy link
Author

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().

@dragonerboom dragonerboom marked this pull request as draft June 25, 2024 23:51
@per1234 per1234 added the conclusion: invalid Issue/PR not valid label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants