Rockbox Development > Starting Development and Compiling

Learning How to Code

(1/4) > >>

Phalangees:
I've taken a stab at learning to code for rockbox. I know C well enough to make something simple but I don't understand everything. Here is a test program I made which basically allows you to move a little circle around the screen. It works fine for the most part but it does funny things to my gigabeat. The hard drive never stops spinning and the music playback pauses. Could someone be so kind to help me out??



--- Code: ---/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (   )  \___|    < | \_\ (   > <  
__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: Testing File$
 *
 * Coded by Kyle Kamperschroer (Phalangees)
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
//The mother of all includes
#include "plugin.h"

PLUGIN_HEADER

//As of now, I'm just learning and I only have a gigabeat so I coded for a gigabeat.
#define QUIT BUTTON_MENU
#define SELECT BUTTON_SELECT
#define UP BUTTON_UP
#define DOWN BUTTON_DOWN
#define LEFT BUTTON_LEFT
#define RIGHT BUTTON_RIGHT

static struct plugin_api* rb;

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
(void)parameter;

rb = api;
int button;
int lcdwidth = LCD_WIDTH;
int lcdheight = LCD_HEIGHT;
int xpos = lcdwidth/2;   //Puts the circle in
int ypos = lcdheight/2;  //the middle of the screen
int xspeed = 0;
int yspeed = 0;
while (true)
{
  button = rb->button_get(false); //Gets user input
if(button==UP){
yspeed++;
}
if(button==DOWN){
yspeed--;
}
if(button==LEFT){
xspeed++;
}
if(button==RIGHT){
xspeed--;
}
if(button==SELECT){
xspeed /= 2; //The hand break  :p
yspeed /= 2;
}
if(button==QUIT){
break;  //Breaks out of the while loop
}
//Checks for walls (edges of screen)
if(xpos = lcdwidth)
xspeed = -xspeed;
if(ypos = lcdheight)
yspeed = -yspeed;

//Changes the position depending on the speed
xpos -= xspeed;
ypos -= yspeed;
               
//Updates display
rb->lcd_clear_display();
rb->lcd_fillrect(xpos,ypos,4,4);
rb->lcd_fillrect(xpos+1,ypos-1,2,6);
rb->lcd_fillrect(xpos-1,ypos+1,6,2);
rb->lcd_update();

//Checks for USB (not tested)
if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
            return PLUGIN_USB_CONNECTED;
}

return PLUGIN_OK;

}
--- End code ---

**Note: I made this specifically for the gigabeat so don't yell at me. :p

So why does this code seem to overload my gigabeat?

linuxstb:
Rockbox uses co-operative multi-tasking.  Meaning that a thread needs to call the "yield()" function periodically in order to allow other threads to run.  In plugins this is "rb->yield();".

Phalangees:
So basically by making the infinite loop I was taking over all of the threads?


EDIT------------
That seemed to do the trick. Is calling rb->yield();  just once sufficient enough?

Thanks for your help.

saratoga:
You want to call it regularly.  If your loop runs very fast, then 1 time per loop may be enough (or maybe even too often).  If its a very involved loop, then you may want to call it more often.  The idea is just to regularly give the other threads (codec, etc) time to also run.

Phalangees:
Ok thanks!! When I add a lot of more stuff I'll make it yield more often.

I'm going to try and make a game with a circle you can move around and you'll collect coins and get points. You have health and there is a bad guy that chases you and speeds up with every 20 coins you get.

I made this game first in flash and now I'll try to make it rockboxable. :p

Navigation

[0] Message Index

[#] Next page

Go to full version