Homebrew Question Any C coders in here?

mrdude

Developer
OP
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,237
@JustBrandonT

While I appreciate your info, this original app wasn't written by me, I'm just adding some mods to it, when adding
#include <pthread.h> - that messes with all the original timers in the original code, so the entire code would need to be re-written, which I don't have time for and doubt I could as I am not a skilled programmer. I only started looking at C about a week or so ago, and don't understand a lot of the code. Everything I'm adding to the mod I'm doing is being learned as I am going along.
I do appreciate you taking the time to reply, but I also know my own limitations - and this is too complex for me just now, until I learn more about C and coding in general. Thanks anyway though.
 

JustBrandonT

Well-Known Member
Newcomer
Joined
Mar 11, 2018
Messages
75
Trophies
0
Age
34
XP
518
Country
Canada
@JustBrandonT

While I appreciate your info, this original app wasn't written by me, I'm just adding some mods to it, when adding
#include <pthread.h> - that messes with all the original timers in the original code, so the entire code would need to be re-written, which I don't have time for and doubt I could as I am not a skilled programmer. I only started looking at C about a week or so ago, and don't understand a lot of the code. Everything I'm adding to the mod I'm doing is being learned as I am going along.
I do appreciate you taking the time to reply, but I also know my own limitations - and this is too complex for me just now, until I learn more about C and coding in general. Thanks anyway though.

Well just in case you decide to continue.. I've created a thread pool and a queue that you can use.. I added it to my post on page 1.. with example usage (main.c)..

OldCode: Uses polling and dispatch to main thread is implemented.
NewCode: Uses condition variables and can have main thread wait or get notified when tasks are complete.. Can dispatch to main thread too.

Uhh don't #include <pthread.h> in your header then.. I modified the code on page 1 to use pthread internally in the threadpool.c file but not in the header file (.h).. This should fix the import issues you are facing and still allow you to use multi-threading..
 
Last edited by JustBrandonT,
  • Like
Reactions: mrdude

mrdude

Developer
OP
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,237
@JustBrandonT

Thanks, I had a look at you're modded post but I don't know if it would be possible or not to do with this. I'll attach the source code I've modded, also the micro sd card files. Once compiled and the payload is injected - you should get this screen:
ruzuYX4.jpg


As you can see from that - there is a battery icon, the battery icon has colour bars - which change depending on the charge level read.

This code is called from: gui_menu.c, in the function: gui_menu_draw_entries - function called: battery_power();

This is the only place I could call that code, so that it would draw on top of the menu + icons. It doesn't draw over the icons from main.c, currently it updates when you touch the screen - as the menu is refreshed. If you could have a look at the source code I'm posting and see if you could implement your 'fix' into that, I would be most grateful, as this level of skill is not something I am blessed with for now.
 

Attachments

  • source.zip
    332.2 KB · Views: 171
  • sd files.zip
    6.3 MB · Views: 155
Last edited by mrdude,

JustBrandonT

Well-Known Member
Newcomer
Joined
Mar 11, 2018
Messages
75
Trophies
0
Age
34
XP
518
Country
Canada
@JustBrandonT

Thanks, I had a look at you're modded post but I don't know if it would be possible or not to do with this. I'll attach the source code I've modded, also the micro sd card files. Once compiled and the payload is injected - you should get this screen...


I checked the code and compiled it. Looks nice. You can modify the code such that the `touch_poll` doesn't loop infinitely blocking the main thread. In fact, you don't need "multi-threading" here due to the code structure.. you just had to find where the code was stalling and where it's main loop was..

For example, modify the code to look like:

touch.c:
Code:
//No longer loops infinitely waiting for a touch to happen..
//Instead, the function polls for a touch event..
touch_event_t touch_wait()
{
    touch_event_t event;
    touch_poll(&event);
    return event;
}


Then you can modify gui_menu.c to:

Code:
static int handle_touch_input(gui_menu_t *menu)
{
    gui_menu_entry_t *entry = NULL;
    touch_event_t event;

    u32 runningtime = get_tmr_us() / 1000000;
    int delay = 5; // delay in seconds
    int total = runningtime + delay;

    do
    {
        if (((u32)get_tmr_us() / 1000000) >= total)
        {
            //Add your code here.. which will only be ran once every 5 seconds..

            //Example: The below will print "Hello World!" every 5 seconds..
            //Note:
            //    We do not need to "redraw" the menu or the background either because it will get redrawn once we "return"..
            //    We do not need to call gfx_swap_buffer because we "return" the control-flow back to gui_menu_update..

            gfx_printf(&g_gfx_con, "%kHELLO WORLD!%k", colour_red, 0xFFCCCCCC);

            return 1;
        }

        //Polls for touches constantly..
        event = touch_wait();
    } while (event.type != STMFTS_EV_MULTI_TOUCH_ENTER);

    for (u32 i = 0; i < menu->next_entry; i++)
    {
        entry = menu->entries[i];

        if (entry->handler != NULL && is_rect_touched(&event, entry->x, entry->y, entry->width, entry->height))
        {
            if (entry->handler(entry->param) != 0)
                return 0;
        }
    }

    return 1;
}


Why? Because touch_wait used to loop infinitely waiting for touches and constantly checking if a touch happened.. you can make it loop still, but in a different location such that you can inject code into its event-loop.. as shown above..
 
Last edited by JustBrandonT,
  • Like
Reactions: snoofly and mrdude

mrdude

Developer
OP
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,237
I checked the code and compiled it. Looks nice. You can modify the code such that the `touch_poll` doesn't loop infinitely blocking the main thread. In fact, you don't need "multi-threading" here due to the code structure.. you just had to find where the code was stalling and where it's main loop was..

For example, modify the code to look like:

touch.c:
Code:
//No longer loops infinitely waiting for a touch to happen..
//Instead, the function polls for a touch event..
touch_event_t touch_wait()
{
    touch_event_t event;
    touch_poll(&event);
    return event;
}


Then you can modify gui_menu.c to:

Code:
static int handle_touch_input(gui_menu_t *menu)
{
    gui_menu_entry_t *entry = NULL;
    touch_event_t event;

    u32 runningtime = get_tmr_us() / 1000000;
    int delay = 5; // delay in seconds
    int total = runningtime + delay;

    do
    {
        if (((u32)get_tmr_us() / 1000000) >= total)
        {
            //Add your code here.. which will only be ran once every 5 seconds..

            //Example: The below will print "Hello World!" every 5 seconds..
            //Note:
            //    We do not need to "redraw" the menu or the background either because it will get redrawn once we "return"..
            //    We do not need to call gfx_swap_buffer because we "return" the control-flow back to gui_menu_update..

            gfx_printf(&g_gfx_con, "%kHELLO WORLD!%k", colour_red, 0xFFCCCCCC);

            return 1;
        }

        //Polls for touches constantly..
        event = touch_wait();
    } while (event.type != STMFTS_EV_MULTI_TOUCH_ENTER);

    for (u32 i = 0; i < menu->next_entry; i++)
    {
        entry = menu->entries[i];

        if (entry->handler != NULL && is_rect_touched(&event, entry->x, entry->y, entry->width, entry->height))
        {
            if (entry->handler(entry->param) != 0)
                return 0;
        }
    }

    return 1;
}


Why? Because touch_wait used to loop infinitely waiting for touches and constantly checking if a touch happened.. you can make it loop still, but in a different location such that you can inject code into its event-loop.. as shown above..

Wow, thanks very much for spending your time to look at this, I really appreciate this a lot and I'm sure others do as well. This is some good code example and explanation, I just wish we had more members here such as yourself. I'm pretty busy for the rest of the day - But I'll implement your code later tonight when I get the chance.

I've just downloaded C for dummies & beginning programming with C so I'll also have a read of those so I can get some more info on C programming (I've not used it before). Thanks again, you're a good person.

EDIT: I just tried it, it works great - thanks again, that's magic :-)
 
Last edited by mrdude,
  • Like
Reactions: JustBrandonT

KuranKu

I am KranK
Developer
Joined
Jan 13, 2019
Messages
367
Trophies
0
Age
34
Location
Israel
XP
1,181
Country
Israel
Code:
        uint32_t runningtime = get_tmr_us() / 1000000;
        int delay = 5; // delay in seconds
        int total = runningtime + delay;
     
        while (true)
        {
            for(size_t i = 0; i < total; i++)
            {
                if(runningtime < i)
                {
                      /* code */
                }
                else
                {
                     /* code */
                }
               
            }
             
        }
 
Last edited by KuranKu,

JustBrandonT

Well-Known Member
Newcomer
Joined
Mar 11, 2018
Messages
75
Trophies
0
Age
34
XP
518
Country
Canada
Wow, thanks very much for spending your time to look at this, I really appreciate this a lot and I'm sure others do as well. This is some good code example and explanation, I just wish we had more members here such as yourself. I'm pretty busy for the rest of the day - But I'll implement your code later tonight when I get the chance.

I've just downloaded C for dummies & beginning programming with C so I'll also have a read of those so I can get some more info on C programming (I've not used it before). Thanks again, you're a good person.

EDIT: I just tried it, it works great - thanks again, that's magic :-)


Nice :) Glad it worked.

For everyone else, I uploaded a zip of the source with the changes :)
It prints "Hello World" every 5 seconds.
 

Attachments

  • source 2.zip
    397.1 KB · Views: 186

mrdude

Developer
OP
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,237
@JustBrandonT

Thanks for posting, I've updated some code in the source files compared to the version you've posted - I have another question though.

Currently I have this code to centre text on the screen (x co-ords only, not y).

Code:
        //code to centre text on the screen
      
        const char *s = "Loading menu - please wait!";
        int total_width = 1280;
        int half_height = 360;
        int scale = 3;
        int font_size = ((scale * 8) / 2);
        int y_loc = (half_height - font_size);
        const int s_width = (strlen(s) * 8) * scale; //change 8 to size of bitmap font in use
        const int field_width = (total_width - s_width) / 2;
      
        g_gfx_con.scale = scale;
        gfx_con_setpos(&g_gfx_con, field_width, y_loc);      
        gfx_printf(&g_gfx_con, "%k%s%k", 0xFF3065E1, s, BLACK);

Instead of needing to write the same code for every line of text I want to centre, I think it would make more sense to create a function that can be called with 2 variables:

Text
Scale

As the payloads are limited to the amount of bytes available - this would save some space and leave that for more code that could be added in future, I am unsure on creating a function with variables - would you happen to know how to code a function like that, and a little example on how to call that function so the variables are added?

Thanks.

EDIT: Scrap that request, I figured it out. Thanks for reading though.
 
Last edited by mrdude,
  • Like
Reactions: JustBrandonT

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • Jayro @ Jayro:
    I have yaoi anime hands, very lorge but slender.
  • Jayro @ Jayro:
    I'm Slenderman.
  • Veho @ Veho:
    I have hands.
  • BakerMan @ BakerMan:
    imagine not having hands, cringe
    +1
  • AncientBoi @ AncientBoi:
    ESPECIALLY for things I do to myself :sad:.. :tpi::rofl2: Or others :shy::blush::evil:
    +1
  • The Real Jdbye @ The Real Jdbye:
    @SylverReZ if you could find a v5 DS ML you would have the best of both worlds since the v5 units had the same backlight brightness levels as the DS Lite unlockable with flashme
  • The Real Jdbye @ The Real Jdbye:
    but that's a long shot
  • The Real Jdbye @ The Real Jdbye:
    i think only the red mario kart edition phat was v5
  • BigOnYa @ BigOnYa:
    A woman with no arms and no legs was sitting on a beach. A man comes along and the woman says, "I've never been hugged before." So the man feels bad and hugs her. She says "Well i've also never been kissed before." So he gives her a kiss on the cheek. She says "Well I've also never been fucked before." So the man picks her up, and throws her in the ocean and says "Now you're fucked."
    +2
  • BakerMan @ BakerMan:
    lmao
  • BakerMan @ BakerMan:
    anyways, we need to re-normalize physical media

    if i didn't want my games to be permanent, then i'd rent them
    +1
  • BigOnYa @ BigOnYa:
    Agreed, that why I try to buy all my games on disc, Xbox anyways. Switch games (which I pirate tbh) don't matter much, I stay offline 24/7 anyways.
  • AncientBoi @ AncientBoi:
    I don't pirate them, I Use Them :mellow:. Like I do @BigOnYa 's couch :tpi::evil::rofl2:
    +1
  • cearp @ cearp:
    @BakerMan - you can still "own" digital media, arguably easier and better than physical since you can make copies and backups, as much as you like.

    The issue is DRM
  • cearp @ cearp:
    You can buy drm free games / music / ebooks, and if you keep backups of your data (like documents and family photos etc), then you shouldn't lose the game. but with a disk, your toddler could put it in the toaster and there goes your $60

    :rofl2:
  • cearp @ cearp:
    still, I agree physical media is nice to have. just pointing out the issue is drm
  • rqkaiju2 @ rqkaiju2:
    i like physical media because it actually feels like you own it. thats why i plan on burning music to cds
  • cearp @ cearp:
    It's nice to not have to have a lot of physical things though, saves space
    +1
  • AncientBoi @ AncientBoi:
    Nor clothes 🤮 . Saves on time, soap, water and money having to wash them. :D
  • SylverReZ @ SylverReZ:
    @rqkaiju2, Physical media is a great source for archiving your data, none of that cloud storage shiz.
    +1
  • AncientBoi @ AncientBoi:
    [squeezes @SylverReZ onto a physical media, then archives you in my old stuff box] :tpi::rofl2::tpi:
    +1
    SylverReZ @ SylverReZ: @AncientBoi, Lmao +1