I'm learning library making on arduino , I want to make a library that works with arrays a user sets, e.g:
User sets array of int, when a certain function is been called the library will check if some instance are true or not ann set the return function accordingly as the array.
Code example:
//in project example
#include <Keyss.h>
int rows=4;
int cols=2;
int a=0;
int b=1;
int c=2;
int inp=3;
int keyss[4][2]={
{0,1},,
{2,3},
{4,5},
{6,7}
};
Key key((keyss),rows,cols,inp,a,b,c);
void setup(){
Serial.begin(9600);
int keyprint=key.getkey();
Serial.println(key print);
}
void loop(){
}
And this will be the library files Keys.h
#ifndef KEYSS_H
#define KEYSS_H
#include <Arduino.h>
class Key {
public:
Key(int (*keys)[4],int rows,int cols,int inp,int t1,int t2,int t3);
int getkey();
int waitforkey();
private:
int *_keys;
int _rows;
int _cols;
int _inp;
int _t1;
int _t2;
int _t3;
};
#endif
And Keys.CPP
#include <Keyss.h>
//initialization
Key::Key(int (*keys)[4],int rows,int cols,int inp,int t1,int t2,int t3){
_keys=keys[4];
_rows=rows;
_cols=cols;
_inp=inp;
_t1=t1;
_t2=t2;
_t3=t3;
pinMode(_inp,INPUT);
pinMode(_t1,OUTPUT);
pinMode(_t2,OUTPUT);
pinMode(_t3,OUTPUT);
}
//getkey()
int Key::getkey(){
int _a;
int _b;
int _i=0;
int _keymap=false;
int _val;
int _dt=500;
int _s1;
int _input;
for(_a=0;_a<_rows;_a++){
for(_b=0;_b<_cols;_b++){
_s1=_i;
digitalWrite(_t1,(_s1>>0)&1);
digitalWrite(_t2,(_s1>>1)&1);
digitalWrite(_t3,(_s1>>2)&1);
delayMicroseconds(_dt);
_input=digitalRead(_inp);
if(_input==true && _keymap==false){
_val=_keys[_a*_cols+_b];
_keymap=true;
}
_i++;
}
}
if(_keymap==false){
_val=-1;
}
return _val;
}
//waitforkey()
int Key::waitforkey(){
int _waitfor=getkey();
while(_waitfor<0){
_waitfor=getkey();
}
return _waitfor;
}
Where I have problem now is I don't know how to use or assign an array in a library I don't know how to use functions to refer to an array a user inputes, like here
int keyss[4][2]={
{0,1},
{2,3},
{4,5},
{6,7}
};
How do I use a function or inside the library to refer to the array a user sets?
Please how do I achieve that?
Its giving this error error message
1 Answer 1
Your keys
variable is a 2D array of int
, i.e. it is an array of
arrays. In C++, when an array is assigned to a variable or passed as an
argument, it implicitly decays to a pointer to its first element.
Note, however, that the decay-to-pointer behavior is not recursive.
In this case you end up with a pointer to an array of three int
.
Your code should work if you give the proper type to the _keys
class
member and the keys
constructor parameter:
class Key {
public:
Key(int (*keys)[3], int trig1, int trig2, int trig3, int inpin);
int getkey();
private:
int (*_keys)[3]; // pointer to array of 3 int.
int _trig1;
int _trig2;
int _trig3;
int _inpin;
int _keye;
};
Edit: You have completely changed the question, to the point that my answer may not be relevant anymore. In the previous version of the question, it looked like you wanted to use a ×ばつ3 2D array of numbers in the class. In the current version, you are using an array of arbitrary sizes represented as a flattened 1D array. These are quite different approaches.
If you want to use a 2D array, the number of columns has to be a compile-time constant. You have to declare the member variable as a pointer-to-array like this:
int (*_keys)[2]; // there are two columns, not four!
assign it like this:
_keys = keys; // do NOT subscript keys
and use it like this:
_val = _keys[_a][_b]; // it is a 2D array
This approach cannot work if you do not know the number of columns beforehand.
If you want to use a flattened (1D) array, then the parameter of the
constructor has to be a simple int*
, and you have to give it the
address of the first element of the original 2D array:
Key key(&keyss[0][0], rows, cols, inp, a, b, c);
The drawback of using a flattened array is that, in order to access the elements, you have to perform explicit index calculations:
_val = _keys[_a*_cols + _b];
-
I tried it but I don't know the correct way to do itJoseph Afodu– Joseph Afodu2023年01月16日 07:13:38 +00:00Commented Jan 16, 2023 at 7:13
-
Thanks bro I didn't understand it that much earlier and also I am new to programming, but now I have understand it moreJoseph Afodu– Joseph Afodu2023年01月16日 14:24:43 +00:00Commented Jan 16, 2023 at 14:24