Changeset 13 in Thomson


Ignore:
Timestamp:
29/01/2012 18:20:30 (16 months ago)
Author:
pulkomandy
Message:
  • Move GUI in its own class
  • Design C++ callbacksystem for IUP
  • Load file from menu
Location:
elec/CrO2/software
Files:
2 added
2 edited
2 moved

Legend:

Unmodified
Added
Removed
  • elec/CrO2/software/Makefile

    r12 r13  
    1414 
    1515CC              = g++ 
    16 CPPFLAGS        = $(USBFLAGS) -O -Wall -I/usr/include/IUP/ 
     16CPPFLAGS        = $(USBFLAGS) -O -Wall -I/usr/include/IUP/ -g 
    1717LIBS    = $(USBLIBS) -L/usr/lib/ -mwindows -liup -lcomctl32 -lole32 
    1818 
     
    2323all: $(PROGRAM) 
    2424 
    25 $(PROGRAM): $(PROJECT).o device.o poller.o 
     25$(PROGRAM): $(PROJECT).o device.o gui.o k5.o 
    2626        $(CC) -o $(PROGRAM) $^ $(LIBS) 
    2727 
  • elec/CrO2/software/cro2.cpp

    r12 r13  
    1212#include <unistd.h> 
    1313 
    14 #include <iup.h> 
    15 #include <iupcontrols.h> 
    1614 
    1715#include "device.h" 
    18 #include "poller.h" 
    19  
    20 /* UI */ 
    21 int menu_open(Ihandle* that) 
    22 { 
    23         IupPopup(IupFileDlg(), IUP_CENTER, IUP_CENTER); 
    24         return IUP_DEFAULT; 
    25 } 
    26  
    27 int menu_exit(Ihandle* that) 
    28 { 
    29         return IUP_CLOSE; 
    30 } 
    31  
    32  
    33 Ihandle* motoron; 
    34 void GUI_open(int* argc, char*** argv) 
    35 { 
    36         IupOpen(argc, argv); 
    37 //      IupControlsOpen(); 
    38  
    39         IupSetFunction("OPEN", menu_open); 
    40         IupSetFunction("EXIT", menu_exit); 
    41  
    42         Ihandle* menu = IupMenu( 
    43                 IupSubmenu("File", 
    44                         IupMenu( 
    45                                 IupItem("Open", "OPEN"),                 
    46                                 IupItem("Exit", "EXIT"), 
    47                                 NULL 
    48                         ) 
    49                 ), 
    50                 NULL 
    51         ); 
    52  
    53         // CONTROL 
    54         motoron = IupProgressBar(); 
    55         IupSetAttribute(motoron, "RASTERSIZE", "16x16"); 
    56  
    57         // EXPLORE 
    58         Ihandle* platformlist = IupList(NULL); 
    59         IupSetAttribute(platformlist, "EXPAND", "HORIZONTAL"); 
    60         IupSetAttribute(platformlist, "DROPDOWN", "YES"); 
    61         IupSetAttribute(platformlist, "1", "MO5"); 
    62         IupSetAttribute(platformlist, "VALUE", "1"); 
    63  
    64         Ihandle* blocklist = IupTree(); 
    65         IupSetAttribute(blocklist, "EXPAND", "VERTICAL"); 
    66  
    67         Ihandle* tabs = IupTabs( 
    68                 IupVbox( 
    69                         IupHbox( 
    70                                 IupLabel("Motor"), 
    71                                 motoron, 
    72                                 NULL 
    73                         ), 
    74                         IupHbox( 
    75                                 IupToggle("play",NULL), 
    76                                 IupToggle("REC",NULL), 
    77                                 NULL 
    78                         ), 
    79                         NULL 
    80                 ), 
    81                 IupVbox( 
    82                         IupHbox( 
    83                                 IupLabel("Format:"), 
    84                                 platformlist, 
    85                                 NULL 
    86                         ), 
    87                         IupHbox( 
    88                                 blocklist, 
    89                                 IupVbox( 
    90 //                                      IupMatrix(NULL), 
    91                                         IupLabel("Checksum:"), 
    92                                         NULL 
    93                                 ), 
    94                                 NULL 
    95                         ) 
    96                 ), 
    97                 NULL 
    98         ); 
    99  
    100         IupSetAttribute(tabs,"TABTITLE0", "Control"); 
    101         IupSetAttribute(tabs,"TABTITLE1", "Explore"); 
    102  
    103         Ihandle* dialog = IupDialog(tabs); 
    104         IupSetAttribute(dialog, "TITLE", "CrO2 tape emulator"); 
    105         IupSetAttributeHandle(dialog, "MENU", menu); 
    106         IupShow(dialog); 
    107  
    108         // Run the timer 
    109         startPolling(); 
    110  
    111         IupMainLoop(); 
    112  
    113         IupClose(); 
    114 } 
    115  
     16#include "k5.h" 
     17#include "gui.h" 
    11618 
    11719int main(int argc, char **argv) 
    11820{ 
    119         //usb_dev_handle      *handle = NULL; 
    120         unsigned char       buffer[275]; 
    12121        int                 nBytes = 0; 
    12222 
    12323    if(argc < 2){ 
    124                 GUI_open(&argc, &argv); 
     24                Gui gui(&argc, &argv); 
    12525                exit(0); 
    12626    } 
     
    13030 
    13131                if(strcmp(argv[1], "get") == 0){ 
     32                        unsigned char       buffer[275]; 
    13233                        memset(buffer, 0, 275); 
    13334                        nBytes = dev.read(buffer, sizeof(buffer)); 
    13435                }else if(strcmp(argv[1], "put") == 0){ 
    13536 
    136                         // wait for motor on 
    137                         while (dev.getStatus() & 8) 
    138                                 usleep(1000000); 
     37                        // load file 
     38                        K5 file(argv[2]); 
    13939 
    140                         // load file 
    141                         FILE* fptr = fopen(argv[2], "rb"); 
    142                         int blockid; 
    143                         uint8_t blktype, blksize; 
    144                         sscanf(argv[3], "%d", &blockid); 
     40                        for (int k = 0; k < file.getBlockCount(); k++) 
     41                        { 
     42                                // wait for motor on 
     43                                while (dev.getStatus() & 8) 
     44                                        Sleep(1000); 
    14545 
    146                         // fast-forward to requested block 
    147                         do 
    148                         { 
    149                                 do 
    150                                 { 
    151                                         fread(&blktype, 1, 1, fptr); 
    152                                         if (feof(fptr)) 
    153                                         { 
    154                                                 fprintf(stderr, "end of file.\n"); 
    155                                                 fclose(fptr); 
    156                                                 exit(0); 
    157                                         } 
    158                                 } 
    159                                 while(blktype != 0x5A); // skip sync header 
     46                                K5::Block block = file.getBlock(k); 
    16047 
    161                                 fread(&blktype, 1, 1, fptr); 
    162                                 fread(&blksize, 1, 1, fptr); 
    163                                 blksize -= 2; 
    164                                 fread(buffer, 1, blksize + 1, fptr); 
    165                                 if (blktype == 0) 
    166                                 { 
    167                                         // new file 
    168                                         printf("%.11s\n",buffer); 
    169                                 } 
     48                                nBytes = dev.write(block.data, block.length - 1, block.type); 
     49 
     50                                // TODO wait for correct time (read status from usb OR compute from size+type) 
     51                                Sleep(1400); 
    17052                        } 
    171                         while (blockid --); 
    172  
    173                         fclose(fptr); 
    174  
    175                         nBytes = dev.write(buffer, blksize, blktype); 
    17653                }else{ 
    17754                        // TODO print usage 
     
    18663                std::cerr << error << std::endl; 
    18764        } 
     65 
     66        exit(0); 
    18867} 
    18968 
  • elec/CrO2/software/gui.cpp

    r12 r13  
    1 #include "poller.h" 
     1/* CrO2 datassette emulator 
     2 * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx> 
     3 * 
     4 * Distributed under the terms of the MIT licence. 
     5 */ 
     6 
     7#include "gui.h" 
    28 
    39#include "device.h" 
     10#include "k5.h" 
    411 
    512#include <stdint.h> 
    6  
     13#include <iup.h> 
     14#include <iupcontrols.h> 
     15 
     16/////////////////////////////////////////////////////////////////////////////// 
     17// Super awesome extended powerful ultimate deluxe C++ IUP callback system ;) 
     18 
     19typedef int (Gui::*Callee)(); 
     20 
     21class Callback 
     22{ 
     23        public: 
     24                static int call(Ihandle* that); 
     25                static int destroy(Ihandle* that); 
     26                static void create(Ihandle* handle, const char* name, Gui* self, Callee what); 
     27 
     28        private: 
     29                Callback(Gui* self, Callee what); 
     30                Callback(); // do not use 
     31 
     32                Gui* self; 
     33                Callee what; 
     34}; 
     35 
     36void Callback::create(Ihandle* handle,const char* name, Gui* self, Callee what) 
     37{ 
     38        Callback* cb = new Callback(self, what); 
     39        IupSetAttribute(handle, "LCALLBACK", (char*)cb); 
     40        IupSetCallback(handle, name, Callback::call); 
     41        IupSetCallback(handle, "LDESTROY_CB", Callback::destroy); 
     42} 
     43 
     44Callback::Callback(Gui* self, Callee what) 
     45{ 
     46        this->self = self; 
     47        this->what = what; 
     48} 
     49 
     50int Callback::call(Ihandle* that) 
     51{ 
     52        Callback* call = (Callback*)IupGetAttribute(that, "LCALLBACK"); 
     53        return ((call->self)->*(call->what))(); 
     54} 
     55 
     56int Callback::destroy(Ihandle* that) 
     57{ 
     58        Callback* call = (Callback*)IupGetAttribute(that, "LCALLBACK"); 
     59        delete call; 
     60        return IUP_DEFAULT; 
     61} 
     62 
     63/////////////////////////////////////////////////////////////////////////////// 
    764 
    865        // Start status poller "thread" 
    966        int pollStatus(Ihandle* ih) 
    1067        { 
     68                Ihandle* motoron = (Ihandle*)IupGetAttribute(ih, "target"); 
     69 
    1170                uint8_t status = Device::getDevice().getStatus(); 
    1271                if (status & 8) 
     
    1776        } 
    1877 
    19 void startPolling() { 
     78void startPolling(Ihandle* target) { 
    2079        Ihandle* timer = IupTimer(); 
     80 
     81        IupSetAttribute(timer, "target", (const char*)target); 
     82 
    2183        IupSetAttribute(timer, "TIME", "300"); 
    2284        IupSetCallback(timer, "ACTION_CB", pollStatus); 
    2385        IupSetAttribute(timer, "RUN", "YES"); 
    2486} 
     87 
     88/* UI */ 
     89 
     90int menu_exit(Ihandle* that) 
     91{ 
     92        return IUP_CLOSE; 
     93} 
     94 
     95 
     96 
     97Gui::Gui(int* argc, char*** argv) 
     98{ 
     99        file = NULL; 
     100 
     101        IupOpen(argc, argv); 
     102//      IupControlsOpen(); 
     103 
     104        IupSetFunction("EXIT", menu_exit); 
     105 
     106        Ihandle* menu_open = IupItem("Open", NULL); 
     107        Callback::create(menu_open, "ACTION", this, &Gui::menu_open); 
     108 
     109        Ihandle* menu = IupMenu( 
     110                IupSubmenu("File", 
     111                        IupMenu( 
     112                                menu_open,               
     113                                IupItem("Exit", "EXIT"), 
     114                                NULL 
     115                        ) 
     116                ), 
     117                NULL 
     118        ); 
     119 
     120 
     121        // CONTROL 
     122        Ihandle* motoron = IupProgressBar(); 
     123        IupSetAttribute(motoron, "RASTERSIZE", "16x16"); 
     124 
     125        // EXPLORE 
     126        Ihandle* platformlist = IupList(NULL); 
     127        IupSetAttribute(platformlist, "EXPAND", "HORIZONTAL"); 
     128        IupSetAttribute(platformlist, "DROPDOWN", "YES"); 
     129        IupSetAttribute(platformlist, "1", "MO5"); 
     130        IupSetAttribute(platformlist, "VALUE", "1"); 
     131 
     132        Ihandle* blocklist = IupTree(); 
     133        IupSetAttribute(blocklist, "EXPAND", "VERTICAL"); 
     134 
     135        Ihandle* tabs = IupTabs( 
     136                IupVbox( 
     137                        IupHbox( 
     138                                IupLabel("Motor"), 
     139                                motoron, 
     140                                NULL 
     141                        ), 
     142                        IupHbox( 
     143                                IupToggle("play",NULL), 
     144                                IupToggle("REC",NULL), 
     145                                NULL 
     146                        ), 
     147                        NULL 
     148                ), 
     149                IupVbox( 
     150                        IupHbox( 
     151                                IupLabel("Format:"), 
     152                                platformlist, 
     153                                NULL 
     154                        ), 
     155                        IupHbox( 
     156                                blocklist, 
     157                                IupVbox( 
     158//                                      IupMatrix(NULL), 
     159                                        IupLabel("Checksum:"), 
     160                                        NULL 
     161                                ), 
     162                                NULL 
     163                        ) 
     164                ), 
     165                NULL 
     166        ); 
     167 
     168        IupSetAttribute(tabs,"TABTITLE0", "Control"); 
     169        IupSetAttribute(tabs,"TABTITLE1", "Explore"); 
     170 
     171        Ihandle* dialog = IupDialog(tabs); 
     172        IupSetAttribute(dialog, "TITLE", "CrO2 tape emulator"); 
     173        IupSetAttributeHandle(dialog, "MENU", menu); 
     174        IupShow(dialog); 
     175 
     176        // Run the timer 
     177        startPolling(motoron); 
     178 
     179        IupMainLoop(); 
     180 
     181        IupClose(); 
     182} 
     183 
     184 
     185Gui::~Gui() 
     186{ 
     187        delete file; 
     188} 
     189 
     190int Gui::menu_open() 
     191{ 
     192        char name[65536]; 
     193        name[0] = 0; 
     194        if (IupGetFile(name) == 0) 
     195        { 
     196                // Load file 
     197                file = new K5(name); 
     198        } 
     199        return IUP_DEFAULT; 
     200} 
  • elec/CrO2/software/gui.h

    r12 r13  
     1/* CrO2 datassette emulator 
     2 * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx> 
     3 * 
     4 * Distributed under the terms of the MIT licence. 
     5 */ 
     6 
    17#include <iup.h> 
    28 
    3 void startPolling(); 
     9class K5; 
    410 
    5 extern Ihandle* motoron; 
     11class Gui { 
     12        public: 
     13                Gui(int* argc, char*** argv); 
     14                ~Gui(); 
     15 
     16        private: 
     17                int menu_open(); 
     18 
     19                K5* file; 
     20}; 
Note: See TracChangeset for help on using the changeset viewer.