Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to structure array from main.c?

Reply
Thread Tools

pointer to structure array from main.c?

 
 
Steve
Guest
Posts: n/a
 
      03-30-2007

Hi,
I have a keypad that I am scanning to get key states, and am putting
those states in a structure. 16 keys, so I have it like
keys[0].status=PRESSSED. to keys[16].status etc.

I can access the elements in keypad.c, but why can't I see it from
main.c? It says I need a pointer type, but I can't figure it out yet.

Can someone show me how to access my key information from main.c?
Thankyou. Here are relevant files

*********************main.c*********************** ************
#include "includes.h" //which includes keypad.h
//KEY *pKey; //commented out
//pKey = &keys;
//*pKEY = &KEY;

//init I/O for keypad
keypad_init();
while(1){
temp =scankeypad();
temp =scankeypad();
if(keys(1).status==KEYPRESSED){// ***This line gives me an error,
wheras in the keypad.c file I can access this. Gives, must have a
pointer to type function.
fprintf(pntr1, "Key1pressed");
}else{
fprintf(pntr1, "Key1Notpressed");
}

}


**********************keypad.h******************** *************/

#include <includes.h>

#ifndef __KEYPAD_H
#define __KEYPAD_H
#define KEYDOWN 1
#define KEYUP 0
#define MAXDOWNCOUNT 100
#define KEYPRESSED 1
#define KEYNOTPRESSED 0



typedef struct keys {
unsigned char state;
unsigned int downcount;
unsigned char status;
}KEY;


typedef enum {//BIT POSITIONS OF KEYS IN keysdown
UP = 0, //SW4
DOWN, //SW8
ENTER, //SW12
BACK, //SW16
SW5,
SW9,
SW13,
SW17,
SW6,
SW10,
SW14,
SW18,
SW7,
SW11,
SW15,
SW19,
STRIPSWITCH
} keyname;


/* Declare functions */
void keypad_init(void);
unsigned int scankeypad(void);
//void getStatusForKey(struct key *pkey);
#endif

**********************keypad.C******************** *************/

#include "keypad.h"
/************************************************** *********
* Function:
*
************************************************** *********/



//setup I/O of matrix. Internal Pullups on Rows which are inputs.

#define row0 (1<<16)
#define row1 (1<<17)
#define row2 (1<<1
#define row3 (1<<19)
#define col0 (1<<20)
#define col1 (1<<21)
#define col2 (1<<22)
#define col3 (1<<23)

void keypad_init(){

IO1DIR |= 0x00f00000;//make rows inputs and columns outputs
1=output, 0 = input
IO1DIR &= 0xfff0ffff;//make columns outputs

//FIO1DIR2 is bits16-23 of port1 which are the rows and columns
//Set all high, and pull columns low to see which row key is
pressed.
/*
P1_16 // Row0 = GPIO
P1_17 // Row1 = GPIO
P1_18 // Row2 = GPIO
P1_19 // Row3 = GPIO

P1_20 // Col0 = GPIO
P1_21 // Col1 = GPIO
P1_22 // Col2 = GPIO
P1_23 // Col3 = GPIO
*/
/*while(1){

IO1CLR |= (col0 | col1 | col2 | col3);
}*/
}//keypad_init


unsigned int scankeypad(void){
unsigned int keysdown=0;
unsigned int temp=0;
unsigned char colbit, rowbit,keybit=0;

KEY keys[10];

IO1SET |= (col0 | col1 | col2 | col3);// set all columns high at
first

for(colbit=20;colbit<24;colbit++){
IO1CLR |= (1<<colbit);//set a column low

for(rowbit=16;rowbit<20;rowbit++){
temp=(IO1PIN & (1<<rowbit));
if(temp==0){//if this row is low update keysdown
keysdown |= (1<<keybit); //place 1's in down key positions,
bit0 is first scanned
keys[keybit].state=KEYDOWN;
if(keys[keybit].downcount<MAXDOWNCOUNT ){
keys[keybit].downcount++;
}else{
if(keys[keybit].downcount==MAXDOWNCOUNT){
keys[keybit].status=KEYPRESSED;
}
}
}else{ //key is up
keysdown =keysdown & ~(0<<keybit);//place 0's in up keypad bit
positions
keys[keybit].status=KEYNOTPRESSED;
keys[keybit].downcount=0;
keys[keybit].state=KEYUP;
}
keybit++;
}
IO1SET |= (1<<colbit);//set the column back high
}
return keysdown;
}
 
Reply With Quote
 
 
 
 
Steve
Guest
Posts: n/a
 
      03-30-2007
On Fri, 30 Mar 2007 09:30:35 -0400, Steve <> wrote:

>
>Hi,
>I have a keypad that I am scanning to get key states, and am putting
>those states in a structure. 16 keys, so I have it like
>keys[0].status=PRESSSED. to keys[16].status etc.
>
>I can access the elements in keypad.c, but why can't I see it from
>main.c? It says I need a pointer type, but I can't figure it out yet.
>
>Can someone show me how to access my key information from main.c?
>Thankyou. Here are relevant files


Joachim, Thankyou very much. I totally missed that () as opposed to
[]. I did have the extern up top at one point, but I can't believe
you caught that subtle error.
Thankyou,
Steve (I accidentally deleted the response by mistake, but I did get
and compile it now.)
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-30-2007
"Steve" <> schrieb im Newsbeitrag
news:...
>
> Hi,
> I have a keypad that I am scanning to get key states, and am putting
> those states in a structure. 16 keys, so I have it like
> keys[0].status=PRESSSED. to keys[16].status etc.
>
> I can access the elements in keypad.c, but why can't I see it from
> main.c? It says I need a pointer type, but I can't figure it out yet.
>
> Can someone show me how to access my key information from main.c?
> Thankyou. Here are relevant files
>
> *********************main.c*********************** ************
> #include "includes.h" //which includes keypad.h
> //KEY *pKey; //commented out
> //pKey = &keys;
> //*pKEY = &KEY;
>
> //init I/O for keypad
> keypad_init();
> while(1){
> temp =scankeypad();
> temp =scankeypad();
> if(keys(1).status==KEYPRESSED){// ***This line gives me an error,

if (keys[1].status==KEYPRESSED) { /* need to use [], not ()*/
/* Beginning of main() is missing as well as an extern KEY keys[]; */

> wheras in the keypad.c file I can access this. Gives, must have a
> pointer to type function.
> fprintf(pntr1, "Key1pressed");
> }else{
> fprintf(pntr1, "Key1Notpressed");
> }
>
> }
>
>
> **********************keypad.h******************** *************/
>
> #include <includes.h>
>
> #ifndef __KEYPAD_H
> #define __KEYPAD_H
> #define KEYDOWN 1
> #define KEYUP 0
> #define MAXDOWNCOUNT 100
> #define KEYPRESSED 1
> #define KEYNOTPRESSED 0
>
>
>
> typedef struct keys {
> unsigned char state;
> unsigned int downcount;
> unsigned char status;
> }KEY;
>
>
> typedef enum {//BIT POSITIONS OF KEYS IN keysdown
> UP = 0, //SW4
> DOWN, //SW8
> ENTER, //SW12
> BACK, //SW16
> SW5,
> SW9,
> SW13,
> SW17,
> SW6,
> SW10,
> SW14,
> SW18,
> SW7,
> SW11,
> SW15,
> SW19,
> STRIPSWITCH
> } keyname;
>
>
> /* Declare functions */
> void keypad_init(void);
> unsigned int scankeypad(void);
> //void getStatusForKey(struct key *pkey);
> #endif
>
> **********************keypad.C******************** *************/
>
> #include "keypad.h"
> /************************************************** *********
> * Function:
> *
> ************************************************** *********/
>
>
>
> //setup I/O of matrix. Internal Pullups on Rows which are inputs.
>
> #define row0 (1<<16)
> #define row1 (1<<17)
> #define row2 (1<<1
> #define row3 (1<<19)
> #define col0 (1<<20)
> #define col1 (1<<21)
> #define col2 (1<<22)
> #define col3 (1<<23)
>
> void keypad_init(){
>
> IO1DIR |= 0x00f00000;//make rows inputs and columns outputs
> 1=output, 0 = input
> IO1DIR &= 0xfff0ffff;//make columns outputs
>
> //FIO1DIR2 is bits16-23 of port1 which are the rows and columns
> //Set all high, and pull columns low to see which row key is
> pressed.
> /*
> P1_16 // Row0 = GPIO
> P1_17 // Row1 = GPIO
> P1_18 // Row2 = GPIO
> P1_19 // Row3 = GPIO
>
> P1_20 // Col0 = GPIO
> P1_21 // Col1 = GPIO
> P1_22 // Col2 = GPIO
> P1_23 // Col3 = GPIO
> */
> /*while(1){
>
> IO1CLR |= (col0 | col1 | col2 | col3);
> }*/
> }//keypad_init
>
>
> unsigned int scankeypad(void){
> unsigned int keysdown=0;
> unsigned int temp=0;
> unsigned char colbit, rowbit,keybit=0;
>
> KEY keys[10];
>
> IO1SET |= (col0 | col1 | col2 | col3);// set all columns high at
> first
>
> for(colbit=20;colbit<24;colbit++){
> IO1CLR |= (1<<colbit);//set a column low
>
> for(rowbit=16;rowbit<20;rowbit++){
> temp=(IO1PIN & (1<<rowbit));
> if(temp==0){//if this row is low update keysdown
> keysdown |= (1<<keybit); //place 1's in down key positions,
> bit0 is first scanned
> keys[keybit].state=KEYDOWN;
> if(keys[keybit].downcount<MAXDOWNCOUNT ){
> keys[keybit].downcount++;
> }else{
> if(keys[keybit].downcount==MAXDOWNCOUNT){
> keys[keybit].status=KEYPRESSED;
> }
> }
> }else{ //key is up
> keysdown =keysdown & ~(0<<keybit);//place 0's in up keypad bit
> positions
> keys[keybit].status=KEYNOTPRESSED;
> keys[keybit].downcount=0;
> keys[keybit].state=KEYUP;
> }
> keybit++;
> }
> IO1SET |= (1<<colbit);//set the column back high
> }
> return keysdown;
> }



 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-30-2007
"Steve" <> schrieb im Newsbeitrag
news:...
> On Fri, 30 Mar 2007 09:30:35 -0400, Steve <> wrote:
>
>>
>>Hi,
>>I have a keypad that I am scanning to get key states, and am putting
>>those states in a structure. 16 keys, so I have it like
>>keys[0].status=PRESSSED. to keys[16].status etc.
>>
>>I can access the elements in keypad.c, but why can't I see it from
>>main.c? It says I need a pointer type, but I can't figure it out yet.
>>
>>Can someone show me how to access my key information from main.c?
>>Thankyou. Here are relevant files

>
> Joachim, Thankyou very much. I totally missed that () as opposed to
> []. I did have the extern up top at one point, but I can't believe
> you caught that subtle error.

I've made enough of these mistakes myself 8.)

Bye, Jojo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments