Ideas for keeping track of the state of individual switches within a matrix?

Status
Not open for further replies.

Geomancer

Active member
I will have an 8x8 switch matrix that's connected to a 16 port I/O expander that's communicated to via I2C. Each switch has a diode attached to it so that when multiple switch closures occur simultaneously there isn't any ghosting. In total there will be 64 switches.

Right now I have just a single uint64_t variable where each bit represents one of the switches. This gets updated once per loop.

In my code I'll want to process events from these switches, but only once per closure and/or release.

The "Bounce" library is basically exactly what I want ... but it's made for directly reading I/O ports rather than through an I2C device (or RAM variable).

What's the best way to implement something like this?

Would I need an array of 64 uint8_t variables to keep track of the state of every switch? That seems like the logical choice, but does consume a chunk of RAM where each would only be keeping track of a few states (around 5 or 6). I can't really think of any way around it though. So that's why I'm here :D

RAM is there to be used though, so why not? I just hate wasting... don't know why. As an aside: Is there a minimum size of actual RAM used regardless of the type used?

Thanks!
 
Would I need an array of 64 uint8_t variables to keep track of the state of every switch? That seems like the logical choice, but does consume a chunk of RAM where each would only be keeping track of a few states (around 5 or 6). I can't really think of any way around it though. So that's why I'm here :D

RAM is there to be used though, so why not? I just hate wasting... don't know why.

I'd go with the simplest way first, then try optimizing only after it's confirmed working, with a backup copy saved!


Is there a minimum size of actual RAM used regardless of the type used?

Yes, lots of stuff uses RAM by default. The USB code in particular needs to allocate several buffers to send and receive packets.
 
Yes, lots of stuff uses RAM by default. The USB code in particular needs to allocate several buffers to send and receive packets.

Whoops, I worded my question wrong.

I was meaning instead is, would a uint8_t switchState[64] take up 64 bytes of RAM, or something more? I'm use to 8-bit micros rather than 32-bit.
 
or a single uint64_t, and bitshift all 64 bits (or simply bitSet, bitClear, etc)

EDIT I reread your using this method, this would be the most efficient and space conserving method
 
Last edited:
Whoops, I worded my question wrong.

I was meaning instead is, would a uint8_t switchState[64] take up 64 bytes of RAM, or something more? I'm use to 8-bit micros rather than 32-bit.

Yes, uint8_t switchState[64] would take 64 bytes of RAM. 64 bits would be much more compact. At some point you'll need a second copy of at least part of the data to compare old versus new state.
 
Status
Not open for further replies.
Back
Top