As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/

SELECT * FROM posts WHERE tid = 'PA PROGRAMMING THREAD'

15681011100

Posts

  • bowenbowen How you doin'? Registered User regular
    Can you give me an example what isn't working like you expected it to?

    I can give you some ideas but I don't have a C compiler handy to test at the moment.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • bowenbowen How you doin'? Registered User regular
    Also the code is incomplete it looks like. The problem may not be happening in those modules. Spoiler the complete [code] maybe?

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • zeenyzeeny Registered User regular
    jaziek wrote: »

    There are a lot of things wrong with the structure of that code, but a glance says your "lines" array's indexes are not pointing where you think they are. Test it.

    such as? I am still very much a beginner to coding in general, so much so that even this, which is essentially just defining strings, is problematic.


    I just don't see where the problem is...

    define the line type.
    create an array of lines.
    create individual lines
    store them in the array.
    access a given index of the array to retrieve the characters on a line.

    where am I not doing what I think I'm doing?

    If this is the code as is, again, currentLine is undefined.

  • jaziekjaziek Bad at everything And mad about it.Registered User regular
    edited February 2012
    currentline is defined as a global variable, because it is used in many other functions (none of which are called before the problem arises, so that isn't an issue.)

    bowen wrote: »
    Can you give me an example what isn't working like you expected it to?

    I can give you some ideas but I don't have a C compiler handy to test at the moment.

    I don't have a camera handy, and this is compiled for, and running on, an embedded chip with a very simple 32 character LCD screen attatched to it. The initial message gets printed fine, however when "messaging" on the top line, and "settings" on the second line should be printed, I instead get just garbage symbols across the entire screen.

    jaziek on
    Steam ||| SC2 - Jaziek.377 on EU & NA. ||| Twitch Stream
  • jackaljackal Fuck Yes. That is an orderly anal warehouse. Registered User regular
    jaziek wrote: »
    currentline is defined as a global variable, because it is used in many other functions (none of which are called before the problem arises, so that isn't an issue.)

    global variables are bad m'kay.

  • urahonkyurahonky Registered User regular
    Have you output currentLine in the main just to make sure it's not garbage?

  • zeenyzeeny Registered User regular
    edited February 2012
    Then I'll say it again. Your "lines" array indexes aren't pointing where you think they are. Test it. :winky:

    Edit: You will find the problem faster if you don't use a typedef. It's hiding it.

    zeeny on
  • jaziekjaziek Bad at everything And mad about it.Registered User regular
    urahonky wrote: »
    Have you output currentLine in the main just to make sure it's not garbage?

    all the lines contain the right characters, and currentline contains the correct number. Clearly then, it is the fact that my array isn't doing what I think it is.

    Steam ||| SC2 - Jaziek.377 on EU & NA. ||| Twitch Stream
  • EtheaEthea Registered User regular
    edited February 2012
    jaziek wrote:
    how good are some people here at C? I see no reason why the following code shouldn't work, but it doesn't.
    
    typedef struct line { 
    			unsigned char* characters; 
    		     } LINE;
    
    //line array containing characters for a given line.
    LINE lines[7];
    	LINE line1;
    	LINE line2;
    	LINE line3;
    	LINE line4;
    	LINE line5;
    	LINE line6;
    	LINE line7;
    	LINE line8;
    
    //sets up the inital home screen and menu options.
    
    void menu_setup(){
    
    	
    	lines[0] = line1;
    	lines[1] = line2;
    	lines[2] = line3;
    	lines[3] = line4;
    	lines[4] = line5;
    	lines[5] = line6;
    	lines[6] = line7;
    	lines[7] = line8;
    	line1.characters = "   welcome      ";
    	line2.characters = " press any key  ";
    
    	index = 0;
    	recorded = 0;
    	hrzpos = 0;
    	vpos = 0;
    	  
    	int i;
    	for(i = 0; i < 16; i++){
    		print_lcd_char(line1.characters[i]);
    	}
    	lcd_xy(0,1);
    	for(i = 0; i < 16; i++){
    		print_lcd_char(line2.characters[i]);
    	}
    
    	keypad_read();
    	delay(200);
    	line1.characters = "messaging       ";
    	line2.characters = "settings        ";
    	line3.characters = "contacts        ";
    	line4.characters = "dialler         ";
    	line5.characters = "help            ";
    
    
    void main_menu(){
    
    	int i;
    	for(i = 0; i < 16; i++){
    		print_lcd_char(lines[currentLine].characters[i]);
    	}
    	lcd_xy(0,1);
    	for(i = 0; i < 16; i++){
    		print_lcd_char(lines[currentLine+1].characters[i]);
    	}
    	
    
    	while(1){
    		key_functions(keypad_read());
    	}
    
    
    


    the idea here is that the strings line1 and line2 get printed to the LCD screen, but instead I just get a garbled mess of random characters.
    In the menu_setup function, the printing of the initial 2 lines, "welcome, press any key" goes off without a hitch, but when you try and get at the strings through the line array, in the main_menu function, it doesn't work.

    So basically, this comes down to me not knowing how arrays work in C, I think. But I have no idea what I'm doing wrong.
    The memory for the messages are removed once you leave the function menu_setup. The reason for this is you are constructing char* on the stack and than moving the stuct char* pointers to point to that memory. Instead you need malloc space for each char array and copy the contents. Also please remember to NULL terminate your char arrays.

    Ethea on
  • zeenyzeeny Registered User regular
    Meh, be like that.

  • bowenbowen How you doin'? Registered User regular
    Well first things first, your array is out of bounds. Array[7] means 7 units, so 0-6. Zeeny is right though, your issue here is a pointer one.

    You can eliminate all those LINE line1; and just reference them directly to be honest.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • jaziekjaziek Bad at everything And mad about it.Registered User regular
    edited February 2012
    The memory for the messages are removed once you leave the function menu_setup. The reason for this is you are constructing char* on the stack and than moving the stuct char* pointers to point to that memory. Instead you need malloc space for each char array and copy the contents. Also please remember to NULL terminate your char arrays.

    well I'll have to find another way to do it since there is no malloc for the board I am running this on.


    also, yeah I realised that mistake with the [7] as soon as I'd posted it. Changed that now.

    jaziek on
    Steam ||| SC2 - Jaziek.377 on EU & NA. ||| Twitch Stream
  • bowenbowen How you doin'? Registered User regular
    Since we're dealing with a fixed width screen you can deal with the stack exclusively and pointers aren't needed.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • bowenbowen How you doin'? Registered User regular
    typedef struct line { 
    	unsigned char characters[17]; 
    } LINE;
    
    //line array containing characters for a given line.
    LINE lines[8];
    
    //sets up the inital home screen and menu options.
    
    void menu_setup(){
    
    	//does C automatically add \0 ?
            lines[0].characters = "   welcome      ";
    	lines[1].characters = " press any key  ";
    
    //... so on and so forth
    
    

    I don't want to do your homework for you obviously. You can make your code much more elegant.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • bowenbowen How you doin'? Registered User regular
    Of course the old C gurus here like eccccooooooo will be better than me and probably point out the (at least) 3 things I just did wrong.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • EtheaEthea Registered User regular
    Ethea wrote: »
    jaziek wrote:
    how good are some people here at C? I see no reason why the following code shouldn't work, but it doesn't.
    
    typedef struct line { 
    			unsigned char* characters; 
    		     } LINE;
    
    //line array containing characters for a given line.
    LINE lines[7];
    	LINE line1;
    	LINE line2;
    	LINE line3;
    	LINE line4;
    	LINE line5;
    	LINE line6;
    	LINE line7;
    	LINE line8;
    
    //sets up the inital home screen and menu options.
    
    void menu_setup(){
    
    	
    	lines[0] = line1;
    	lines[1] = line2;
    	lines[2] = line3;
    	lines[3] = line4;
    	lines[4] = line5;
    	lines[5] = line6;
    	lines[6] = line7;
    	lines[7] = line8;
    	line1.characters = "   welcome      ";
    	line2.characters = " press any key  ";
    
    	index = 0;
    	recorded = 0;
    	hrzpos = 0;
    	vpos = 0;
    	  
    	int i;
    	for(i = 0; i < 16; i++){
    		print_lcd_char(line1.characters[i]);
    	}
    	lcd_xy(0,1);
    	for(i = 0; i < 16; i++){
    		print_lcd_char(line2.characters[i]);
    	}
    
    	keypad_read();
    	delay(200);
    	line1.characters = "messaging       ";
    	line2.characters = "settings        ";
    	line3.characters = "contacts        ";
    	line4.characters = "dialler         ";
    	line5.characters = "help            ";
    
    
    void main_menu(){
    
    	int i;
    	for(i = 0; i < 16; i++){
    		print_lcd_char(lines[currentLine].characters[i]);
    	}
    	lcd_xy(0,1);
    	for(i = 0; i < 16; i++){
    		print_lcd_char(lines[currentLine+1].characters[i]);
    	}
    	
    
    	while(1){
    		key_functions(keypad_read());
    	}
    
    
    


    the idea here is that the strings line1 and line2 get printed to the LCD screen, but instead I just get a garbled mess of random characters.
    In the menu_setup function, the printing of the initial 2 lines, "welcome, press any key" goes off without a hitch, but when you try and get at the strings through the line array, in the main_menu function, it doesn't work.

    So basically, this comes down to me not knowing how arrays work in C, I think. But I have no idea what I'm doing wrong.
    The memory for the messages are removed once you leave the function menu_setup. The reason for this is you are constructing char* on the stack and than moving the stuct char* pointers to point to that memory. Instead you need malloc space for each char array and copy the contents. Also please remember to NULL terminate your char arrays.

    My brain keeps forgetting all the rules about string literals in C, but they should exist through out the lifespan of the entire application. So I was wrong about that.

  • InfidelInfidel Heretic Registered User regular
    Aside from all the other issues, the main one with your corrupt output:

    line1 is a struct. When you go lines[1] = line1, you're doing a copy into the struct at lines[1]. You then set the contents of line1 to something.

    lines[1] has already been set and is a totally different struct. Modifying line1 won't do anything to it.

    These are not pointers. You shouldn't have lineX at all. Just use lines[x].

    OrokosPA.png
  • jaziekjaziek Bad at everything And mad about it.Registered User regular
    edited February 2012
    got it to work fine now. Got rid of line1,line2,line3 etc... since I realised they were pointless (and I was gonna have to delete them sooner or later anyway, there's only 32k of ram on this thing and it needs most of that to store sound data.)

    I didn't post the whole code because there are about 2-3 thousand lines (that I've written) and about 5 times that in the libraries that I'm using.

    FG5B4.jpg

    found a picture of what I'm working with.

    jaziek on
    Steam ||| SC2 - Jaziek.377 on EU & NA. ||| Twitch Stream
  • zeenyzeeny Registered User regular
    Infidel wrote: »
    Aside from all the other issues, the main one with your corrupt output:

    line1 is a struct. When you go lines[1] = line1, you're doing a copy into the struct at lines[1]. You then set the contents of line1 to something.

    lines[1] has already been set and is a totally different struct. Modifying line1 won't do anything to it.

    These are not pointers. You shouldn't have lineX at all. Just use lines[x].



    :D

  • jaziekjaziek Bad at everything And mad about it.Registered User regular
    Infidel wrote: »
    Aside from all the other issues, the main one with your corrupt output:

    line1 is a struct. When you go lines[1] = line1, you're doing a copy into the struct at lines[1]. You then set the contents of line1 to something.

    lines[1] has already been set and is a totally different struct. Modifying line1 won't do anything to it.

    These are not pointers. You shouldn't have lineX at all. Just use lines[x].

    Well when you say it like that....

    Yes the way I was thinking was that the = was making line[0] point to line1 ...

    Not entirely sure why I was thought that. Incredibly stupid of me. Maybe I'm just not switched on at all today, because that is a mistake that even I shouldn't be making.

    Steam ||| SC2 - Jaziek.377 on EU & NA. ||| Twitch Stream
  • zeenyzeeny Registered User regular
    edited February 2012
    You thought it because your typedef was blurring the datatype you are manipulating.....while structs are structs.

    zeeny on
  • bowenbowen How you doin'? Registered User regular
    I've always wanted to do stuff like this with hardware. Someday!

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • jaziekjaziek Bad at everything And mad about it.Registered User regular
    I've got 3 more weeks to finish off what needs to be done with this project. Ideally I'd like to get the USB port working to use it for mass storage, but I probably won't have time.

    Gotta be able to

    a) send and recieve SMS messages (I've almost finished this. Can recieve stuff, but can't send quite yet.)
    b) send, recieve and play ringtones
    c) transmit and recieve live voice data over the network.

    I already have the basic drivers for the network interface, adc and dac written, so from there it shouldn't be TOO hard...
    We're supposed to be using speex to encode our voice data for transmission over the network but nobody has been able to get it to work yet. Most of the information out there about speex is to do with using it on windows, which isn't really that helpful to us.

    Steam ||| SC2 - Jaziek.377 on EU & NA. ||| Twitch Stream
  • urahonkyurahonky Registered User regular
    That looks like a bomb... that's so awesome.

  • BarrakkethBarrakketh Registered User regular
    zeeny wrote: »
    Cantido wrote: »
    Oh my god, learning jQuery Mobile is so awesome, I think I'm gonna cry.

    I believe it was Crockford who said that JS is a language that programmers prefer "to use" instead of "to learn". I'm mentioning this simply because I haven't gotten the impression that you are comfortable with the language(from your posts so far), yet you are learning JQuery.
    Somehow, JS is the only language that's acceptable in. Nobody will advise you to start with zend, instead of php or plunge into web2py before having an understand of python, but hey, it's Js, it's all good!
    At least when it comes to jQuery you're not not somehow avoiding learning JavaScript, you're avoiding not having to learn/deal about the various incompatibilities between web browsers (or versions between the same browser) in how they handle DOM querying, transversal, and manipulation. In the case of jQuery UI/Mobile, that extends to learning an API for designing UIs primarily through JS.

    Your complaint sounds a lot like someone saying that because a guy wrote a program using Qt they aren't actually learning C++ in the process. It's more like they sure as hell aren't learning the Win32/MFC API, and good for them for avoiding as much of that nightmare as possible.
    bowen wrote: »
    urahonky wrote: »
    I think they finally added that post-update so we can actually go to the post that's being quoted now.

    There it goes. Weird.
    It was added to the board about a day ago, but before then End had a Greasemonkey script and I had my forum extension that added those to quote which is why you'd see them pop up every so often.

    Rollers are red, chargers are blue....omae wa mou shindeiru
  • zeenyzeeny Registered User regular
    edited February 2012
    Your complaint sounds a lot like someone saying that because a guy wrote a program using Qt they aren't actually learning C++ in the process.

    Yup. They aren't.

    Edit: And it wasn't intended much as a complaint, just an observation. The standard of JS code out there is lower than almost any other code I've seen. Probably even worse than PHP.

    zeeny on
  • bowenbowen How you doin'? Registered User regular
    jaziek wrote: »
    I've got 3 more weeks to finish off what needs to be done with this project. Ideally I'd like to get the USB port working to use it for mass storage, but I probably won't have time.

    Gotta be able to

    a) send and recieve SMS messages (I've almost finished this. Can recieve stuff, but can't send quite yet.)
    b) send, recieve and play ringtones
    c) transmit and recieve live voice data over the network.

    I already have the basic drivers for the network interface, adc and dac written, so from there it shouldn't be TOO hard...
    We're supposed to be using speex to encode our voice data for transmission over the network but nobody has been able to get it to work yet. Most of the information out there about speex is to do with using it on windows, which isn't really that helpful to us.

    Looks like speex can plug into stdin and stdout.

    I'm assuming your board has a way to change those? That'd probably let you stream in some fashion.

    transmit and receive live voice data might be a bit much if you want to use it like a telephone with speex. Push-to-talk would be easy... depending on the onboard memory that thing has, and if you limit how long someone can talk.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • EndEnd Registered User regular
    edited February 2012
    his environment probably doesn't have any existing concept of stdin and stdout (or files in general)

    End on
    I wish that someway, somehow, that I could save every one of us
    zaleiria-by-lexxy-sig.jpg
  • InfidelInfidel Heretic Registered User regular
    The use, not learn, statement is pretty apt.

    I know a lot of "standard" developers that end up taking on some JS/jQuery code and they get by fine for the most part. But they run into walls eventually.

    Usually when they need to grok what is going on with their frameworks and OO. Javascript is a prototype language for OO and a classical developer is dumbstruck by it. Their normal intuition and understanding fails them hard, and they often never even know what the hell a prototype-based language is or that Javascript is one.

    An example: Just explaining it on the bus with my roommate helped him a shit load. He didn't acknowledge it up until that point, he just had work to do and was busy plowing ahead. He got deep enough eventually that he was asking me "what the fuck is this and how is it even working, I don't get it."

    OrokosPA.png
  • bowenbowen How you doin'? Registered User regular
    edited February 2012
    Something like:
    freopen("/dev/audio","w",stdout);
    

    Then any bytes you send with printf, would get sent to your speaker instead, hopefully.

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • bowenbowen How you doin'? Registered User regular
    edited February 2012
    End wrote: »
    his environment probably doesn't have any existing concept of stdin and stdout (or files in general)

    You're probably right. That makes it way more complicated to use things like speex then. I'm assuming he's got another set of functions like "getSoundBuffer()" which will let him get something to pass to speex. At that point it'd be pretty easy to do. You're basically making a rudimentary ventrilo built into hardware.

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • InfidelInfidel Heretic Registered User regular
    bowen wrote:
    End wrote: »
    his environment probably doesn't have any existing concept of stdin and stdout (or files in general)

    You're probably right. That makes it way more complicated to use things like speex then. I'm assuming he's got another set of functions like "getSoundBuffer()" which will let him get something to pass to speex. At that point it'd be pretty easy to do. You're basically making a rudimentary ventrilo built into hardware.

    I hope you remember your DMA programming!!! :D

    OrokosPA.png
  • bowenbowen How you doin'? Registered User regular
    Infidel wrote: »
    bowen wrote:
    End wrote: »
    his environment probably doesn't have any existing concept of stdin and stdout (or files in general)

    You're probably right. That makes it way more complicated to use things like speex then. I'm assuming he's got another set of functions like "getSoundBuffer()" which will let him get something to pass to speex. At that point it'd be pretty easy to do. You're basically making a rudimentary ventrilo built into hardware.

    I hope you remember your DMA programming!!! :D

    I went to ITT, I was lucky to use C++ for data structures and not Java.

    But yeah that's a possibility I hope he doesn't have to do. This seems to be way above what an entry level class should be doing though. Maybe 2nd or 3rd year?

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • zeenyzeeny Registered User regular
    Infidel wrote: »
    The use, not learn, statement is pretty apt.

    I know a lot of "standard" developers that end up taking on some JS/jQuery code and they get by fine for the most part. But they run into walls eventually.

    Usually when they need to grok what is going on with their frameworks and OO. Javascript is a prototype language for OO and a classical developer is dumbstruck by it. Their normal intuition and understanding fails them hard, and they often never even know what the hell a prototype-based language is or that Javascript is one.

    An example: Just explaining it on the bus with my roommate helped him a shit load. He didn't acknowledge it up until that point, he just had work to do and was busy plowing ahead. He got deep enough eventually that he was asking me "what the fuck is this and how is it even working, I don't get it."

    Prototype based functional language with explicit binding, tricky scope and closures left and right.
    "Pfff, I don't need to read on that. It looks just like C. EASY!"

  • PhyphorPhyphor Building Planet Busters Tasting FruitRegistered User regular
    Infidel wrote: »
    bowen wrote:
    End wrote: »
    his environment probably doesn't have any existing concept of stdin and stdout (or files in general)

    You're probably right. That makes it way more complicated to use things like speex then. I'm assuming he's got another set of functions like "getSoundBuffer()" which will let him get something to pass to speex. At that point it'd be pretty easy to do. You're basically making a rudimentary ventrilo built into hardware.

    I hope you remember your DMA programming!!! :D

    He might be lucky to have DMA!

  • InfidelInfidel Heretic Registered User regular
    zeeny wrote:
    Infidel wrote: »
    The use, not learn, statement is pretty apt.

    I know a lot of "standard" developers that end up taking on some JS/jQuery code and they get by fine for the most part. But they run into walls eventually.

    Usually when they need to grok what is going on with their frameworks and OO. Javascript is a prototype language for OO and a classical developer is dumbstruck by it. Their normal intuition and understanding fails them hard, and they often never even know what the hell a prototype-based language is or that Javascript is one.

    An example: Just explaining it on the bus with my roommate helped him a shit load. He didn't acknowledge it up until that point, he just had work to do and was busy plowing ahead. He got deep enough eventually that he was asking me "what the fuck is this and how is it even working, I don't get it."

    Prototype based functional language with explicit binding, tricky scope and closures left and right.
    "Pfff, I don't need to read on that. It looks just like C. EASY!"

    Exactly, it's something that most developers think they can just power on through.

    Which works up until a point and then you're like "oh god fuck burn it down and start over." :lol:

    OrokosPA.png
  • bowenbowen How you doin'? Registered User regular
    It's like someone skipped like 30 years of computer hardware on this setup or something.

    Here's all this stuff, no DMA or anything... and I want you do to realtime voice compression with speex over a network.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • InfidelInfidel Heretic Registered User regular
    bowen wrote:
    It's like someone skipped like 30 years of computer hardware on this setup or something.

    Here's all this stuff, no DMA or anything... and I want you do to realtime voice compression with speex over a network.

    That's just fiddling bits, no problemo!

    You know how to negotiate ethernet and do encoding with an FPGA right?

    Well, that's more of a later years course...

    OrokosPA.png
  • bowenbowen How you doin'? Registered User regular
    Can you feel my hate yet Infidel?

    Also I think this is more of a dummy terminal than anything. It probably uses serial but uses the PC to do a bulk of it's processing (he mentions USB).

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • InfidelInfidel Heretic Registered User regular
    I imagine it isn't that low-level no. Just limited.

    OrokosPA.png
This discussion has been closed.