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?
