-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Refactor getBattery function for voltage reading #1915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Refactor getBattery function to improve voltage calculation and ensure percent is clamped between 0 and 100.
|
I also removed a couple unnecessary/redundant lines such as setting GPIO10 to input as analogReadMilliVolts() already does this, also changed the voltage to be stored as a 16 bit unsigned integer as 32 bit is very very overkill lol |
|
Making the math in the float realm is a good idea as It removes the risk of lose some values in the float/int conversions mid calculation But I didn't understand why you multiplied the voltage by 2... It will give you always 100% in the end... |
|
Because GPIO10 reads the voltage after a voltage divider, so you must account for that by multiplying it by two. Idk if you've ever used any other firmwares [edit to add: on the cardputer], but Bruce reports wildly differently than others including one I made that uses the function built into m5unified. for example, a week after using my cardputer everyday, Bruce was still reporting 85%, yet (for example) Evil-M5 was reporting around 40%. So to get to the bottom of it, I wrote a basic firmware that printed the battery percent via M5Cardputer.Power() and it was pretty much spot on to Evil-M5. Here is the section of the Power_Class that corresponds to the cardputer, notice the 2x ADC ratio case board_t::board_M5CardputerADV: https://github.com/m5stack/M5Unified/blob/master/src/utility/Power_Class.cpp I'm pretty sure yours is underflowing tbh, since at full charge pin 10 will read about 2.1V after the divider. So after all that math you do you end up with a negative battery percent every time |
|
Just checked out of curiosity and evil-cardputer also accounts for the voltage divider uint32_t mv = esp_adc_cal_raw_to_voltage(raw, adc_chars) * 2; // facteur 2 car diviseur interne Edit: just saw on the sticker on the back of the cardputer it shows 1/2VBAT:G10 coming out of the voltage divider. Would've been much much easier to look there to confirm that instead of combing through crap tons of code like I did 😅 |
|
well well.. now it doesn't show anything when unplugged :( better wrong then missing |
|
So how then will you fix this Mr Jader242... =) As all respected coders
finish what they start--
Good thing we have all the time in the world.. almost.. And i am very
curious about this as it have been the smallest issue ever but still it
needs to get that final finish.. Haha. Also it needs to fit all the Bruce
Devices.. Its like 30 or more now.. And growing.. How to make a battery
icon that will fit and work fluidly on all of them... We put our trust in
you Mr Jader242...
…-In Jader We Trust-
On Thu, Dec 11, 2025 at 4:53 AM Pirata ***@***.***> wrote:
*bmorcelli* left a comment (BruceDevices/firmware#1915)
<#1915 (comment)>
well well.. now it doesn't show anything when unplugged :(
better wrong then missing
—
Reply to this email directly, view it on GitHub
<#1915 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BLDL6BTAJRAKVYMLDK3KWN34BDTDRAVCNFSM6AAAAACOVQT4RSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMMZZHE3DGMRQGY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
--
Kind Regards
C.R
May Prosperity allways be with us!!
|
|
I fixed it... Now I need to fix a problem of other PR |
|
Thanks Mr Pirata =) Doing great as always.. We can almost land on the moon
with this now.. =)
The world is a safer place indeed.. !
…On Thu, Dec 11, 2025 at 10:05 AM Pirata ***@***.***> wrote:
*bmorcelli* left a comment (BruceDevices/firmware#1915)
<#1915 (comment)>
I fixed it... Now I need to fix a problem of other PR
—
Reply to this email directly, view it on GitHub
<#1915 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BLDL6BV4BWAVWKOO27ENBN34BEXVHAVCNFSM6AAAAACOVQT4RSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMNBQHEZTSNJUGE>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
Kind Regards
C.R
May Prosperity allways be with us!!
|
|
What was the fix if you don't mind me asking? |
|
@Huzzla101 why would I need to do it for every device? Do you not see that this one file is specific to the cardputer? Lololol Sure the logic could probably benefit from being upgraded, but the biggest issue here (which is fixed now) was that Bruce wasn't accounting for the voltage divider so even at 100% the value was underflowing |
|
All other devices that use ADC in fact use voltage divider.. The problem was in an implicit conversion.. now it is working and showing the same as Evil |
|
Wait I'm confused now, it looks like you removed the whole section of code in question if I'm not mistaken. So where is it accounting for the voltage divider (2x factor for the cardputer, but it may be different for other devices idk)? Is it in another file I'm not seeing? Edit: is it the ini file maybe? |
|
I removed the redundant code from all envs that use the same kind of logic, moved the code to other file and set the flags on these environment config files https://github.com/BruceDevices/firmware/blob/dev/src%2Fcore%2Futils.cpp#L32-L57 |
|
Gotcha, I ended up finding it right before you made that comment 😅 out of curiosity (and to learn) what was the issue with the code I submitted? I have a feeling it was with removing the line that sets pin 10 to input, as after researching it I was pretty sure analogReadMilliVolts() already did that, but I could've been wrong I guess. Doing a quick comparison of the two, that's the only change that stands out to me. Am I correct? Edit: or I suppose it could've been an issue with using uint16_t instead of uint32_t? |
|
@jader242 hehe... Well it's needed for all devices but... Lets get in the
Xmas mood and make the next update the greatest of all.. !! 💫
I want Xmas games and all the good stuff....
…On Thu, Dec 11, 2025 at 11:28 AM jader242 ***@***.***> wrote:
*jader242* left a comment (BruceDevices/firmware#1915)
<#1915 (comment)>
Gotcha, I ended up finding it right before you made that comment 😅 out of
curiosity (and to learn) what was the issue with the code I submitted? I
have a feeling it was with removing the line that sets pin 10 to input, as
after researching it I was pretty sure readMilliVolts() already did that,
but I could've been wrong I guess. Doing a quick comparison of the two,
that's the only change that stands out to me. Am I correct?
—
Reply to this email directly, view it on GitHub
<#1915 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BLDL6BRKBOXQQHXERSK3UAL4BFBMNAVCNFSM6AAAAACOVQT4RSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMNBRGI3DINZUHE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Kind Regards
C.R
May Prosperity allways be with us!!
|
|
@Huzzla101 idk if you meant it, but your original comment came off very condescending/passive aggressive/dickish, like: Yea, I wasn't going to just leave it not working... Why would I go through all of the work of figuring out what the issue was if I was just going to leave it in an unworking state? I was taking a nap after working an 8 hour day and being a full time student, so I apologize I didn't see piratas notification the second he made the comment saying something was wrong with my code. Unfortunately, I do not have all the time in the world as you say. If you indeed do, count your blessings my friend |
|
@bmorcelli what was the charge state of your battery when you tested it and it was blank unless plugged in? I have a feeling your battery was relatively low, and the math I did was slightly off (at least lol) and likely calculated it was below minimum voltage and returned 0. Which would explain why it was blank, because when getBattery() is called from drawStatusBar() in display.cpp it only calls drawBatteryStatus() if getBattery() returns a value greater than 0 edit: wait I now see that you said the problem was with an implicit conversion, my bad, for some reason I didn't realize that was you telling me the issue with the original code. I thought that part of your reply was to something else. Thanks though! I hope to continue learning and contributing :) |
|
My friend @jader242... The thing is that alot of people have been
interested in this, coming from all areas, but only a few is dedicated
coders so i didn't know who you were yet so i just needed to approach
you somehow .. =) To put it short i love all real programmers , especially
regarding my situation , as from a Social Engineering aspect, we are the
ones who fully understand the importance of a programmer, and also will
use every method to keep the world a safer place.. As you already know...
Peace...
Doing a great job to..
…On Thu, Dec 11, 2025 at 5:28 PM jader242 ***@***.***> wrote:
*jader242* left a comment (BruceDevices/firmware#1915)
<#1915 (comment)>
@bmorcelli <https://github.com/bmorcelli> what was the charge state of
your battery when you tested it and it was blank unless plugged in? I have
a feeling your battery was relatively low, and the math I did was slightly
off (at least lol) and likely calculated it was below minimum voltage and
returned 0. Which would explain why it was blank, because when getBattery()
is called from drawStatusBar() in display.cpp it only calls
drawBatteryStatus() if getBattery() returns a value greater than 0
—
Reply to this email directly, view it on GitHub
<#1915 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BLDL6BQW4TEPOTU7A3FSVN34BGLUVAVCNFSM6AAAAACOVQT4RSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMNBSG42DENRTHE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Kind Regards
C.R
May Prosperity allways be with us!!
|
|
@Huzzla101 well in that case no hard feelings. to be fair, this was my very first github pull request, and it's also some of the first c++ i've ever written. so you have the right to be skeptical of me. i'm really just here to learn. the cardputer is a fascinating device that i'm absolutely in love with, so i thought it would be a good way to learn to code. and i just happened to notice that bruce/launcher reported a battery percentage way different than other firmwares, and after bringing it up in the discord a couple times with no response, i figured why not fix it myself ya know? i hope to contribute more in the future, but as i'm still a beginner (i've only known that esp32s exist for about a month lol) i won't rush anything :) |
I'm sorry for being rude before, I wasn't on a good day.. Bruce and Launcher are all about learning, you are welcome to ask whatever you need and contribute whenever possible.. Your suggestion triggered a modification on Launcher too.. so the Beta version is already fixed and working.. About the code you made, I have no idea why it didn't work.. because the changes I made were about converting to the right kind of variable in the calculation.. kinda misterious |
|
You have not been rude Mr Pirata ... haha .. We know you work a lot with
your great ancient mind.. =) .. Ask and you shall receive says the Matrix..
Bruce is rocketing to the moon and hopefully back again to .. Great work
everybody--
Getting the Xmas spirit...
…On Fri, Dec 12, 2025 at 7:03 PM Pirata ***@***.***> wrote:
*bmorcelli* left a comment (BruceDevices/firmware#1915)
<#1915 (comment)>
@Huzzla101 <https://github.com/Huzzla101> well in that case no hard
feelings. to be fair, this was my very first github pull request, and it's
also some of the first c++ i've ever written. so you have the right to be
skeptical of me. i'm really just here to learn. the cardputer is a
fascinating device that i'm absolutely in love with, so i thought it would
be a good way to learn to code. and i just happened to notice that
bruce/launcher reported a battery percentage way different than other
firmwares, and after bringing it up in the discord a couple times with no
response, i figured why not fix it myself ya know? i hope to contribute
more in the future, but as i'm still a beginner (i've only known that
esp32s exist for about a month lol) i won't rush anything :)
I'm sorry for being rude before, I wasn't on a good day..
Bruce and Launcher are all about learning, you are welcome to ask whatever
you need and contribute whenever possible..
Your suggestion triggered a modification on Launcher too.. so the Beta
version is already fixed and working..
About the code you made, I have no idea why it didn't work.. because the
changes I made were about converting to the right kind of variable in the
calculation.. kinda misterious
—
Reply to this email directly, view it on GitHub
<#1915 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BLDL6BVDDBHCNCIEAXJANEL4BL7PJAVCNFSM6AAAAACOVQT4RSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMNBXGU3TONBSGY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Kind Regards
C.R
May Prosperity allways be with us!!
|
|
@jader242 you just started with esp32-s3 boards.. Wow man your in for a
good ride then... I would recommend the M5 stick cplus 2 that is a gold
mine right there.. Cardputer you already have , that is also a pioneer..
Hehe Its so much good ESP32 S3 boards out there .. Bruce even have a own
smoochie board etc to ... Its alot of love.. And best part they cost like
2-10 dollars at aliexpress, buy a couple and get free shipping to.. China
will send you a privat jet with your stuff rest assured... Craziest part is
that it's not a joke ..... And keep coding that is what makes this world go
smoothly .. :p
On Fri, Dec 12, 2025 at 9:56 PM Christian Roth. ***@***.***>
wrote:
… You have not been rude Mr Pirata ... haha .. We know you work a lot with
your great ancient mind.. =) .. Ask and you shall receive says the Matrix..
Bruce is rocketing to the moon and hopefully back again to .. Great work
everybody--
Getting the Xmas spirit...
On Fri, Dec 12, 2025 at 7:03 PM Pirata ***@***.***> wrote:
> *bmorcelli* left a comment (BruceDevices/firmware#1915)
> <#1915 (comment)>
>
> @Huzzla101 <https://github.com/Huzzla101> well in that case no hard
> feelings. to be fair, this was my very first github pull request, and it's
> also some of the first c++ i've ever written. so you have the right to be
> skeptical of me. i'm really just here to learn. the cardputer is a
> fascinating device that i'm absolutely in love with, so i thought it would
> be a good way to learn to code. and i just happened to notice that
> bruce/launcher reported a battery percentage way different than other
> firmwares, and after bringing it up in the discord a couple times with no
> response, i figured why not fix it myself ya know? i hope to contribute
> more in the future, but as i'm still a beginner (i've only known that
> esp32s exist for about a month lol) i won't rush anything :)
>
> I'm sorry for being rude before, I wasn't on a good day..
>
> Bruce and Launcher are all about learning, you are welcome to ask
> whatever you need and contribute whenever possible..
>
> Your suggestion triggered a modification on Launcher too.. so the Beta
> version is already fixed and working..
>
> About the code you made, I have no idea why it didn't work.. because the
> changes I made were about converting to the right kind of variable in the
> calculation.. kinda misterious
>
> —
> Reply to this email directly, view it on GitHub
> <#1915 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/BLDL6BVDDBHCNCIEAXJANEL4BL7PJAVCNFSM6AAAAACOVQT4RSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMNBXGU3TONBSGY>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>
--
Kind Regards
C.R
May Prosperity allways be with us!!
--
Kind Regards
C.R
May Prosperity allways be with us!!
|
Refactor getBattery function to improve voltage calculation and ensure percent is clamped between 0 and 100.
Proposed Changes
Changed battery calculation math/logic
Types of Changes
added voltage divider compensation per the m5unified library, both upper and lower ends bound
Testing
I have not tested it yet, but I will in a few hours
Linked Issues
None yet
User-Facing Change
Should drastically increase battery percentage accuracy, as it stands now Bruce is very innacurate often being 30-40% off what the power function of M5Unified returns
Further Comments
None atm