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
|-+  Support and General Use
| |-+  Plugins/Viewers
| | |-+  Example plugin
« previous next »
  • Print
Pages: [1]

Author Topic: Example plugin  (Read 421 times)

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Example plugin
« on: April 15, 2022, 07:16:48 PM »
Code: [Select]
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2022
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "lib/pluginlib_actions.h"

const struct button_mapping* plugin_contexts[]= {
    pla_main_ctx,
#if defined(HAVE_REMOTE_LCD)
    pla_remote_ctx,
#endif
};

int cubeSize = 24;
int cubeSpacing = 4;


#ifdef HAVE_SCROLLWHEEL
#define ACT_UP          PLA_SCROLL_FWD
#define ACT_UP_REPEAT   PLA_SCROLL_FWD_REPEAT
#define ACT_DOWN        PLA_SCROLL_BACK
#define ACT_DOWN_REPEAT PLA_SCROLL_BACK_REPEAT
#else
#define ACT_UP          PLA_UP
#define ACT_UP_REPEAT   PLA_UP_REPEAT
#define ACT_DOWN        PLA_DOWN
#define ACT_DOWN_REPEAT PLA_DOWN_REPEAT
#endif

struct candidate
{
    char character;
    int guess;
};
#define NB_LETTERS 26
struct candidate candidates[NB_LETTERS];
static char guesses[NB_LETTERS];

static void cleanup(void *parameter)
{
    (void)parameter;
}
void make_background(void)
{
    rb->lcd_set_background(LCD_WHITE);
    rb->lcd_set_foreground(LCD_BLACK);
    rb->lcd_clear_display();
}
 
void draw_square(int x, int y, int font_w, int font_h, char letter)
{
    char tempCharacter[2];
    rb->snprintf(tempCharacter, sizeof(tempCharacter), "%c", letter);

    rb->lcd_fillrect(x,y,cubeSize,cubeSize);
    rb->lcd_set_drawmode(DRMODE_COMPLEMENT);

    int x0 = x + (cubeSize - font_w) / 2;
    int y0 = y + (cubeSize - font_h) / 2;
    rb->lcd_putsxy(x0, y0, tempCharacter);

    rb->lcd_set_drawmode(DRMODE_SOLID);
}

 
enum plugin_status plugin_start(const void* parameter)
{
    (void)parameter;
    bool quit = false;
    int action = ACTION_NONE;
    int nbguesses = 0;
 
    for (int i = 0; i < NB_LETTERS; i++)
    {
        candidates[i].character = 65 + i;
        guesses[i] = ' ';
    }
   
    int xStart = (LCD_WIDTH - (5 * cubeSize) - (4 * cubeSpacing)) / 2;
    int yStart = 32;

    rb->lcd_setfont(FONT_SYSFIXED);
    struct font *font = rb->font_get(FONT_SYSFIXED);
    int font_h = font->height;
    int font_w = rb->font_get_width(font, 'W');
   
    int fchr = 0;
   
    while(!quit)
    {
        make_background();
       
        int xPosTest = 0;
        rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
        for (int i = fchr; i < NB_LETTERS; i++)
        {
            char tempCharacter[2];
            rb->snprintf(tempCharacter, sizeof(tempCharacter), "%c", candidates[i].character);
            rb->lcd_putsxy(xPosTest, 0, tempCharacter);
            xPosTest += font_w * 2;
        }
        rb->lcd_drawrect(0, 0, font_w + 2, font_h + 2);
        rb->lcd_set_drawmode(DRMODE_SOLID);

        int xPos = xStart;
        int yPos = yStart;

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                char guessed = guesses[i * 5 + j];
                draw_square(xPos, yPos, font_w, font_h, guessed);
                xPos += cubeSize + cubeSpacing;
            }
            yPos += cubeSize + cubeSpacing;
            xPos = xStart;
        }

        //rb->lcd_set_drawmode(DRMODE_BG);
        //rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
 
        //rb->lcd_set_drawmode(DRMODE_SOLID);
 
        rb->lcd_update();
 
        action = pluginlib_getaction(0, plugin_contexts, ARRAYLEN(plugin_contexts));
   
        switch (action)
        {
            case PLA_EXIT:
            /* FALL-Through */
            case PLA_CANCEL:
                quit = true;
                cleanup(NULL);
                return PLUGIN_OK;
            case PLA_LEFT:
                break;
            case PLA_RIGHT:
                break;
            case ACT_UP:
            /* FALL-Through */
            case ACT_UP_REPEAT:
                fchr++;
                if (fchr >= NB_LETTERS)
                    fchr = NB_LETTERS -1;
                break;
           
            case ACT_DOWN:
            /* FALL-Through */
            case ACT_DOWN_REPEAT:
                fchr--;
                if (fchr < 0)
                    fchr = 0;
                break;
            case PLA_SELECT:
                guesses[nbguesses++] = candidates[fchr].character;
                if (nbguesses >= NB_LETTERS)
                    nbguesses = 0;
                break;

            default:
                if (rb->default_event_handler_ex(action, cleanup, NULL)
                    == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
        }

    }
    return PLUGIN_OK;
}

« Last Edit: April 15, 2022, 07:21:38 PM by Bilgus »
Logged

  • Print
Pages: [1]
« previous next »
+  Rockbox Technical Forums
|-+  Support and General Use
| |-+  Plugins/Viewers
| | |-+  Example plugin
 

  • SMF 2.0.19 | SMF © 2021, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.028 seconds with 21 queries.