Skip to content

Commit

Permalink
add M5StickC-Plus2 port
Browse files Browse the repository at this point in the history
pin toolchain to a known working ver
add board definition based on v2.x esp-arduino
use macro to define plus2 pinouts
use macro to impl dummy axp for plus2
fix confliting ir/led pin, active val
  • Loading branch information
tao-j committed Nov 23, 2024
1 parent fd1f298 commit 16f6c8a
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 22 deletions.
15 changes: 13 additions & 2 deletions arduino_workspace/AlphaFairy/AlphaFairy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,22 @@ FairyEncoder fencoder;
FairySubmenu main_menu(NULL, 0);

void setup()
{
{

Serial.begin(SERIAL_PORT_BAUDRATE);

#ifdef ARDUINO_M5Stick_C_Plus2
// enable power HOLD
pinMode(POWER_HOLD_PIN, OUTPUT);
digitalWrite(POWER_HOLD_PIN, HIGH);
// enable LCD BL
ledcSetup(LCD_PWM_CHAN, LCD_BL_HZ, LCD_BL_RESOLUTION_BITS);
ledcAttachPin(LCD_BL_PIN, LCD_PWM_CHAN);
ledcWrite(LCD_PWM_CHAN, 1 << LCD_BL_RESOLUTION_BITS - 1);
#endif
dbg_ser.enabled = true;

Wire1.begin(21, 22);
Wire1.begin(GPIO_NUM_21, GPIO_NUM_22);
Wire1.setClock(400000);

cpufreq_init();
Expand Down
16 changes: 10 additions & 6 deletions arduino_workspace/AlphaFairy/TallyLite.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "AlphaFairy.h"

#ifndef ARDUINO_M5Stick_C_Plus2
#define LED_PIN GPIO_NUM_10
#else
#define LED_PIN GPIO_NUM_19
#endif
extern bool redraw_flag;
bool tallylite_nopoll = false; // used to prevent recursion in app_poll()
bool tallylite_enable = true;
Expand Down Expand Up @@ -66,17 +70,17 @@ bool tallylite_ledIsOn = false;

void tallylite_ledOn()
{
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
tallylite_ledIsOn = true;
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
tallylite_ledIsOn = true;
}

void tallylite_ledOff()
{
if (tallylite_ledIsOn)
{
pinMode(10, INPUT);
digitalWrite(10, HIGH);
pinMode(LED_PIN, INPUT);
digitalWrite(LED_PIN, HIGH);
}
tallylite_ledIsOn = false;
}
6 changes: 3 additions & 3 deletions arduino_workspace/AlphaFairy/alfy_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

//#define DISABLE_STATUS_BAR
//#define DISABLE_POWER_SAVE
#define DISABLE_CMD_LINE
#define DISABLE_ALL_MSG
// #define DISABLE_CMD_LINE
// #define DISABLE_ALL_MSG
#define MUTE_NETMSG_ON_BOOT
//#define HTTP_MOCKBTNS_ENABLE
//#define HTTP_ON_BOOT
Expand All @@ -42,7 +42,7 @@

#define BTN_DEBOUNCE 50

#define SERIAL_PORT_BAUDRATE 500000
#define SERIAL_PORT_BAUDRATE 115200

//#define ENABLE_BUILD_LEPTON
//#define ENABLE_BUILD_LEPTON_TRIGGER_SIMPLE
Expand Down
243 changes: 243 additions & 0 deletions arduino_workspace/libraries/M5StickC-Plus/src/AXP192.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "AXP192.h"
#include "utility/Config.h"

AXP192::AXP192() {
}

#ifndef ARDUINO_M5Stick_C_Plus2
void AXP192::begin(void) {
Wire1.begin(21, 22);
Wire1.setClock(400000);
Expand Down Expand Up @@ -426,3 +428,244 @@ void AXP192::SetLDO2(bool State) {
void AXP192::PowerOff() {
Write1Byte(0x32, Read8bit(0x32) | 0x80); // MSB for Power Off
}
#else
void AXP192::begin(void) { BtnC.read(); }
void AXP192::Write1Byte(uint8_t Addr, uint8_t Data) {}

uint8_t AXP192::Read8bit(uint8_t Addr) {
return 0;
}

uint16_t AXP192::Read12Bit(uint8_t Addr) {
uint16_t Data = 0;
return Data;
}

uint16_t AXP192::Read13Bit(uint8_t Addr) {
uint16_t Data = 0;
return Data;
}

uint16_t AXP192::Read16bit(uint8_t Addr) {
uint16_t ReData = 0;
return ReData;
}

uint32_t AXP192::Read24bit(uint8_t Addr) {
uint32_t ReData = 0;
return ReData;
}

uint32_t AXP192::Read32bit(uint8_t Addr) {
uint32_t ReData = 0;
return ReData;
}

void AXP192::ReadBuff(uint8_t Addr, uint8_t Size, uint8_t *Buff) {}

void AXP192::ScreenBreath(uint8_t brightness) {
if (brightness > 12) {
brightness = 12;
} else if (brightness < 1) {
brightness = 1;
}
ledcWrite(LCD_PWM_CHAN, gamma_lut[brightness]);
}

void AXP192::ScreenSwitch(bool state) {
uint8_t brightness;
if (state == false) {
brightness = 0;
} else if (state == true) {
brightness = LCD_BL_RESOLUTION_BITS;
}
ledcWrite(LCD_PWM_CHAN, gamma_lut[brightness]);
}

bool AXP192::GetBatState() {
// TODO:
return true;
}
//---------coulombcounter_from_here---------
// enable: void EnableCoulombcounter(void);
// disable: void DisableCOulombcounter(void);
// stop: void StopCoulombcounter(void);
// clear: void ClearCoulombcounter(void);
// get charge data: uint32_t GetCoulombchargeData(void);
// get discharge data: uint32_t GetCoulombdischargeData(void);
// get coulomb val affter calculation: float GetCoulombData(void);
//------------------------------------------
void AXP192::EnableCoulombcounter(void) { Write1Byte(0xB8, 0x80); }

void AXP192::DisableCoulombcounter(void) { Write1Byte(0xB8, 0x00); }

void AXP192::StopCoulombcounter(void) { Write1Byte(0xB8, 0xC0); }

void AXP192::ClearCoulombcounter(void) { Write1Byte(0xB8, 0xA0); }

uint32_t AXP192::GetCoulombchargeData(void) { return 0; }

uint32_t AXP192::GetCoulombdischargeData(void) { return 0; }

float AXP192::GetCoulombData(void) {
uint32_t coin = 0;
uint32_t coout = 0;

// coin = GetCoulombchargeData();
// coout = GetCoulombdischargeData();

// c = 65536 * current_LSB * (coin - coout) / 3600 / ADC rate
// Adc rate can be read from 84H ,change this variable if you change the ADC
// reate
float ccc = 65536 * 0.5 * (int32_t)(coin - coout) / 3600.0 / 25.0;

return ccc;
}
//----------coulomb_end_at_here----------

uint16_t AXP192::GetVbatData(void) {
uint16_t vbat = 4;
return vbat;
}

uint16_t AXP192::GetVinData(void) {
uint16_t vin = 5;
return vin;
}

uint16_t AXP192::GetIinData(void) {
return 0;
}

uint16_t AXP192::GetVusbinData(void) { return 5; }

uint16_t AXP192::GetIusbinData(void) {
return 0;
}

uint16_t AXP192::GetIchargeData(void) {
return 0;
}

uint16_t AXP192::GetIdischargeData(void) {
return 0;
}

uint16_t AXP192::GetTempData(void) {
return 0;
}

uint32_t AXP192::GetPowerbatData(void) {
return 0;
}

uint16_t AXP192::GetVapsData(void) {
return 0;
}

void AXP192::SetSleep(void) {

}

uint8_t AXP192::GetWarningLeve(void) {
// TODO:;
uint8_t buf = 1;
return (buf & 0x01);
}

// -- sleep
void AXP192::DeepSleep(uint64_t time_in_us) {
SetSleep();

if (time_in_us > 0) {
esp_sleep_enable_timer_wakeup(time_in_us);
} else {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
(time_in_us == 0) ? esp_deep_sleep_start() : esp_deep_sleep(time_in_us);
}

void AXP192::LightSleep(uint64_t time_in_us) {
SetSleep();

if (time_in_us > 0) {
esp_sleep_enable_timer_wakeup(time_in_us);
} else {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
esp_light_sleep_start();
}

// 0 not press, 0x01 long press, 0x02 press
uint8_t AXP192::GetBtnPress() {
BtnC.read();
if (BtnC.wasPressed()) {
return 0x02;
}
return 0;
}

uint8_t AXP192::GetWarningLevel(void) { return 0; }

float AXP192::GetBatVoltage() {
return 4.2;
}

float AXP192::GetBatCurrent() {
return 0.0;
}

float AXP192::GetVinVoltage() {
return 5.0;
}

float AXP192::GetVinCurrent() {
return 0.0;
}

float AXP192::GetVBusVoltage() {
return 4.2;
}

float AXP192::GetVBusCurrent() {
return 0.0;
}

float AXP192::GetTempInAXP192() {
return 37;
}

float AXP192::GetBatPower() {
float VoltageLSB = 1.1;
float CurrentLCS = 0.5;
uint32_t ReData = 0;
return VoltageLSB * CurrentLCS * ReData / 1000.0;
}

float AXP192::GetBatChargeCurrent() {
float ADCLSB = 0.5;
uint16_t ReData = 0;
return ReData * ADCLSB;
}
float AXP192::GetAPSVoltage() {
float ADCLSB = 1.4 / 1000.0;
uint16_t ReData = 0;
return ReData * ADCLSB;
}

float AXP192::GetBatCoulombInput() {
uint32_t ReData = 0;
return ReData * 65536 * 0.5 / 3600 / 25.0;
}

float AXP192::GetBatCoulombOut() {
uint32_t ReData = 0;
return ReData * 65536 * 0.5 / 3600 / 25.0;
}

void AXP192::SetCoulombClear() {}

void AXP192::SetLDO2(bool State) {}

void AXP192::PowerOff() { digitalWrite(POWER_HOLD_PIN, LOW); }
#endif
5 changes: 5 additions & 0 deletions arduino_workspace/libraries/M5StickC-Plus/src/AXP192.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Arduino.h>
#include <Wire.h>
#include "utility/Button.h"

#define SLEEP_MSEC(us) (((uint64_t)us) * 1000L)
#define SLEEP_SEC(us) (((uint64_t)us) * 1000000L)
Expand Down Expand Up @@ -37,6 +38,10 @@ class AXP192 {
uint16_t GetIusbinData(void) __attribute__((deprecated));
uint16_t GetVapsData(void) __attribute__((deprecated));
uint8_t GetBtnPress(void);
#define DEBOUNCE_MS 10
#ifdef ARDUINO_M5Stick_C_Plus2
Button BtnC = Button(35, true, DEBOUNCE_MS);
#endif

// -- sleep
void SetSleep(void);
Expand Down
26 changes: 26 additions & 0 deletions arduino_workspace/libraries/M5StickC-Plus/src/utility/Config.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_

#ifndef ARDUINO_M5Stick_C_Plus2
#define M5_IR 9
#define M5_LED 10
#else
#define M5_IR 19
#define M5_LED 19
#define POWER_HOLD_PIN 4
#define LCD_BL_PIN 27
#define LCD_PWM_CHAN 2
// NOTE: PWM HZ and BITS has to be carefully chosen https://lastminuteengineers.com/esp32-pwm-tutorial/
#define LCD_BL_HZ 500
#define LCD_BL_RESOLUTION_BITS 12
const uint32_t gamma_lut[LCD_BL_RESOLUTION_BITS + 1] = {0,516,920,1289,1639,1974,2298,2613,2920,3222,3517,3808,4095};
#endif
/*
import matplotlib.pyplot as plt
import numpy as np
max_bits = int(12)
gamma = float(2.2)
x = np.arange(0, max_bits + 1)
y = np.power(x, 1. / gamma)
y = y / np.max(y) * (2 ** max_bits - 1)
plt.plot(x, y)
# plt.show()
print(",".join(y.astype(int).astype(str)))
*/
#define M5_BUTTON_HOME 37
#define M5_BUTTON_RST 39

Expand Down
Loading

0 comments on commit 16f6c8a

Please sign in to comment.