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

Consider providing CLK_ defines as dictionary #5

Closed
JiFish opened this issue Jun 19, 2016 · 12 comments
Closed

Consider providing CLK_ defines as dictionary #5

JiFish opened this issue Jun 19, 2016 · 12 comments

Comments

@JiFish
Copy link
Contributor

JiFish commented Jun 19, 2016

In the project I am working on, I want the user to be able to choose a key in a configuration file. This has forced me to create a dict so I can reference key names. This is a shame, because it's basically repeating information already in cue_sdk.

This problem could be avoided if cue_sdk already provided the values in a dict. Making it easy to do stuff like:

from cue_sdk import CUE, CLK_KEYS

Corsair = CUE("CUESDK.x64_2013.dll")

keyname = input("Enter a keyname.")
if (keyname in CLK_KEYS):
    print "That is a valid key."
else:
    print "That is an invalid key."

for key in CLK_KEYS:
    # Do something for every key
    Corsair.SetLedsColors(1, CorsairLedColor(CLK_KEYS[key], 255, 255, 255))

etc.

Thoughts? I'm not hugely experienced in this area, so maybe there is something I am missing?

@JiFish JiFish changed the title Conisder providing CLK_ defines are dictionary Conisder providing CLK_ defines as dictionary Jun 19, 2016
@JiFish JiFish changed the title Conisder providing CLK_ defines as dictionary Consider providing CLK_ defines as dictionary Jun 19, 2016
@10se1ucgo
Copy link
Owner

10se1ucgo commented Jun 19, 2016

It'll be better if I just use an IntEnum class to hold the keys. I'll get on that now. You'll be able to index it with CLK[key_name] and check for membership by try/catching a KeyError, or checking if key_name in CLK.__members__

For now though, to test if a x is a valid key check if CLK_ESCAPE <= x <= CLI_Last, and to perform an action on every key, do

for key in range(CLK_ESCAPE, CLI_Last + 1):
    Corsair.SetLedsColors(1, CorsairLedColor(key, 255, 255, 255))

@JiFish
Copy link
Contributor Author

JiFish commented Jun 19, 2016

Thanks for replying and apologies in advance for stupid questions.

I'm using python 2.7 are enums available here?

So will this additionally solve the issue of getting a key's number from a user provided string of it's name? i.e. would this work?

'CLK_A' in CLK.__members__:
    # do something
print CLK['CLK_A']

etc.
I ask because I think it's friendlier to ask the user to put the key name in the config file, rather than it's id.

@10se1ucgo
Copy link
Owner

10se1ucgo commented Jun 19, 2016

Enums are available via backport for any python version below 3.4

It would likely be

'A' in CLK.__members__

or

try:
    CLK['A']
    valid_key = True
except KeyError:
    valid_key = False

@JiFish
Copy link
Contributor Author

JiFish commented Jun 19, 2016

Great, that's what I was hoping.

@10se1ucgo
Copy link
Owner

10se1ucgo commented Jun 19, 2016

I've run into a slight problem.

CLK and CDC contain names that are not valid Python identifiers (e.g. CLK_1 -> CLK.1, CDC_None -> CDC.None. Both raise SyntaxErrors), you will have to index them in a non-normal way.

For example, for the 1 key, you can index it the following ways:

CLK._1
CLK[1]
CLK['1']

for the device capability None

CDC._None
CDC[None]
CDC['None']

All in all, not really a big deal. Only affects the programmer really, makes it slightly inconvenient.

The good news is that I've implemented easier membership testing. Now you can do

>>> 'A' in CLK
True

instead of the previous methods I showed you.

For the weird ones, you can do

>>> '1' in CLK
True
>>> '_1' in CLK
True

>>> 'None' in CDC
True
>>> '_None' in CDC
True

Unfortunately, you can't use the integer or NoneType values like you can to index.

I'll push an update later.

@10se1ucgo
Copy link
Owner

10se1ucgo commented Jun 19, 2016

Oh wow, I just remembered, you can get the key ID from key name with Corsair.GetLedIdForKeyName, so this issue technically can be closed, but the enum is more convenient. Also, the GetLedIdForKeyName function returns the key code relative to logical layout (e.g., GetLedIdForKeyName('a') on an AZERTY keyboard would return CLK.Q instead of CLK.A), which is quite useless unless you are trying to make animations or something.

@JiFish
Copy link
Contributor Author

JiFish commented Jun 19, 2016

GetLedIdForKeyName implements CorsairGetLedIdForKeyName which only takes a char not a string, so I was unsure how to check non-alphanumeric keys. If it's even possible that way. In fact based on the documentation, you can't even look up numbers and punctuation!

Input arguments:
• char keyName - key name. [‘A’..’Z’] (26 values) are valid values.

@10se1ucgo
Copy link
Owner

Ah, that's incredibly dumb.

@JiFish
Copy link
Contributor Author

JiFish commented Jun 19, 2016

I assumed it was just for dealing with keyboards with alternate layouts: AZERTY, dvorak etc. Since it didn't seem to do what I wanted, I ignored it. :)

@10se1ucgo
Copy link
Owner

I'm taking a bit longer because I'm trying to abstract away the usage of ctypes structures and pointers and trying to give the developer the data directly, instead of letting them fend for their own. I'll have an update pushed out within 30 minutes.

10se1ucgo added a commit that referenced this issue Jun 19, 2016
- Moved all defines to their own Enum, see issue #5 for more details
- Abstracted away structures and pointers, now you get easy-to-use namedtuples!
- Updated examples to reflect new API.
@10se1ucgo
Copy link
Owner

10se1ucgo commented Jun 19, 2016

Done! eb99b58

Note: It breaks pretty much everything, you'll have to redo many parts of your program. Sorry!

@JiFish
Copy link
Contributor Author

JiFish commented Jun 19, 2016

Nice, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants