Skip to content

Commit

Permalink
Merge pull request #5 from FreeSK8/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
r3n33 authored Sep 28, 2021
2 parents c4ae15e + ad1357d commit 8373540
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 111 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
v0.2.0

* Added Left Handed User Setting
* Added "Custom" Model for developers
* Added Display Frame Buffer
* Updated Boot Images
* Remove fuzz from Display on boot
* Filter accelerometer readings
* Increase is_display_visible sensitivity

v0.1.2

* Reset idle throttle timer while charging
Expand Down
Binary file added filesystem/freesk8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added filesystem/gear.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added filesystem/pear.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 78 additions & 59 deletions main/display.c

Large diffs are not rendered by default.

69 changes: 42 additions & 27 deletions main/lib/st7789/st7789.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static const int SPI_Data_Mode = 1;
static const int SPI_Frequency = SPI_MASTER_FREQ_40M;
//static const int SPI_Frequency = SPI_MASTER_FREQ_80M;

static uint16_t _frame_buffer[240*240] = {BLACK};

void spi_master_init(TFT_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t GPIO_CS, int16_t GPIO_DC, int16_t GPIO_RESET, int16_t GPIO_BL)
{
Expand Down Expand Up @@ -246,6 +247,9 @@ void lcdInit(TFT_t * dev, int width, int height, int offsetx, int offsety)
spi_master_write_command(dev, 0x13); //Normal Display Mode On
delayMS(10);

lcdFillScreen(dev, BLACK); //Write full screen of black
lcdUpdate(dev);

spi_master_write_command(dev, 0x29); //Display ON
delayMS(255);

Expand All @@ -266,12 +270,7 @@ void lcdDrawPixel(TFT_t * dev, uint16_t x, uint16_t y, uint16_t color){
uint16_t _x = x + dev->_offsetx;
uint16_t _y = y + dev->_offsety;

spi_master_write_command(dev, 0x2A); // set column(x) address
spi_master_write_addr(dev, _x, _x);
spi_master_write_command(dev, 0x2B); // set Page(y) address
spi_master_write_addr(dev, _y, _y);
spi_master_write_command(dev, 0x2C); // Memory Write
spi_master_write_data_word(dev, color);
_frame_buffer[_x + (_y * 240)] = color; // Update frame buffer at pixel location
}


Expand All @@ -285,16 +284,12 @@ void lcdDrawMultiPixels(TFT_t * dev, uint16_t x, uint16_t y, uint16_t size, uint
if (y >= dev->_height) return;

uint16_t _x1 = x + dev->_offsetx;
uint16_t _x2 = _x1 + size;
uint16_t _y1 = y + dev->_offsety;
uint16_t _y2 = _y1;

spi_master_write_command(dev, 0x2A); // set column(x) address
spi_master_write_addr(dev, _x1, _x2);
spi_master_write_command(dev, 0x2B); // set Page(y) address
spi_master_write_addr(dev, _y1, _y2);
spi_master_write_command(dev, 0x2C); // Memory Write
spi_master_write_colors(dev, colors, size);
//TODO: Fill memory faster
for (int i=0; i<size; ++i) {
_frame_buffer[_x1 + (_y1 * 240) + i] = colors[i];
}
}

// Draw rectangle of filling
Expand All @@ -314,21 +309,15 @@ void lcdDrawFillRect(TFT_t * dev, uint16_t x1, uint16_t y1, uint16_t x2, uint16_
uint16_t _x2 = x2 + dev->_offsetx;
uint16_t _y1 = y1 + dev->_offsety;
uint16_t _y2 = y2 + dev->_offsety;
uint16_t size = _y2 -_y1 + 1;

spi_master_write_command(dev, 0x2A); // set column(x) address
spi_master_write_addr(dev, _x1, _x2);
spi_master_write_command(dev, 0x2B); // set Page(y) address
spi_master_write_addr(dev, _y1, _y2);
spi_master_write_command(dev, 0x2C); // Memory Write
for(int i=_x1;i<=_x2;i++){
uint16_t size = _y2-_y1+1;
spi_master_write_color(dev, color, size);
#if 0
for(j=y1;j<=y2;j++){
//ESP_LOGD(TAG,"i=%d j=%d",i,j);
spi_master_write_data_word(dev, color);
//TODO: Fill memory faster
for (int j=_y1; j<_y2+1; ++j) {
for (int k=0; k<size; ++k) {
_frame_buffer[i + (j * 240) + k] = color;
}
}
#endif
}
}

Expand All @@ -345,7 +334,10 @@ void lcdDisplayOn(TFT_t * dev) {
// Fill screen
// color:color
void lcdFillScreen(TFT_t * dev, uint16_t color) {
lcdDrawFillRect(dev, 0, 0, dev->_width-1, dev->_height-1, color);
//TODO: Fill memory faster
for (int i=0; i<(240*240); ++i) {
_frame_buffer[i] = color;
}
}

// Draw line
Expand Down Expand Up @@ -1565,3 +1557,26 @@ void lcdFillArc2(TFT_t * dev, int x, int y, int start_angle, int seg_count, int
y1 = y3;
}
}

void line_test(TFT_t * dev, uint16_t x, uint16_t y, uint16_t size, uint16_t * colors) {
if (x+size > dev->_width) return;
if (y >= dev->_height) return;

uint16_t _x1 = x + dev->_offsetx;
uint16_t _x2 = _x1 + size;
uint16_t _y1 = y + dev->_offsety;
uint16_t _y2 = _y1;

spi_master_write_command(dev, 0x2A); // set column(x) address
spi_master_write_addr(dev, _x1, _x2);
spi_master_write_command(dev, 0x2B); // set Page(y) address
spi_master_write_addr(dev, _y1, _y2);
spi_master_write_command(dev, 0x2C); // Memory Write
spi_master_write_colors(dev, colors, size);
}

void lcdUpdate(TFT_t * dev) {
for (int i=0; i<240; ++i) {
line_test(dev, 0, i, 240, &_frame_buffer[i*240]);
}
}
2 changes: 2 additions & 0 deletions main/lib/st7789/st7789.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,7 @@ void lcdFillArc3(TFT_t * dev, int x, int y, int start_angle, int seg_count, int
int lcdDrawString2(TFT_t * dev, uint8_t sizeht , uint8_t sizew, uint16_t x, uint16_t y, uint8_t * ascii, uint16_t color);
int lcdDrawString3(TFT_t * dev, uint8_t sizeht , uint8_t sizew, uint16_t x, uint16_t y, uint8_t * ascii, uint16_t color);

void lcdUpdate(TFT_t * dev);

#endif /* MAIN_ST7789_H_ */

83 changes: 59 additions & 24 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

#include "lib/haptic/haptic.h"

const char * version = "0.1.2";
const char * version = "0.2.0";

#define ADC_BATTERY_MIN 525

int gyro_x, gyro_y, gyro_z;
float accel_g_x, accel_g_x_delta;
float accel_g_y, accel_g_y_delta;
float accel_g_z, accel_g_z_delta;
float accel_g_x;
float accel_g_y;
float accel_g_z;

bool is_remote_idle = true; //NOTE: Controlled by IMU
bool display_second_screen = false;
Expand Down Expand Up @@ -247,7 +247,10 @@ static void gpio_input_task(void* arg)
my_user_settings.throttle_reverse = !my_user_settings.throttle_reverse;
break;
case SETTING_MODEL:
if (++my_user_settings.remote_model > MODEL_CLINT) my_user_settings.remote_model = MODEL_ALBERT;
if (++my_user_settings.remote_model > MODEL_CUSTOM) my_user_settings.remote_model = MODEL_ALBERT;
break;
case SETTING_LEFTY:
my_user_settings.left_handed = !my_user_settings.left_handed;
break;
}
save_user_settings(&my_user_settings);
Expand Down Expand Up @@ -443,17 +446,19 @@ static void i2c_task(void *arg)
float accel_gy = (float) accel.accel_y * accel_res - accel_bias[1];
float accel_gz = (float) accel.accel_z * accel_res - accel_bias[2];

accel_g_x_delta = accel_g_x - accel_gx;
accel_g_y_delta = accel_g_y - accel_gy;
accel_g_z_delta = accel_g_z - accel_gz;
accel_g_x = (0.2 * accel_gx) + (0.8 * accel_g_x);
accel_g_y = (0.2 * accel_gy) + (0.8 * accel_g_y);
accel_g_z = (0.2 * accel_gz) + (0.8 * accel_g_z);
// Filter accelerometer
double acceleration = sqrt(pow(accel_gx, 2) + pow(accel_gy, 2) + pow(accel_gz, 2));
if (acceleration > 0.9 && acceleration < 1.15) {
// Smooth accelerometer readings
accel_g_x = (0.4 * accel_gx) + (0.6 * accel_g_x);
accel_g_y = (0.4 * accel_gy) + (0.6 * accel_g_y);
accel_g_z = (0.4 * accel_gz) + (0.6 * accel_g_z);
}

gyro_x = gyro.gyro_x;
gyro_y = gyro.gyro_y;
gyro_z = gyro.gyro_z;
//ESP_LOGI(__FUNCTION__, "ADC joy1:%d joy2:%d batt:%d rssi:%d IMU x:%f y:%f z:%f", adc_raw_joystick, adc_raw_joystick_2, adc_raw_battery_level, adc_raw_rssi, accel_g_x, accel_g_y, accel_g_z);
//ESP_LOGI(__FUNCTION__, "ADC joy1:%d joy2:%d batt:%d rssi:%d IMU x:%.3f y:%.3f z:%.3f t:%.3f (%d)", adc_raw_joystick, adc_raw_joystick_2, adc_raw_battery_level, adc_raw_rssi, accel_g_x, accel_g_y, accel_g_z, acceleration, (acceleration > 0.9 && acceleration < 1.15));
//ESP_LOGI(__FUNCTION__, "IMU x:%d y:%d z:%d", gyro_x, gyro_y, gyro_z);

if (accel_g_x == 0 && accel_g_y == 0 && accel_g_z == 0) {
Expand Down Expand Up @@ -883,7 +888,19 @@ void ST7789_Task(void *pvParameters)

TFT_t dev;
spi_master_init(&dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO, CONFIG_BL_GPIO);
lcdInit(&dev, CONFIG_WIDTH, CONFIG_HEIGHT, CONFIG_OFFSETX, CONFIG_OFFSETY);
int8_t x_offset = 0;
switch (my_user_settings.remote_model) {
case MODEL_ALBERT:
x_offset = -5;
break;
case MODEL_BRUCE:
x_offset = -2;
break;
case MODEL_CUSTOM:
x_offset = 0;
break;
}
lcdInit(&dev, CONFIG_WIDTH, CONFIG_HEIGHT, x_offset, CONFIG_OFFSETY);

#if CONFIG_INVERSION
ESP_LOGI(TAG, "Enable Display Inversion");
Expand All @@ -892,14 +909,17 @@ void ST7789_Task(void *pvParameters)

// FreeSK8 Logo
lcdFillScreen(&dev, BLACK);
drawFirmwareVersion(&dev, (char *)version);
if (remote_in_pairing_mode) {
drawJPEG(&dev, (char*)"/spiffs/logo_badge_pairing.jpg", 206, 179, 17, 63);
drawJPEG(&dev, (char*)"/spiffs/pear.jpg", 92, 110, 74, 65);
} else if (remote_in_setup_mode) {
drawJPEG(&dev, (char*)"/spiffs/gear.jpg", 110, 110, 65, 65);
} else {
drawJPEG(&dev, (char*)"/spiffs/logo_badge.jpg", 206, 114, 17, 63);
drawJPEG(&dev, (char*)"/spiffs/freesk8.jpg", 114, 82, 63, 79);
}
drawFirmwareVersion(&dev, (char *)version);
lcdUpdate(&dev);

vTaskDelay(1000/portTICK_PERIOD_MS);
vTaskDelay(2000/portTICK_PERIOD_MS);
lcdFillScreen(&dev, BLACK);

bool is_display_visible = true;
Expand All @@ -910,7 +930,7 @@ void ST7789_Task(void *pvParameters)
if (remote_in_setup_mode)
{
// Move settings selection up and down
if (joystick_value_mapped < 55 && user_settings_index < SETTING_MODEL) ++user_settings_index;
if (joystick_value_mapped < 55 && user_settings_index < SETTING_LEFTY) ++user_settings_index;
if (joystick_value_mapped > 200 && user_settings_index > SETTING_PIEZO) --user_settings_index;

lcdBacklightOn(&dev);
Expand Down Expand Up @@ -965,9 +985,10 @@ void ST7789_Task(void *pvParameters)
// Check if remote is in a visible orientation
switch (my_user_settings.remote_model)
{
//TODO: left vs right handed values: my_user_settings.throttle_reverse ?
case MODEL_ALBERT:
if (accel_g_x > 0.7 || accel_g_z > 0.4) {
if ( (!my_user_settings.left_handed && (accel_g_x > 0.7 || accel_g_z > 0.4)) ||
(my_user_settings.left_handed && (accel_g_x < -0.7 || accel_g_z > 0.4))
) {
if ((xTaskGetTickCount() - remote_is_visible_tick)*portTICK_RATE_MS > 500) {
is_display_visible = false;
}
Expand All @@ -978,8 +999,9 @@ void ST7789_Task(void *pvParameters)
}
break;
case MODEL_BRUCE:
//NOTE: Left and Right handed is the same
if (accel_g_y > 0.5 || accel_g_z > 0.3) {
if ((xTaskGetTickCount() - remote_is_visible_tick)*portTICK_RATE_MS > 500) {
if ((xTaskGetTickCount() - remote_is_visible_tick)*portTICK_RATE_MS > 250) {
is_display_visible = false;
}
}
Expand All @@ -989,9 +1011,22 @@ void ST7789_Task(void *pvParameters)
}
break;
case MODEL_CLINT:
//TODO: estimated
//TODO: estimated values. Left handed not coded
if (accel_g_y > 0.5 || accel_g_x > 0.6) {
if ((xTaskGetTickCount() - remote_is_visible_tick)*portTICK_RATE_MS > 500) {
if ((xTaskGetTickCount() - remote_is_visible_tick)*portTICK_RATE_MS > 250) {
is_display_visible = false;
}
}
else {
remote_is_visible_tick = xTaskGetTickCount();
is_display_visible = true;
}
break;
case MODEL_CUSTOM:
//NOTE: Insert custom logic to determine if display is visible
// my_user_settings.left_handed
if (accel_g_z > -0.8) {
if ((xTaskGetTickCount() - remote_is_visible_tick)*portTICK_RATE_MS > 250) {
is_display_visible = false;
}
}
Expand Down Expand Up @@ -1128,7 +1163,7 @@ void app_main(void)
while (ret != ESP_OK)
{
my_user_settings.settings_version = SETTINGS_VERSION;
my_user_settings.remote_model = MODEL_BRUCE;
my_user_settings.remote_model = MODEL_CUSTOM;
save_user_settings(&my_user_settings);
ret = load_user_settings(&my_user_settings);
}
Expand Down
5 changes: 4 additions & 1 deletion main/user-settings.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef USERSETTINGS_H_
#define USERSETTINGS_H_

#define SETTINGS_VERSION 1
#define SETTINGS_VERSION 2

typedef enum {
SETTING_PIEZO = 0,
Expand All @@ -10,13 +10,15 @@ typedef enum {
SETTING_TEMP,
SETTING_THROTTLE,
SETTING_MODEL,
SETTING_LEFTY,
} SETTINGS_INDEX;

typedef enum {
MODEL_UNDEFINED = 0,
MODEL_ALBERT,
MODEL_BRUCE,
MODEL_CLINT,
MODEL_CUSTOM,
} REMOTE_MODELS;

typedef struct {
Expand All @@ -27,6 +29,7 @@ typedef struct {
bool dispaly_fahrenheit;
bool throttle_reverse;
uint8_t remote_model; //1=Albert,2=Bruce,3=Clint
bool left_handed;
} user_settings_t;

esp_err_t save_user_settings(user_settings_t * my_user_settings);
Expand Down

0 comments on commit 8373540

Please sign in to comment.