Skip to content

Commit

Permalink
reworked button support, added joystick and rotary selection to menu.
Browse files Browse the repository at this point in the history
- IoTuz::encoder_changed now says by how many clicks it changed
- 3 methods added to return button changes, removed scan_buttons from
  fulldemo
- read_joystick was also cleaned up and made a proper method
- get_selections now works with both joystick and rotary encoder, with
  the help of a moveable graphical menu selection.
- leddemo removeds, didn't really belong here.
  • Loading branch information
marcmerlin committed Apr 15, 2017
1 parent c13a8c7 commit 2ee418b
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 352 deletions.
81 changes: 62 additions & 19 deletions IoTuz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,79 @@ int16_t IoTuz::read_encoder()
return encoder0Pos;
}

bool IoTuz::encoder_changed() {
int8_t IoTuz::encoder_changed() {
static int16_t old_encoder0Pos = 0;
if (encoder0Pos != old_encoder0Pos)
{
old_encoder0Pos = encoder0Pos;
return true;
}
return false;
int8_t encoder0Diff = encoder0Pos - old_encoder0Pos;

old_encoder0Pos = encoder0Pos;
return encoder0Diff;
}

// There is no separate method to return button state changed
// because this one already does it, look for PUSHED and RELEASED
ButtState IoTuz::read_encoder_button()
ButtState IoTuz::_but(uint8_t button)
{
static bool butEnc = false;
uint8_t butt_state = i2cexp_read() & I2CEXP_ENC_BUT;
static bool but[9];
uint8_t butt_state = i2cexp_read() & button;

if (butt_state && !butEnc)
if (butt_state && !but[button])
{
butEnc = true;
//Serial.println("Encoder Button Pushed");
return ENC_PUSHED;
but[button] = true;
Serial.println("Button Pushed");
return BUT_PUSHED;
}
if (!butt_state && butEnc)
if (!butt_state && but[button])
{
butEnc = false;
//Serial.println("Encoder Button Released");
return ENC_RELEASED;
but[button] = false;
Serial.println("Button Released");
return BUT_RELEASED;
}
return (butt_state?BUT_DOWN:BUT_UP);
}

ButtState IoTuz::butEnc()
{
return(_but(I2CEXP_ENC_BUT));
}

ButtState IoTuz::butA()
{
return(_but(I2CEXP_A_BUT));
}

ButtState IoTuz::butB()
{
return(_but(I2CEXP_B_BUT));
}


void IoTuz::read_joystick(bool showdebug)
{
// X is wired in reverse.
joyValueX = 4096-analogRead(JOYSTICK_X_PIN);
joyValueY = analogRead(JOYSTICK_Y_PIN);
joyBtn = !digitalRead(JOYSTICK_BUT_PIN);

// Sadly on my board, the middle is 1785/1850 and not 2048/2048 and it's not the same
// on other boards, so add a huge dead center:
joyRelX = map(joyValueX, 0, 1600, -5, 0);
joyRelY = map(joyValueY, 0, 1600, -5, 0);
if (joyValueX > 1600) joyRelX = map(constrain(joyValueX, 2400, 4095), 2400, 4095, 0, 5);
if (joyValueY > 1600) joyRelY = map(constrain(joyValueY, 2400, 4095), 2400, 4095, 0, 5);

if (showdebug) {
// print the results to the serial monitor:
Serial.print("X Axis = ");
Serial.print(joyValueX);
Serial.print("/rel: ");
Serial.print(joyRelX);
Serial.print("\t Y Axis = ");
Serial.print(joyValueY);
Serial.print("/rel: ");
Serial.print(joyRelY);
Serial.print("\t Joy Button = ");
Serial.println(joyBtn);
}
return (butt_state?ENC_DOWN:ENC_UP);
}

// True turns the BL on
Expand Down
23 changes: 17 additions & 6 deletions IoTuz.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ extern IRrecv irrecv;
extern decode_results IR_result;

typedef enum {
ENC_DOWN = 0,
ENC_PUSHED = 1,
ENC_UP = 2,
ENC_RELEASED = 3,
BUT_DOWN = 0,
BUT_PUSHED = 1,
BUT_UP = 2,
BUT_RELEASED = 3,

} ButtState;

Expand All @@ -196,6 +196,13 @@ class IoTuz {
// tft_width, tft_height, calculated in setup after tft init
uint16_t tftw, tfth;

// Actual analog values, 0 to 4096
uint16_t joyValueX, joyValueY;
// massaged values to remove dead center and return -5 to +5
int8_t joyRelX, joyRelY;
bool joyBtn;


// Buffer to store strings going to be printed on tft
char tft_str[64];

Expand All @@ -204,8 +211,11 @@ class IoTuz {
void i2cexp_set_bits(uint8_t);
uint8_t i2cexp_read();
int16_t read_encoder();
bool encoder_changed();
ButtState read_encoder_button();
int8_t encoder_changed();
ButtState butEnc();
ButtState butA();
ButtState butB();
void read_joystick(bool showdebug=false);
float battery_level();
void screen_bl(bool);
void reset_tft();
Expand All @@ -217,6 +227,7 @@ class IoTuz {
private:
uint8_t _i2cexp;
void pcf8574_write_(uint8_t);
ButtState _but(uint8_t);
};


Expand Down
14 changes: 8 additions & 6 deletions examples/EncoderTestPinIntrButt/EncoderTestPinIntrButt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,29 @@ void loop() {
}

// There is no debouncing here, delay at the end will help a bit, but is not foolproof
switch (iotuz.read_encoder_button()) {
case ENC_DOWN:
switch (iotuz.butEnc()) {
case BUT_DOWN:
// This would spam the console too much
//Serial.println("Encoder Button Down");
iotuz.tftprint(8, 1, 8, "Down");
break;
case ENC_PUSHED:
case BUT_PUSHED:
Serial.println("Encoder Button Just Pushed");
iotuz.tftprint(8, 1, 8, "Pushed");
break;
case ENC_UP:
case BUT_UP:
//Serial.println("Encoder Button Up");
iotuz.tftprint(8, 1, 8, "Up");
break;
case ENC_RELEASED:
case BUT_RELEASED:
Serial.println("Encoder Button Just Released");
iotuz.tftprint(8, 1, 6, "Released");
break;
}


//Serial.println("Do other stuff in loop()");
delay(10);
delay(100);
}

// vim:sts=4:sw=4
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/fulldemo/Handheld-Color-Console/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Joystick

static boolean fire()
{
return (digitalRead(JOYSTICK_BUT_PIN) == LOW || iotuz.read_encoder_button() == ENC_DOWN);
return (digitalRead(JOYSTICK_BUT_PIN) == LOW || iotuz.butEnc() == BUT_DOWN);
}

static void waitForRelease()
Expand Down
Loading

0 comments on commit 2ee418b

Please sign in to comment.