Homebrew Question Any C coders in here?

mrdude

Developer
OP
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
@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,227
@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: 170
  • sd files.zip
    6.3 MB · Views: 154
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,227
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: 185

mrdude

Developer
OP
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
@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
    S @ salazarcosplay: @Skelletonike first time I hear of stellar blade