Skip to content

Commit 3f476d8

Browse files
committed
Wrap up backlight for Tough
- Rework Screen class for Core2 and Tough. - Update nugets.
1 parent c544157 commit 3f476d8

File tree

15 files changed

+119
-121
lines changed

15 files changed

+119
-121
lines changed

M5StackCommon/Core2ToughCommon.cs

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,33 @@ public static Pcf8563 ReatTimeClock
7676
get => _rtc;
7777
}
7878

79+
#if M5CORE2
80+
7981
/// <summary>
80-
/// Sets on or off the Power Led.
82+
/// Sets on or off the Power LED.
8183
/// </summary>
8284
public static bool PowerLed
8385
{
8486
get => _powerLed;
8587
set
8688
{
8789
_powerLed = value;
88-
_power.EnableLDO2(_powerLed);
90+
91+
if(_powerLed)
92+
{
93+
// turn ON by setting duty cycle to 100%
94+
_power.Pwm1DutyCycleSetting1 = 10;
95+
_power.Pwm1DutyCycleSetting2 = 10;
96+
}
97+
else
98+
{
99+
// tuen OFF by setting duty cycle to 0%
100+
_power.Pwm1DutyCycleSetting1 = 0;
101+
_power.Pwm1DutyCycleSetting2 = 0;
102+
}
89103
}
90104
}
91105

92-
#if M5CORE2
93-
94106
/// <summary>
95107
/// Vibrate the M5Core2 when true.
96108
/// </summary>
@@ -136,21 +148,6 @@ public static Chs6540 TouchController
136148
return _touchController;
137149
}
138150
}
139-
140-
/// <summary>
141-
/// Gets or sets the state of the display backlight.
142-
/// Set to <see langword="true"/> to turn on and <see langword="false"/> to turn off.
143-
/// </summary>
144-
public static bool Backlight
145-
{
146-
get => _backLight;
147-
set
148-
{
149-
_backLight = value;
150-
_power.EnableLDO3(_backLight);
151-
}
152-
}
153-
154151
#endif
155152

156153
/// <summary>
@@ -292,7 +289,8 @@ static Tough()
292289
I2cDevice i2c = new(new I2cConnectionSettings(1, Axp192.I2cDefaultAddress));
293290
_power = new(i2c);
294291

295-
// Configuration common for M5Core2 and Tough
292+
// Configuration common for M5Core2 and M5Tough
293+
296294
// VBUS-IPSOUT Pass-Through Management
297295
_power.SetVbusSettings(false, false, VholdVoltage.V4_0, true, VbusCurrentLimit.MilliAmper500);
298296
// Set Power off voltage 3.0v
@@ -303,87 +301,80 @@ static Tough()
303301
_power.SetChargingFunctions(true, ChargingVoltage.V4_2, ChargingCurrent.Current100mA, ChargingStopThreshold.Percent10);
304302
// Enable RTC BAT charge
305303
_power.SetBackupBatteryChargingControl(true, BackupBatteryCharingVoltage.V3_0, BackupBatteryChargingCurrent.MicroAmperes200);
306-
// 128ms power on, 4s power off
307-
_power.SetButtonBehavior(LongPressTiming.S1, ShortPressTiming.Ms128, true, SignalDelayAfterPowerUp.Ms32, ShutdownTiming.S4);
304+
308305
// Set ADC all on
309306
_power.AdcPinEnabled = AdcPinEnabled.All;
310307
// Set ADC sample rate to 25Hz
311308
_power.AdcFrequency = AdcFrequency.Frequency25Hz;
312309
_power.AdcPinCurrent = AdcPinCurrent.MicroAmperes80;
313310
_power.BatteryTemperatureMonitoring = true;
314311
_power.AdcPinCurrentSetting = AdcPinCurrentSetting.AlwaysOn;
312+
315313
// GPIO0 is LDO
316314
_power.Gpio0Behavior = Gpio0Behavior.LowNoiseLDO;
317315
// GPIO0 LDO output 2.8V
318316
_power.PinOutputVoltage = PinOutputVoltage.V2_8;
319-
320-
#if TOUGH
321-
// PWM1 X
322-
_power.Pwm1OutputFrequencySetting = 0;
323-
// PWM1 Y1
324-
_power.Pwm1DutyCycleSetting1 = 0xFF;
325-
// PWM1 Y2
326-
_power.Pwm1DutyCycleSetting2 = 0xFF;
327-
#endif
328-
329-
// enable pins
330-
#if M5CORE2
331-
_power.LdoDcPinsEnabled = LdoDcPinsEnabled.DcDc1 | LdoDcPinsEnabled.DcDc3 | LdoDcPinsEnabled.Ldo2 | LdoDcPinsEnabled.Ldo3;
332-
#elif TOUGH
333-
_power.LdoDcPinsEnabled = LdoDcPinsEnabled.DcDc1 | LdoDcPinsEnabled.Ldo2 | LdoDcPinsEnabled.Ldo3;
334-
#endif
335-
336317
// Sets DCDC1 3350mV (ESP32 VDD)
337318
_power.DcDc1Voltage = ElectricPotential.FromVolts(3.35);
338-
// Switch on the power
339-
PowerLed = true;
340319

341-
// LCD + SD peripheral power supply
320+
// LCD and peripherals power supply
342321
_power.LDO2OutputVoltage = ElectricPotential.FromVolts(3.3);
343322
_power.EnableLDO2(true);
344323

324+
// GPIO2 enables the speaker
325+
_power.Gpio2Behavior = Gpio12Behavior.MnosLeakOpenOutput;
326+
327+
// GPIO4 LCD reset (and also Touch controller reset on M5Core2)
328+
_power.Gpio4Behavior = Gpio4Behavior.MnosLeakOpenOutput;
329+
330+
// Set temperature protection
331+
_power.SetBatteryHighTemperatureThreshold(ElectricPotential.FromVolts(3.2256));
332+
333+
// This part of the code will handle the button behavior
334+
_power.EnableButtonPressed(ButtonPressed.LongPressed | ButtonPressed.ShortPressed);
335+
// 128ms power on, 4s power off
336+
_power.SetButtonBehavior(LongPressTiming.S1, ShortPressTiming.Ms128, true, SignalDelayAfterPowerUp.Ms32, ShutdownTiming.S4);
337+
345338
#if M5CORE2
339+
// enable DCO and LDO outputs
340+
_power.LdoDcPinsEnabled = LdoDcPinsEnabled.DcDc1 | LdoDcPinsEnabled.DcDc3 | LdoDcPinsEnabled.Ldo2 | LdoDcPinsEnabled.Ldo3;
341+
346342
// Sets the Vibrator voltage
347343
_power.LDO3OutputVoltage = ElectricPotential.FromVolts(2.0);
348344
// vibrator off
349345
Vibrate = false;
350346

351-
// Sets the LCD Voltage to 2.8V
347+
// Sets the LCD backlight voltage to 2.8V
352348
_power.DcDc3Voltage = ElectricPotential.FromVolts(2.8);
353349

354-
// GPIO1 PWM
350+
// GPIO1 set to PWM to control power LED
355351
_power.Gpio1Behavior = Gpio12Behavior.PwmOutput;
356352

353+
// Switch on the power LED
354+
PowerLed = true;
355+
357356
// battery = 360mAh
358357
_power.ChargingCurrent = ChargingCurrent.Current360mA;
359358

360359
#elif TOUGH
361-
// Sets backlight voltage to 3.0
362-
//_power.LDO3OutputVoltage = ElectricPotential.FromVolts(3.0);
363-
Backlight = true;
364-
_power.DcDc3Voltage = ElectricPotential.FromVolts(0);
360+
// PWM1 X
361+
_power.Pwm1OutputFrequencySetting = 0;
362+
// PWM1 Y1
363+
_power.Pwm1DutyCycleSetting1 = 0xFF;
364+
// PWM1 Y2
365+
_power.Pwm1DutyCycleSetting2 = 0xFF;
365366

366-
// GPIO1 Touch Reset
367+
// enable DCO and LDO outputs
368+
_power.LdoDcPinsEnabled = LdoDcPinsEnabled.DcDc1 | LdoDcPinsEnabled.Ldo2 | LdoDcPinsEnabled.Ldo3;
369+
370+
// Sets the LCD backlight voltage to 3V
371+
_power.LDO3OutputVoltage = ElectricPotential.FromVolts(3.0);
372+
373+
// GPIO1 is reset for Touch controller
367374
_power.Gpio1Behavior = Gpio12Behavior.MnosLeakOpenOutput;
368375

369376
#endif
370377

371-
// GPIO2 speaker enable
372-
_power.Gpio2Behavior = Gpio12Behavior.MnosLeakOpenOutput;
373-
374-
// GPIO4 LCD reset (and Touch reset on M5Core2)
375-
_power.Gpio4Behavior = Gpio4Behavior.MnosLeakOpenOutput;
376-
377-
// Set GPIO4 as output (rest LCD)
378-
// _power.Gpio4Behavior = Gpio4Behavior.MnosLeakOpenOutput;
379-
380-
// Set temperature protection
381-
_power.SetBatteryHighTemperatureThreshold(ElectricPotential.FromVolts(3.2256));
382-
383-
// This part of the code will handle the button behavior
384-
//_power.EnableButtonPressed(ButtonPressed.LongPressed | ButtonPressed.ShortPressed);
385-
// _power.SetButtonBehavior(LongPressTiming.S2, ShortPressTiming.Ms128, true, SignalDelayAfterPowerUp.Ms32, ShutdownTiming.S10);
386-
387378
// Setup buttons
388379
_gpio = new();
389380

M5StackCommon/Screen.cs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
namespace nanoFramework.M5Stack
1313
{
1414
/// <summary>
15-
/// M5Core2 screen class
15+
#if M5CORE2
16+
/// M5Core2 screen class.
17+
#elif TOUGH
18+
/// M5Tough screen class.
19+
#endif
1620
/// </summary>
1721
public class Screen : ScreenBase
1822
{
@@ -21,11 +25,12 @@ public class Screen : ScreenBase
2125
/// </summary>
2226
public const int DefaultMemoryAllocationBitmap = 320 * 240 * 4;
2327

28+
private const byte DefaultScreenBrightness = 75;
2429
private const int ChipSelect = 5;
2530
private const int DataCommand = 15;
2631
private const int Reset = -1;
2732
private static Axp192 _power;
28-
private static byte _lumi;
33+
private static byte _brightness;
2934
private static bool _isInitialized = false;
3035

3136
/// <summary>
@@ -39,8 +44,9 @@ public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap)
3944
return;
4045
}
4146

42-
// We're allocating anough memory for the full screen as this is a SPRAM board
47+
// We're allocating enough memory for the full screen because these targets have PSRAM
4348
MemoryAllocationBitmap = memoryBitMapAllocation;
49+
// backligth is not controlled by the screen driver
4450
BackLightPin = -1;
4551

4652
#if M5CORE2
@@ -49,10 +55,7 @@ public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap)
4955
_power = M5Stack.Tough.Power;
5056
#endif
5157

52-
// Enable the screen
53-
Enabled = true;
54-
55-
// Reset
58+
// Reset screen
5659
_power.Gpio4Value = PinValue.Low;
5760
Thread.Sleep(100);
5861
_power.Gpio4Value = PinValue.High;
@@ -61,56 +64,57 @@ public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap)
6164
// Create the screen
6265
DisplayControl.Initialize(new SpiConfiguration(2, ChipSelect, DataCommand, Reset, BackLightPin), new ScreenConfiguration(0, 0, 320, 240), (uint)MemoryAllocationBitmap);
6366

64-
// For M5Core2, values from 2.5 to 3V are working fine
65-
// 3.0V for the screen
66-
LuminosityPercentage = 100;
67+
// set initial value for brightness
68+
BrightnessPercentage = DefaultScreenBrightness;
6769

68-
#if M5CORE2
69-
_power.EnableDCDC3(true);
70-
#elif TOUGH
71-
_power.EnableLDO3(true);
72-
#endif
70+
// enable back-light
71+
Enabled = true;
7372

7473
_isInitialized = true;
7574
}
7675

7776
/// <summary>
7877
/// Enables or disables the screen.
7978
/// </summary>
79+
/// <value><see langword="true"/> to enable the screen, <see langword="false"/> to disable it.</value>
8080
public static new bool Enabled
8181
{
8282
get => IsEnabled;
8383

8484
set
8585
{
8686
IsEnabled = value;
87-
_power.EnableLDO2(IsEnabled);
87+
88+
#if M5CORE2
89+
_power.EnableDCDC3(IsEnabled);
90+
#elif TOUGH
91+
_power.EnableLDO3(IsEnabled);
92+
#endif
8893
}
8994
}
9095

9196
/// <summary>
92-
/// Sets or gets the screen luminosity.
97+
/// Gets or sets the screen brightness.
9398
/// </summary>
94-
/// <remarks> On M5Core2, anything less than 20% will be fully black</remarks>
95-
public static new byte LuminosityPercentage
99+
/// <value>Brightness as percentage.</value>
100+
public static new byte BrightnessPercentage
96101
{
97-
get => _lumi;
102+
get => _brightness;
98103

99104
set
100105
{
101-
102-
// For M5Core2, values from 2.5 to 3V are working fine
106+
// For M5Core2 and M5Tough, values from 2.5 to 3V are working fine
103107
// 2.5 V = dark, 3.0 V full luminosity
104-
_lumi = (byte)(value > 100 ? 100 : _lumi);
105-
106-
#if TOUGH
107-
_power.LDO2OutputVoltage = ElectricPotential.FromVolts(_lumi * 0.5 / 100.0 + 2.0);
108-
#elif M5CORE2
109-
_power.LDO3OutputVoltage = ElectricPotential.FromVolts((byte)(_lumi * 0.5 / 100.0 + 2.5));
108+
_brightness = (byte)(value > 100 ? 100 : value);
109+
var backLightVoltage = ElectricPotential.FromVolts(_brightness * 0.5 / 100.0 + 2.5);
110+
#if M5CORE2
111+
_power.LDO3OutputVoltage = backLightVoltage;
112+
#elif TOUGH
113+
_power.LDO3OutputVoltage = backLightVoltage;
110114
#endif
111-
112115
}
113116
}
114117
}
115118
}
116-
#endif
119+
120+
#endif

Tests/M5Core2TestApp/M5Core2TestApp.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,4 @@
172172
<ProjectConfigurationsDeclaredAsItems />
173173
</ProjectCapabilities>
174174
</ProjectExtensions>
175-
</Project>
175+
</Project>

Tests/M5Core2TestApp/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
<package id="UnitsNet.nanoFramework.ElectricPotential" version="4.132.0" targetFramework="netnanoframework10" />
3535
<package id="UnitsNet.nanoFramework.Power" version="4.132.0" targetFramework="netnanoframework10" />
3636
<package id="UnitsNet.nanoFramework.Temperature" version="4.132.0" targetFramework="netnanoframework10" />
37-
</packages>
37+
</packages>

Tests/M5StickTestApp/M5StickTestApp.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@
132132
<ProjectConfigurationsDeclaredAsItems />
133133
</ProjectCapabilities>
134134
</ProjectExtensions>
135-
</Project>
135+
</Project>

Tests/M5StickTestApp/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
<package id="UnitsNet.nanoFramework.ElectricPotential" version="4.132.0" targetFramework="netnanoframework10" />
2525
<package id="UnitsNet.nanoFramework.Power" version="4.132.0" targetFramework="netnanoframework10" />
2626
<package id="UnitsNet.nanoFramework.Temperature" version="4.132.0" targetFramework="netnanoframework10" />
27-
</packages>
27+
</packages>

Tests/ToughTestApp/Program.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414

1515
Debug.WriteLine("Hello from Tough!");
1616

17-
const string Ssid = "SSID";
18-
const string Password = "YourWifiPasswordHere";
19-
// Give 60 seconds to the wifi join to happen
20-
CancellationTokenSource cs = new(60000);
21-
var success = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true, token: cs.Token);
22-
if (!success)
23-
{
24-
// Something went wrong, you can get details with the ConnectionError property:
25-
Debug.WriteLine($"Can't connect to the network, error: {WifiNetworkHelper.Status}");
26-
if (WifiNetworkHelper.HelperException != null)
27-
{
28-
Debug.WriteLine($"ex: {WifiNetworkHelper.HelperException}");
29-
}
30-
}
17+
Console.WriteLine("Hello from Tough!");
18+
19+
20+
//const string Ssid = "SSID";
21+
//const string Password = "YourWifiPasswordHere";
22+
//// Give 60 seconds to the wifi join to happen
23+
//CancellationTokenSource cs = new(60000);
24+
//var success = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true, token: cs.Token);
25+
//if (!success)
26+
//{
27+
// // Something went wrong, you can get details with the ConnectionError property:
28+
// Debug.WriteLine($"Can't connect to the network, error: {WifiNetworkHelper.Status}");
29+
// if (WifiNetworkHelper.HelperException != null)
30+
// {
31+
// Debug.WriteLine($"ex: {WifiNetworkHelper.HelperException}");
32+
// }
33+
//}
3134

3235
Tough.TouchEvent += TouchEventCallback;
3336

0 commit comments

Comments
 (0)