B4R Question How to make Esplora object.

ellpopeb4a

Member
Licensed User
Longtime User
Hi all..
I should enable events "Btn StateChanged" of the four button "switch_1/switch_4" Esplora how can I do?
I saw that there is the library "Esplora.cpp" how can I use?
Thank you..
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with this code that reads the joysticks:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private timer1 As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   timer1.Initialize("timer1_Tick", 100)
   timer1.Enabled = True
   
End Sub

Sub Timer1_Tick
   Dim jx As Int = RunNative("readJoystickX", Null)
   Dim jy As Int = RunNative("readJoystickY", Null)
   Log("jx: ", jx, ", jy: ", jy)
End Sub

#if C
#include <Esplora.h>
B4R::Object res;
 B4R::Object* readJoystickX(B4R::Object* o) {
   return res.wrapNumber(Esplora.readJoystickX());
}
B4R::Object* readJoystickY(B4R::Object* o) {
   return res.wrapNumber(Esplora.readJoystickY());
}
B4R::Object* readButton(B4R::Object* channel) {
   return res.wrapNumber(Esplora.readButton(channel->toULong()));
}
#end if

Does it work for you (I don't have such board here)?
 
Upvote 0

ellpopeb4a

Member
Licensed User
Longtime User
Thank you..Erel
It is working well
In the log see valute change..

I had made a simple library to test the rgb led this work.


B4X:
#pragma once
#include "B4RDefines.h"
//~dependson: <Esplora.h>
namespace B4R {
    //~Version: 1.00
    //~ShortName: B4REsplora
    class B4REsplora {
        /*
* The following constants are used internally by the Esplora
* library code.
*/
const byte JOYSTICK_BASE  = 16; // it's a "virtual" channel: its ID won't conflict with real ones

const byte MAX_CHANNELS   = 13;

const byte CH_SWITCH_1    = 0;
const byte CH_SWITCH_2    = 1;
const byte CH_SWITCH_3    = 2;
const byte CH_SWITCH_4    = 3;
const byte CH_SLIDER      = 4;
const byte CH_LIGHT       = 5;
const byte CH_TEMPERATURE = 6;
const byte CH_MIC         = 7;
const byte CH_JOYSTICK_SW = 10;
const byte CH_JOYSTICK_X  = 11;
const byte CH_JOYSTICK_Y  = 12;

/*
* The following constants can be used with the readButton()
* method.
*/

const byte SWITCH_1       = 1;
const byte SWITCH_2       = 2;
const byte SWITCH_3       = 3;
const byte SWITCH_4       = 4;

const byte SWITCH_DOWN  = SWITCH_1;
const byte SWITCH_LEFT  = SWITCH_2;
const byte SWITCH_UP    = SWITCH_3;
const byte SWITCH_RIGHT = SWITCH_4;

const byte JOYSTICK_DOWN  = JOYSTICK_BASE;
const byte JOYSTICK_LEFT  = JOYSTICK_BASE+1;
const byte JOYSTICK_UP    = JOYSTICK_BASE+2;
const byte JOYSTICK_RIGHT = JOYSTICK_BASE+3;

/*
* These constants can be use for comparison with the value returned
* by the readButton() method.
*/
const boolean PRESSED   = LOW;
const boolean RELEASED  = HIGH;

/*
* The following constants can be used with the readTemperature()
* method to specify the desired scale.
*/
const byte DEGREES_C = 0;
const byte DEGREES_F = 1;

/*
* The following constants can be used with the readAccelerometer()
* method to specify the desired axis to return.
*/
const byte X_AXIS = 0;
const byte Y_AXIS = 1;
const byte Z_AXIS = 2;
        private:

        public:
        #define /*Byte SWITCH_UP;*/ B4REsplora_SWITCH_UP SWITCH_UP
    
        //Sets the writeRGB
        void rwriteRGB(byte red, byte green, byte blue);
    };
}


B4X:
#include "B4RDefines.h"
namespace B4R {
    void B4REsplora::rwriteRGB(byte r, byte g, byte b) {
      Esplora.writeRGB(r,g,b);
    }
}

The problem for me is to create an event in the change of state of the buttons

It would be nice in that there was a call to a "Sub xx_StateChanged"

Thank Erel
Ps: this read the state.

B4X:
Dim but As Int = RunNative("readButton",1) '1 = SWITCH_DOWN, 2 = SWITCH_LEFT, 3 = SWITCH_UP, 4 = SWITCH_RIGHT
 
Last edited:
Upvote 0
Top