diff --git a/compjade.sh b/compjade.sh new file mode 100755 index 00000000..312d8764 --- /dev/null +++ b/compjade.sh @@ -0,0 +1,100 @@ +#!/bin/bash +set -o errexit +set -o nounset +set -o pipefail + +cleanup() { + rm -rf -- "${temp_directory}" + if [ -n "${initial_tty_device_permissions:-}" ] && + [ "$(stat -c '%a' "${tty_device}")" != "${initial_tty_device_permissions}" ]; then + sudo chmod "${initial_tty_device_permissions}" "${tty_device}" + fi +} + +working_directory="${HOME}/Downloads/diy_jade" +temp_directory="${working_directory}/temp" +trap cleanup EXIT + +jade_git_tag="master" +jade_save_directory="${working_directory}/jade" +jade_repo_url="https://github.com/danielboro/jade.git" + +esp_idf_git_tag="v5.4.2" +esp_idf_temp_directory="${temp_directory}/esp-idf" +esp_idf_save_directory="${working_directory}/esp-idf" +esp_idf_repo_url="https://github.com/espressif/esp-idf.git" + +chosen_device="TTGO T-Display" +tty_device="/dev/ttyACM0" + +clear +echo "------------------------------------------------------------" +echo "------------------------------------------------------------" +echo "--- ---" +echo "--- Do-It-Yourself Jade Install Script ---" +echo "--- Written by Epic Curious ---" +echo "--- ---" +echo "------------------------------------------------------------" +echo "------------------------------------------------------------" +echo + +if [ "$(whoami)" = "root" ]; then + echo -e "ALERT: You're running the script as root/superuser.\nYou may notice PIP 'sudo -H' warnings.\n" +fi + +echo "LINUX ONLY. Flashing the ${chosen_device}..." + +while read -r dependency; do + if ! command -v "${dependency}" &> /dev/null; then + echo -en "\n\nERROR:\n${dependency} was not found on your system.\nPlease install ${dependency} by running:\n\n" + if [ "${dependency}" == "pip" ] || [ "${dependency}" == "virtualenv" ]; then + echo -en "sudo apt update && sudo apt install -y python3-${dependency}\n\n" + else + echo -en "sudo apt update && sudo apt install -y ${dependency}\n\n" + fi + exit 1 + fi +done < <(curl -fsSL https://github.com/epiccurious/jade-diy/raw/master/depends.txt) + +if [ ! -f "${esp_idf_save_directory}"/export.sh ]; then + git clone --branch "${esp_idf_git_tag}" --single-branch --depth 1 "${esp_idf_repo_url}" "${esp_idf_temp_directory}" + cd "${esp_idf_temp_directory}"/ + git submodule update --depth 1 --init --recursive + ./install.sh esp32 &> /dev/null + source ./export.sh 1> /dev/null + mv "${esp_idf_temp_directory}" "${esp_idf_save_directory}" +fi +cd "${esp_idf_save_directory}"/ +./install.sh esp32 +source ./export.sh + +if [ ! -d "${jade_save_directory}" ]; then + git clone --branch "${jade_git_tag}" --single-branch --depth 1 "${jade_repo_url}" "${jade_save_directory}" + cd "${jade_save_directory}" + git submodule update --depth 1 --init --recursive &> /dev/null +fi +cd "${jade_save_directory}" +jade_version="$(git describe --tags)" + +cp configs/sdkconfig_display_ttgo_tdisplay.defaults sdkconfig.defaults +sed -i.bak '/CONFIG_DEBUG_MODE/d' ./sdkconfig.defaults +sed -i.bak '1s/^/CONFIG_LOG_DEFAULT_LEVEL_NONE=y\n/' sdkconfig.defaults +rm sdkconfig.defaults.bak + +idf.py build + +[ "${CI:-false}" = true ] && echo "Exiting the script for CI runners." && exit 0 + +while [ ! -c "${tty_device}" ]; do + read -srn1 -p "Connect your ${chosen_device} and PRESS ANY KEY to continue... " && echo +done +initial_tty_device_permissions="$(stat -c '%a' "${tty_device}")" +if [ "${initial_tty_device_permissions:2}" -lt 6 ]; then + echo -e "\nElevating write permissions for ${chosen_device}" + sudo chmod o+rw "${tty_device}" + echo +fi + +idf.py flash + +echo -e "\nSUCCESS! Jade ${jade_version} is now installed on your ${chosen_device}.\nYou can close this window.\n" \ No newline at end of file diff --git a/dependencies.lock.esp32 b/dependencies.lock.esp32 index 4136b33d..3f9f6ba6 100644 --- a/dependencies.lock.esp32 +++ b/dependencies.lock.esp32 @@ -101,7 +101,7 @@ dependencies: idf: source: type: idf - version: 5.4.0 + version: 5.4.2 direct_dependencies: - espressif/button - espressif/cbor diff --git a/flastdip.sh b/flastdip.sh new file mode 100755 index 00000000..1976b7ca --- /dev/null +++ b/flastdip.sh @@ -0,0 +1,32 @@ +#!/bin/bash +working_directory="${HOME}/Downloads/diy_jade" +temp_directory="${working_directory}/temp" +trap cleanup EXIT + +jade_git_tag="master" +jade_save_directory="${working_directory}/jade" +jade_repo_url="https://github.com/danielboro/jade.git" + +esp_idf_git_tag="v5.4.2" +esp_idf_temp_directory="${temp_directory}/esp-idf" +esp_idf_save_directory="${working_directory}/esp-idf" +esp_idf_repo_url="https://github.com/espressif/esp-idf.git" + +chosen_device="TTGO T-Display" +tty_device="/dev/ttyACM0" + +[ "${CI:-false}" = true ] && echo "Exiting the script for CI runners." && exit 0 + +while [ ! -c "${tty_device}" ]; do + read -srn1 -p "Connect your ${chosen_device} and PRESS ANY KEY to continue... " && echo +done +initial_tty_device_permissions="$(stat -c '%a' "${tty_device}")" +if [ "${initial_tty_device_permissions:2}" -lt 6 ]; then + echo -e "\nElevating write permissions for ${chosen_device}" + sudo chmod o+rw "${tty_device}" + echo +fi + +idf.py flash + +echo -e "\nSUCCESS! Jade ${jade_version} is now installed on your ${chosen_device}.\nYou can close this window.\n" \ No newline at end of file diff --git a/main/power.c b/main/power.c index 95474b63..52be7b4f 100644 --- a/main/power.c +++ b/main/power.c @@ -30,6 +30,9 @@ #elif defined(CONFIG_BOARD_TYPE_TTGO_TDISPLAYS3) && defined(CONFIG_HAS_BATTERY) // ttgo-tdisplays3 can read battery level and charging status if a battery is connected #include "power/tdisplays3.inc" +#elif defined(CONFIG_BOARD_TYPE_TTGO_TDISPLAY) +// ttgo-tdisplays3 can read battery level and charging status if a battery is connected +#include "power/tdisplay.inc" #else // Stubs for other hw boards (ie. no power management) #include "power/minimal.inc" diff --git a/main/power/tdisplay.inc b/main/power/tdisplay.inc new file mode 100644 index 00000000..3d1e2bdb --- /dev/null +++ b/main/power/tdisplay.inc @@ -0,0 +1,98 @@ +// tdisplay and devices with no power-management but it can measure voltage level of the battery and +// detect charging status when a battery is connected through an analog input +// +#include +#include +#include + +#define BATTERY_ADC_CHANNEL ADC_CHANNEL_6 + +static adc_oneshot_unit_handle_t adc1_handle = NULL; +static adc_cali_handle_t adc1_cali_handle = NULL; + +esp_err_t power_init(void) +{ + // Initialise the ADC to measure battery level + adc_oneshot_unit_init_cfg_t init_config1 = { + .unit_id = ADC_UNIT_1, + }; + ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle)); + JADE_ASSERT(adc1_handle); + // ADC Config + adc_oneshot_chan_cfg_t config = { + .atten = ADC_ATTEN_DB_12, + .bitwidth = ADC_BITWIDTH_DEFAULT, + }; + ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, BATTERY_ADC_CHANNEL, &config)); + // Curve fitting calibration + adc_cali_line_fitting_config_t cali_config = { + .unit_id = ADC_UNIT_1, + .atten = ADC_ATTEN_DB_12, + .bitwidth = ADC_BITWIDTH_DEFAULT, + }; +// ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_handle)); + return ESP_OK; +} + +esp_err_t power_shutdown(void) { return ESP_OK; } +esp_err_t power_screen_on(void) { return ESP_OK; } +esp_err_t power_backlight_on(const uint8_t brightness) { return ESP_OK; } +esp_err_t power_backlight_off(void) { return ESP_OK; } +esp_err_t power_camera_on(void) { return ESP_OK; } +esp_err_t power_camera_off(void) { return ESP_OK; } + +uint16_t power_get_vbat(void) +{ + JADE_ASSERT(adc1_handle); + int cal_vbat = 0; + int raw_vbat = 0; + ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC_CHANNEL, &raw_vbat)); +// ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, raw_vbat, &cal_vbat)); + +// uint16_t v = analogRead(BATTERY_ADC_CHANNEL); + cal_vbat =((float)raw_vbat / 4095.0) * 2.0 * 3.3 * (1100 / 1.0); + return (uint16_t)(cal_vbat); +} +uint8_t power_get_battery_status(void) +{ + const uint16_t vbat = power_get_vbat(); + + if (vbat > 4000) { + return 5; + } else if (vbat > 3800) { + return 4; + } else if (vbat > 3600) { + return 3; + } else if (vbat > 3400) { + return 2; + } else if (vbat > 3200) { + return 1; + } + return 0; +} + +bool power_get_battery_charging(void) +{ + uint16_t vbat = power_get_vbat(); + // If the voltage is greater than 4500 mV and less than 4750 it means its charging + if (vbat > 4500 && vbat < 4750) { + return true; + } + return false; +} + +uint16_t power_get_ibat_charge(void) { return 0; } +uint16_t power_get_ibat_discharge(void) { return 0; } +uint16_t power_get_vusb(void) { return 0; } +uint16_t power_get_iusb(void) { return 0; } +uint16_t power_get_temp(void) { return 0; } + +bool usb_connected(void) +{ + // If the voltage is greater than 4500 mV it means USB is connected + uint16_t vbat = power_get_vbat(); + if (vbat > 4500) { + return true; + } + return false; +}