Jump to content

Simple keyboard matrix scanner


ballooneater
 Share

Recommended Posts

I'm interested in creating a simple 8x8 keyboard matrix scanner and was wondering if I was heading down the right path with my code.

Initializations:


int m,j,i;

for(m=0;m<8;m++){

	MIOS32_DOUT_PinSet(m,1);

}

int prevState[8] = {1,1,1,1,1,1,1,1};

And this is inside of APP_Background, repeating endlessly:

for (j=0; j<8; j++) {

	MIOS32_DOUT_PinSet(j,0);


	for (i=0; i<8; i++) {

		if(MIOS32_DIN_PinGet(i)==0 && prevState[i]==1)

		{

			//Send button on message

			prevState[i]=0;		

		}

		if (MIOS32_DIN_PinGet(i)==1 && prevState[i]==0) 

		{

			//Send button off message

			prevState[i]=1;

		}

		else prevState[i] = MIOS32_DIN_PinGet(i);

	}

	MIOS32_DOUT_PinSet(j,1);

}

Link to comment
Share on other sites

At a quick glance, I'd make the comment that you should use SRIO service prepare to set up the dout, and service finnish to read and process the din.

By reading and writing in the background loop your setting and reading the ram image of srio transfers but you need to sychronise with the actual transfers that occur each millisecond.

Link to comment
Share on other sites

At a quick glance, I'd make the comment that you should use SRIO service prepare to set up the dout, and service finnish to read and process the din.

By reading and writing in the background loop your setting and reading the ram image of srio transfers but you need to sychronise with the actual transfers that occur each millisecond.

Thanks! I'll try using the SRIO functions and see how that goes.

Edit: how does this look?



void APP_Background(void){


	u8 shift = 0x01;

	u8 i = 0;

	u8 j = 0;


	while(1){

		shift = 0x01;

		for (i=0;i<8;i++)

		{

			mios32_srio_dout[0] = shift;


			for (j=0;j<8;j++)

			{

				if(mios32_srio_din[0] & (0x01 << j))

				{

					//Note is on

				}

				else {

					//Note is off

				}


			}

			shift << 1;

		}

	}

}


Edited by ballooneater
Link to comment
Share on other sites

The mios32 arrays in your code are still the ram values that get used, and are unsynchronised with the actual transfers.

If I was doing this the first thought is to set the DOUT pins in a routine called from inside:

/////////////////////////////////////////////////////////////////////////////

// This hook is called before the shift register chain is scanned

/////////////////////////////////////////////////////////////////////////////

void APP_SRIO_ServicePrepare(void)

{

}
and then read the DIN pins from a routine inside:
/////////////////////////////////////////////////////////////////////////////

// This hook is called after the shift register chain has been scanned

/////////////////////////////////////////////////////////////////////////////

void APP_SRIO_ServiceFinish(void)

{

}

In the repo Apps/examples is a 16x16 decoder app that uses another method: define an RTOS task that does the scan and calls the notify toggle hook. There are always very many solutions to a given requirement!

Link to comment
Share on other sites

  • 2 weeks later...

I think I get what you mean now. Is this in line with what you were thinking?


volatile u8 shift = 0x01;

volatile u8 shiftCount = 0;

volatile u16 din_values;


void APP_SRIO_ServicePrepare(void){

	MIOS32_IRQ_Disable();

	mios32_srio_dout[15] = shift;

	MIOS32_IRQ_Enable();

}


void APP_SRIO_ServiceFinish(void){


	MIOS32_IRQ_Disable();


	//Assign first 8 bits of din_values

	din_values = mios32_srio_din[0];	

	//Assign 9th bit

	din_values |= (mios32_srio_din[1] << 8);


	MIOS32_IRQ_Enable();


	u8 j=0;


	for (j=0;j<9;j++)

	{

		if(din_values & (0x01 << j))

		{

			noteOn(j,shiftCount);

		}

		else {

			noteOff(j,shiftCount);

		}

	}


	if (shift == 0x20)

	{

		shift = 0x01;

		shiftCount = 0;

	}

	else 

	{

		shift = shift << 1;

		shiftCount++;

	}

}

Edit: It's worth noting that my set-up uses 6 DOUT pins and 9 DIN

Edited by ballooneater
Link to comment
Share on other sites

At a glance, it embodies the idea.

two points I can make:

1. irq enable and disable is allready handled by the system. You dont need to call them when using these hooks .

2. you call noteon() and noteoff() on the pin state. This results in repeated calls on the same keypress (and keyoff!). You really need to call them only on a (debounced) transition!

There are various examples in the repo to study to see how these tasks typically get done.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...