Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Thank You for your continued support and contributions!

+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Learning How to Code
« previous next »
  • Print
Pages: [1] 2

Author Topic: Learning How to Code  (Read 4874 times)

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Learning How to Code
« on: August 23, 2007, 12:22:23 PM »
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: [Select]
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $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 <=0 || xpos >= lcdwidth)
xspeed = -xspeed;
if(ypos <=0 || 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;

}

**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?
Logged

Offline linuxstb

  • Developer
  • Member
  • *
  • Posts: 1163
Re: Learning How to Code
« Reply #1 on: August 23, 2007, 01:02:06 PM »
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();".
Logged

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #2 on: August 23, 2007, 01:08:12 PM »
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.
« Last Edit: August 23, 2007, 01:12:42 PM by Phalangees »
Logged

Offline saratoga

  • Developer
  • Member
  • *
  • Posts: 8974
Re: Learning How to Code
« Reply #3 on: August 23, 2007, 01:15:17 PM »
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.
Logged

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #4 on: August 23, 2007, 01:20:45 PM »
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
Logged

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #5 on: August 24, 2007, 03:42:08 PM »
Sorry about double posting but I've got some questions.

Is there any documentation about the structure of rockbox? Linuxstb introduced me to the GraphicsAPI which is a great page but it's not linked to anywhere so it seems to be hidden.

Say I wanted to load an external bitmap in a plugin. Is there any documentation on how to do this rather then opening up other code and just trying to figure it out?
Logged

Offline AlexP

  • Global Moderator
  • Member
  • *
  • Posts: 3688
  • ex-BigBambi
Re: Learning How to Code
« Reply #6 on: August 24, 2007, 03:50:00 PM »
I don't know about the main question, but the modify button is there so you don't have to double post.
Logged
H140, F60, S120, e260, c240, Clip, Fuze v2, Connect, MP170, Meizu M3, Nano 1G, Android

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #7 on: August 24, 2007, 04:16:49 PM »
Again, sorry for double posting but modifying the thread wouldn't make it "new" for everyone who has read it already. I thought double posting was a better idea then another thread.
Logged

Offline GodEater

  • Member
  • *
  • Posts: 2829
Re: Learning How to Code
« Reply #8 on: August 24, 2007, 04:17:22 PM »
Double posting is against the forum rules however...
Logged

Read The Manual Please

Offline Llorean

  • Member
  • *
  • Posts: 12931
Re: Learning How to Code
« Reply #9 on: August 24, 2007, 05:20:35 PM »
And editing your thread *does* make it show the new tab.
Logged

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #10 on: August 24, 2007, 05:43:16 PM »
Then I apologize. Does anyone know the answer to my question? Again, I'm sorry for breaking the forum rules.
Logged

Offline Bagder

  • Member
  • *
  • Posts: 1452
    • Daniel's site
Re: Learning How to Code
« Reply #11 on: August 24, 2007, 05:53:15 PM »
Most of the code and Rockbox' inner workings are not documented outside the source files, but we have hundreds of contributors who are fairly familiar with it, and you can easily ask for help here, on IRC or on the dev mailing list.

A plugin can read a bmp by using the regular plugin API, see apps/plugin.h
Logged

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #12 on: August 24, 2007, 09:08:01 PM »
Thank you for your help Badger. I really appreciate.

If I have another question I'll just edit this post or ask on IRC.

Again, I'm sorry about breaking the forum rules.
Logged

Offline elborak

  • Member
  • *
  • Posts: 177
Re: Learning How to Code
« Reply #13 on: August 24, 2007, 10:26:22 PM »
Quote from: Phalangees on August 24, 2007, 09:08:01 PM
Thank you for your help Badger.
That's Bagder.
Logged

Offline Phalangees

  • Member
  • *
  • Posts: 136
  • Gigabeat F20 and S60
Re: Learning How to Code
« Reply #14 on: August 26, 2007, 03:19:12 PM »
My goal right now is to display a part of a simple bitmap image.

I'm making a little program called "coinmuncher.c." I have a bitmap called "coinmuncher_objects.240x320x16.bmp" inside of the apps/plugins/bitmaps/native folder. My code is starting to get huge so I won't post it all but could someone explain to me why I keep getting the compiler error: "undefined reference to `coinmuncher_objects'."

Here are some snipits of code that involve the bitmap.

This line is above the global variables.
Code: [Select]
extern const fb_data coinmuncher_objects[];

This line is my call to display the bitmap.
Code: [Select]
rb->lcd_bitmap_transparent_part(coinmuncher_objects, 0, 30, 60, manX, manY, 20, 20);


I'm a little bit confused about the whole thing and if it's something I'm doing that's completely idiotic then please let me know. I've looked through a bunch of other plugins that display bitmaps and I don't see why mine doesn't work.

Thanks for all your help.
Logged

  • Print
Pages: [1] 2
« previous next »
+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Learning How to Code
 

  • SMF 2.0.17 | SMF © 2019, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.119 seconds with 15 queries.