From 4933a6ed594c39ec9e6e4f579a8ccd28f503e108 Mon Sep 17 00:00:00 2001 From: Logxen Date: Wed, 26 Jun 2013 20:06:15 -0700 Subject: [PATCH] SMOOTHIEPANEL LIVES! :D --- src/modules/utils/panel/Panel.cpp | 8 +-- .../utils/panel/panels/Smoothiepanel.cpp | 60 ++++++++++--------- .../utils/panel/panels/Smoothiepanel.h | 4 ++ .../panel/panels/smoothiepanel/I2CBang.h | 2 +- .../panel/panels/smoothiepanel/LCDBang.h | 4 +- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/modules/utils/panel/Panel.cpp b/src/modules/utils/panel/Panel.cpp index 36ef0029..aa6ffccc 100644 --- a/src/modules/utils/panel/Panel.cpp +++ b/src/modules/utils/panel/Panel.cpp @@ -84,8 +84,8 @@ void Panel::on_module_loaded(){ this->click_button.down_attach( this, &Panel::on_click_release ); this->back_button.up_attach( this, &Panel::on_back ); -// this->kernel->slow_ticker->attach( 100, this, &Panel::button_tick ); -// this->kernel->slow_ticker->attach( 1000, this, &Panel::encoder_check ); + this->kernel->slow_ticker->attach( 100, this, &Panel::button_tick ); + this->kernel->slow_ticker->attach( 1000, this, &Panel::encoder_check ); // Default top screen this->top_screen = new MainMenuScreen(); @@ -127,7 +127,7 @@ uint32_t Panel::encoder_check(uint32_t dummy){ int change = lcd->readEncoderDelta(); encoder_counter += change; // TODO divisor needs to be configurable - if( change != 0 /*&& encoder_counter % 2 == 0*/ ){ + if( change != 0 && encoder_counter % this->encoder_click_resolution == 0 ){ this->counter_changed = true; (*this->counter) += change; this->idle_time= 0; @@ -177,7 +177,7 @@ void Panel::on_idle(void* argument){ this->click_button.check_signal(but&BUTTON_SELECT); // FIXME test - //if(but&BUTTON_AUX1) lcd->buzz(10, 500); +// if(but&BUTTON_AUX1) lcd->buzz(10, 500); } // If we are in menu mode and the position has changed diff --git a/src/modules/utils/panel/panels/Smoothiepanel.cpp b/src/modules/utils/panel/panels/Smoothiepanel.cpp index 26eb37f7..3ac166f4 100644 --- a/src/modules/utils/panel/panels/Smoothiepanel.cpp +++ b/src/modules/utils/panel/panels/Smoothiepanel.cpp @@ -38,6 +38,8 @@ Smoothiepanel::Smoothiepanel() { this->lcd_contrast = THEKERNEL->config->value(panel_checksum, lcd_contrast_checksum)->by_default(0)->as_number(); // this->interrupt_pin.from_string(THEKERNEL->config->value(panel_checksum, i2c_interrupt_pin_checksum)->by_default("nc")->as_string())->as_input(); + this->encoder_a_pin.from_string(THEKERNEL->config->value( panel_checksum, encoder_a_pin_checksum)->by_default("nc")->as_string())->as_input(); + this->encoder_b_pin.from_string(THEKERNEL->config->value( panel_checksum, encoder_b_pin_checksum)->by_default("nc")->as_string())->as_input(); paused= false; } @@ -93,25 +95,32 @@ void ledbang_init(I2C i2c, int address){ void Smoothiepanel::init(){ // init lcd and buzzer lcdbang_init(*this->i2c); - lcdbang_print(*this->i2c, " Smoothiepanel Beta - design by Logxen -"); +// lcdbang_print(*this->i2c, " Smoothiepanel Beta - design by Logxen -"); lcdbang_contrast(*this->i2c, this->lcd_contrast); ledbang_init(*this->i2c, this->i2c_address); - wait_us(1000); - this->clear(); +// wait_us(3000); +// this->clear(); } // cycle the buzzer pin at a certain frequency (hz) for a certain duration (ms) void Smoothiepanel::buzz(long duration, uint16_t freq) { const int expander = PCA9505_ADDRESS | this->i2c_address; char cmd[2]; + char saved; + + // save register state + cmd[0] = 0x04; + this->i2c->write(expander, cmd, 1, false); + this->i2c->read(expander, cmd, 1); + saved = cmd[0]; // buzz cmd[0] = 0x0C; - cmd[1] = 0xFE; + cmd[1] = saved & 0xFE; this->i2c->write(expander, cmd, 2); wait_ms(duration); // TODO: Make this not hold up the whole system - cmd[1] = 0xFF; + cmd[1] = saved; this->i2c->write(expander, cmd, 2); } @@ -123,48 +132,41 @@ uint8_t Smoothiepanel::readButtons(void) { cmd[0] = 0x03; this->i2c->write(expander, cmd, 1, false); this->i2c->read(expander, cmd, 1); - if(cmd[0] & 0x10) button_bits |= BUTTON_SELECT; - if(cmd[0] & 0x02) button_bits |= BUTTON_AUX1; // back button - if(cmd[0] & 0x01) button_bits |= BUTTON_AUX2; // play button + //cmd[0] = ~cmd[0]; + if((cmd[0] & 0x10) > 0) button_bits |= BUTTON_SELECT; + if((cmd[0] & 0x02) > 0) button_bits |= BUTTON_LEFT; // back button +// if((cmd[0] & 0x01) > 0) button_bits |= BUTTON_AUX1; // play button // check the button pause // button_pause.check_signal(); +// this->setCursor(6, 4); +// this->printf("Buttons: 0x%02X", button_bits); return button_bits; } int Smoothiepanel::readEncoderDelta() { - const int expander = PCA9505_ADDRESS | this->i2c_address; - char cmd[1]; - bool enc_a, enc_b; - - cmd[0] = 0x03; - this->i2c->write(expander, cmd, 1, false); - this->i2c->read(expander, cmd, 1); - enc_a = cmd[0] & 0x04; - enc_b = cmd[0] & 0x08; - - static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; - static uint8_t old_AB = 0; - old_AB <<= 2; //remember previous state - old_AB |= ( enc_a + ( enc_b * 2 ) ); //add current state - return enc_states[(old_AB&0x0f)]; + static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; + static uint8_t old_AB = 0; + old_AB <<= 2; //remember previous state + old_AB |= ( this->encoder_a_pin.get() + ( this->encoder_b_pin.get() * 2 ) ); //add current state + return enc_states[(old_AB&0x0f)]; } void Smoothiepanel::clear() { command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero -#ifndef USE_FASTMODE - wait_us(2000); // this command takes a long time! -#endif +//#ifndef USE_FASTMODE +// wait_ms(50); // this command takes a long time! +//#endif } void Smoothiepanel::home() { command(LCD_RETURNHOME); // set cursor position to zero -#ifndef USE_FASTMODE - wait_us(2000); // this command takes a long time! -#endif +//#ifndef USE_FASTMODE +// wait_us(2000); // this command takes a long time! +//#endif } void Smoothiepanel::setCursor(uint8_t col, uint8_t row) diff --git a/src/modules/utils/panel/panels/Smoothiepanel.h b/src/modules/utils/panel/panels/Smoothiepanel.h index 6601e8f2..06b6e41d 100644 --- a/src/modules/utils/panel/panels/Smoothiepanel.h +++ b/src/modules/utils/panel/panels/Smoothiepanel.h @@ -39,6 +39,8 @@ using namespace std; #define i2c_address_checksum CHECKSUM("i2c_address") #define i2c_frequency_checksum CHECKSUM("i2c_frequency") #define i2c_interrupt_pin_checksum CHECKSUM("i2c_interrupt_pin") +#define encoder_a_pin_checksum CHECKSUM("encoder_a_pin") +#define encoder_b_pin_checksum CHECKSUM("encoder_b_pin") #define lcd_contrast_checksum CHECKSUM("lcd_contrast") @@ -95,6 +97,8 @@ class Smoothiepanel : public LcdBase { mbed::I2C* i2c; Pin interrupt_pin; + Pin encoder_a_pin; + Pin encoder_b_pin; // Button button_pause; // uint32_t on_pause_release(uint32_t dummy); bool paused; diff --git a/src/modules/utils/panel/panels/smoothiepanel/I2CBang.h b/src/modules/utils/panel/panels/smoothiepanel/I2CBang.h index 0b21906e..db54ae51 100644 --- a/src/modules/utils/panel/panels/smoothiepanel/I2CBang.h +++ b/src/modules/utils/panel/panels/smoothiepanel/I2CBang.h @@ -15,7 +15,7 @@ void i2cbang_init(I2C i2c){ cmd[1] = 0x3F; i2c.write(addr, cmd, 2); cmd[0] = 0x0B; - cmd[1] = 0xFF; + cmd[1] = 0xC0; i2c.write(addr, cmd, 2); wait_ms(1); } diff --git a/src/modules/utils/panel/panels/smoothiepanel/LCDBang.h b/src/modules/utils/panel/panels/smoothiepanel/LCDBang.h index ebc68fa5..250c126d 100644 --- a/src/modules/utils/panel/panels/smoothiepanel/LCDBang.h +++ b/src/modules/utils/panel/panels/smoothiepanel/LCDBang.h @@ -12,8 +12,8 @@ void lcdbang_writenibble(I2C i2c, char c, bool command = false){ const int addr = 0x40; char cmd[2]; - c &= 0x0F; c <<= 4; + c &= 0xF0; c |= 0x01; if(!command) c |= 0x02; @@ -24,7 +24,7 @@ void lcdbang_writenibble(I2C i2c, char c, bool command = false){ i2c.write(addr, cmd, 2); cmd[1] = c; i2c.write(addr, cmd, 2); - wait_ms(1); +// wait_ms(1); } void lcdbang_write(I2C i2c, char c, bool command = false){ -- 2.20.1