Tuesday, May 31, 2011

tired

I'm just too tired to get any real coding done. I read a little bit about SDL's audio capabilities and realized that it's going to be an uphill battle getting my head around how that works. Video is almost trivially easy in comparison - or at least it's easier to understand because imagining an array where every element is a pixel is easier than whatever mess audio uses.

I also did a little bit of reading trying to see if anyone else solved how to handle dates and making a histogram of how many times a thing happened on a date in Python. There are lots of examples but I think Python programmers are even more sadistic than C programmers when it comes to trying to do complex things in as few lines as possible. It just makes for unreadable code that you can't learn from.

Friday, May 27, 2011

Python project

We're in the middle of a transition at work so I've been without an "official" support case database/tracking solution since the beginning of 2011. I've been keeping a spreadsheet instead with six columns: Date added, University, Contact, Issue, Notes, and Order/RMA number. I don't track when the case is closed (although I'd like to) and I have no priority index (but that would be nice).

Since the start of 2011 I've entered about 300 cases - the "true" number is actually higher because I don't enter in things that are knocked out in one phone conversation. Simple stuff that just involves sending a link to a document or telling someone where a button is doesn't get added because I'd spend half my day updating the spreadsheet! Anything that I have to sit and ponder on or do rigorous recreations of a problem get on the list, and anything that is a brand new problem gets added so that I can remember it if it comes up again.

I wanted to attempt to do some basic metrics like cases per month or per day and I think that I can use Python to do that fairly easily. There are several solutions for reading .xls files and there are certainly some functionality for parsing dates and binning per month/day (and maybe even dumping out an .xls files of this data).

I'll put that on the list for "someday" whenever the apartment mess and wedding stuff gets all handled.... so maybe September 2011? Woof.

Monday, May 23, 2011

UDK

I wish I had time to mess with the Unreal Development Kit or the one about to be released by Crytek. Even some time to make Portal 2 levels would be nice. I loved that stuff when I was younger.

Sunday, May 22, 2011

design patterns

I feel like I should pick up a book on common design patterns or maybe any kind of text with explanations of the thought process that real programmers take when dealing with medium size projects. The intense pondering I've been doing about this file reader is useful but I wonder if I'm missing something obvious that's a better way of doing it. Matt revealed the model-view-controller paradigm to me recently and that more or less blew my mind. Makes me wonder what else is out there.

Saturday, May 21, 2011

netbook

Today's exercise was done on the Ubuntu netbook while at Ana's mom's house. I brought both the laptop and the netbook and they're using the laptop to do wedding invitation stuff.

The point here is that I forgot how much I like CodeBlocks under Ubuntu. XCode is intimidating and the OSX build of CodeBlocks is known to be a bit wonky. I use XCode anyways but sometimes it does things or adds files to a project and I don't know why. There isn't a project template for "simple uncomplicated console application". Even getting to the console output is two clicks away and oddly decoupled. I've considered going back to the command line but I am getting spoiled on auto-complete and function prototype reminders - especially because the structures involved with the file reader have a dozen or so members and it's hard to remember exact spelling and capitalisation.

Truth be told I'd be better of for having to do things all through the terminal (well I'd use TextWrangler or Notepad++ for syntax highlighting). I suppose what I mean to say is I need to keep it down to a few hand-made files and compiling at the command line. I abandoned that method on the netbook after a while since it wasn't convenient to quickly change one thing and recompile. Might not be as true on the laptop.

Alloc so far so good

This worked the first time which is a good sign that some stuff I see over and over again but don't actually use sticks!

The output is: Storage is at 0x808e008 and is size 4

...and the address naturally changes run to run.
So the program for getting data out of a file will return an address of where the data is stored. That's what malloc is for right? Put data into the heap and that data remains until freed (oh snap I just realized I never freed my array in the program above...) regardless of where it was allocated (like it won't go out of scope unless the variable storing the address goes out of scope before being passed out of the function).

This has the double benefit of delivering the data and allowing for knowing how many elements is in the stored space (proved with my use of sizeof).

So, hooray.

final decision

The final decision is to make a function that simply returns a pointer where some allocated memory with the data is.

Friday, May 20, 2011

even better

I keep thinking of better ways to write this program.

Like maybe what my file reader (the part of the program dealing with extracting the blocks) should shuffle the blocks out to some different program. Or maybe buffer them somewhere else?

Maybe I should stick to what it's at right now. Brute force and bulky but functional?

reusability

Programming before sleeping results in code dreams.

I realized last night (err... in a dream) that I'm copy/pasting a lot of code. Every function in the toolkit I'm making has a large chunk of semi-identical code for opening a file and seeking/reading through it. The only difference is that what particular piece of data is skipped over or read changes. I considered making one function for everything and having the lines for dumping the data out be keyed to a bunch of conditions you pass to the function, but that started to look obfuscated.

Is this what OOP is for? Could I have made one class for reading data and then have the subclasses be slight modifications? Is that the right word? I dunno.

Ok, so one way I could have done it was have one block of code in a function for reading out everything, but the difference between functions is the return values (or rather what data gets dumped to previously allocated storage).

I decided to have the size functions return the number of data instead of the total size in bytes. This way you have a number to use as your index when scanning through the returned data.

EDIT: I think I hashed out in the shower how to handle this with ONE function that goes through the file. The trick is passing the function a pointer to other "which data to get" functions. I haven't completely worked it out yet but it's promising. If I can write a program that does memory allocation AND function pointers in one go I'll be very happy!

Thursday, May 19, 2011

specificity

On a whim I once again re-wrote some of my file reader. This time I'm making a library of functions that do specific things. I have one function finished and another half-started.

The first function scans through a file and reports back how many timestamps there are for a specific unit on a specific channel. You pass the function a string (the filename), a channel, and a unit. With this data I want to then allocate some memory to hold an array of all the timestamps for that unit/channel.

The second function will actually pass the timestamps to that allocated memory.

I can't decide if the first function should just report back the number of timestamps or if it should just go ahead and report back the number of bytes that need to be allocated. There are benefits to both methods.

Why allocate on the fly? Each timestamp is a long integer. There might be a dozen in a file or there might be several hundred thousand. This seems prime for learning me some malloc.

So this library would be used with all the different data types in the file eventually and would be used as follows:

1) Call the function that determines how big the data is in the file for that channel/unit, or digitized waveform channel, or event.

2) Allocate space for the data.

3) Call the function to get the data (passing it a pointer to the space allocated as well as the channel/unit/filename/etc).

This won't be too particularly useful in real life but it is good practice for making a program that does specific things instead of a kitchen sink program that I hand-modify every time I need it to do something else.

Practice is practice.

Some summer goals

I have to limit my expectations for the summer. Getting married is taking up all of my free time. I am hoping that between late August and December I'll make lots of progress but until then I'm stuck only being able to squeeze in maybe 30 minutes a week actually coding.

A reasonable goal that will unlock some new projects is to get a handle on dynamic and shared libraries. I've used shared libraries that I like to during compiling, but only because I'm following instructions - not because I know what it's doing.

I think I should make a simple function that just adds two numbers together and try to put that into a .dll and then write another program that dynamically links to that .dll and can use the function. The examples I've seen so far use really unfamiliar data types and macros but it might be Windows-specific stuff that I wouldn't need. I dunno - it looks scary.

click

I think I wrote about this a few months ago, but I had more thoughts on it. I wrote a program at work to play a click whenever certain things happened in the data acquisition system. These things happened fast - potentially every 800 microseconds although realistically it's not that fast. Maybe 4-5 milliseconds at the fastest? I should look. The point is that the Windows function I was using to play the sound was slow and calling it faster than it could respond created unpredictable results - that is to say I'd try to play two sounds before the first one was done playing. It SHOULD have supported this (it had a flag you could pass to allow overlapping) and to some extent it worked but the behavior was inconsistent. I wrote the same program in Matlab with Matlab's function for playing sound and it behaved rather well.

This kind of irritates me because I had expected the C program to perform better. The bottleneck is the generic PlaySound() function I was using (part of the Windows SDK). I want to rewrite the program with SDL's sound playing libraries.

There MIGHT be a chance I'm just doing something stupid that's making it slow. Oh, the other crappy thing is that I can't just play a click - I had to make a .wav file of a 0 to 1 edge to be my click. I spent forever trying to figure out why it wasn't working and it turns out my .wav file was too short. I had to play "guess and check" to see how short I could make it before it stopped working. The minimum sample size wasn't documented anywhere I could see. I forget what the end result was.

Monday, May 16, 2011

ah, tech

I was pondering updating my Ubuntu install that I run through VirtualBox to 11.04 and debating the consequences. Then I remembered that it's a virtual install... I can either take a snapshot and then roll it back to that snapshot or just install another Ubuntu OS on the side. It's a freaking playground with zero consequences (well.... disk space).

Kind of makes me wish I had a spare XP license sitting around. Wait how does that work with virtual installs?

200

200 posts, cool!

I did some more of the GTK book. In the spirit of full disclosure I skipped a minor part about packing buttons in a vertical pane one way instead of the other. It seemed somewhat redundant. I have a feeling the self test part of this chapter will require that knowledge (if the prior chapter was any indication of how those tend to go). Anyhow, section 3-3 was taking a gtkcontainer and packing in "panes" separated by a vertical line that you can grab to resize the two spaces. Cool stuff (not screenshot-worthy, however).

The next section is about tables and I think this will really get me going. Finally it's getting into laying things out and making something that looks like a UI.

EDIT: Ok, screenshot: http://i.imgur.com/6QvgV.png

Friday, May 13, 2011

Old problems

I had a funky dream last night about taking a test in a math class and realizing I didn't remember anything. That basically sums up my college experience in mathematics but it shook me up a bit and got me thinking. I have an old calculus book sitting here and I wonder if it would be worth my while to flip through it a bit. I've toyed with the idea of tackling other topics I've struggled with in my learnin' years. My programming has slowed down because I'm doing enough Matlab at work to keep me from not wanting to mess with code at home.

If I remember I'll take the book home (it's sitting up here at the office for some reason) and see about it.

Wednesday, May 11, 2011

29

I should change the above description of the journal because I'm not 28 anymore. Also, the terms of the project, to be C literate, isn't very good. I consider myself C literate now in the sense that there aren't a lot of functions/syntax/words/operators that are unfamiliar to me. The knowledge I lack is comprised of advanced data structures, the mathematical rigor of optimizing code (Euler project showed me that), and the finer points of WHEN to use advanced concepts. It's like I know all the words in the English language but can't write above a third grade level. I can get the job done but it's not going to be elegant or optimized.

Not that I'm complaining. I've gotten pretty far in the past year.

burp

I've been doodling a bit in SDL but nothing post-worthy. It's mostly just little modifications to the example program so that I can figure out what controls what. I'll back it up and start slow because I don't want any of the functions for initializing the system to be a mystery.

I really need to get through more of the GTK book so that I can write a few test programs at work.

Thursday, May 5, 2011

SDL, now what?

Ok, so I want to go from quick and dirty tech demo I copy/pasted from a website to something I understand. The website I got it from has several tutorials that actually spell out the data types pertinent to manipulating pixels. That's cool, but I wonder if SDL has primitives (boxes, circles, triangles, etc) so that not everything is so manual. Actually it would be slick to make my own functions for primitives.

Here are the next steps as I see them.
1) Get a screen up (640 x 480) that is a solid color.
2) Get a screen up that alternates from red to blue to green and back (every X milliseconds or by keypress or something).
3) Figure out how coordinates work and make half the screen red and half the screen green.
4) Make a tiled pattern (this is working towards the Wumpus graphics output) and figure out how to address the screen on a tile by tile basis - like tell it that tile x:3 y:5 should be blue - the function should figure out which pixels that tile is and just change that one tile.
5) By this point it should be a matter of taking the code that can address a grid of tiles and then be able to define what those tiles are (a solid color or picture) and call it the game board.

cast and shift

I have a minority of users I support that are the programmers/techs in the lab. They're the genesis of the various programming-related support I do that has increasingly been things I can handle myself and not have to automatically pitch to a programmer here.


The data acquisition system has a clock that only resets when you cycle the power. I don't remember how many bits the clock is (EDIT: 40 bits) but it will roll over after about a month of being on (and deity help you if that happens during a recording). Anyways, the actual time recorded in the file isn't this clock value because a "start" time is noted and considered to be 0 and all subsequent acquisitions are subtracted against the start value to get the correct relative time. The SDK that can access data on the fly doesn't have access to that information and so it returns the actual value of the timer. Someone wrote in wanting to know if the non-adjusted times were kept in the file, so I modified my file reader to pitch the clock values of the data at me. It was tricky and here's why.

The file format keeps the time of each data block in two variables representing the upper 8 bits and one more variable representing the lower 32 bits (5 bytes total). This was apparently to save space and not use two 32 bit variables. The high byte is stored in an unsigned short, and the other 4 bytes is in an unsigned int. To get this into one variable the following is done:

long int timestamp = ((long int)datablock.upperbyte << 32) + (long int)datablock.lower4bytes;

I take my one byte and cast it to a bigger variable that can hold 5 bytes and then shift that byte to the left 32 bits (4 bytes). Then I add to that my lower 4 bytes. The 5 bytes are recombined.

It's a minor annoyance but it's the first time I did any shifting and I figured I should make a note of it.

Wednesday, May 4, 2011

I think we have a winner

http://www.friedspace.com/cprogramming/sdlbasic.php


The C code linked on that page makes a whole mess of pretty colors. Straight SDL, no OpenGL. I think I can get a good handle on this and make it work for my nefarious purposes.

SDL

I got SDL installed and it seems to play nice with XCode. The basic "pop up a window/Hello world" program it defaults to works but a few warnings about things not in my user directory pop up. I didn't spend a lot of time messing with it. I'm still not comfortable with XCode when doing non-strictly-C stuff. The basic SDL project has a handful of framework files and some Cocoa/ObjC stuff that has to be there (described as "glue code") which somewhat irks me because I hate not knowing what things are doing. GTK was pretty straightforward compared to this.

Next I guess I'll try to find a good tutorial source.

misc

I used a bit of C programming a work yesterday. There is a C/C++ SDK for accessing the data coming into the system on the fly and someone was saying one of the functions for returning the currently selected (in the UI) channel wasn't working. I was able to make a very small (~12 line) program to test that function myself. Turns out it doesn't work in some situations, but it's a known limitation that doesn't have a workaround. I wouldn't have been able to do that even a few months ago - not because it was difficult code (it's just a few lines and a function that returns an int) but because it requires linking to a library which up until recently I wasn't sure how to do. I'm still a bit shaky and I let the IDE do the hard part, but it's still more advanced than what I could do before.

Monday, May 2, 2011

QSort followup

Props to Matt for pointing out that stringarray[i] is the same as saying *(stringarray+i).

There is another point I needed to make about some confusion I was having, but I figured it out and now I don't remember why I was confused.

another project idea

I have more ideas than time/energy to do them.

It would be cool to make a "Life" clone. I've somewhat worked out in my head how to do it, but the edge cases (literally, the cases of the cells around the edges) don't work in my head as I'd want them to. I'll draw it out sometime this week.

I've come up with a roadmap of sorts to Wumpus but it seems useless to write it out without even picking a graphics library to work with. I'm very tempted to just use OpenGL since there is a mountain of tutorials. SDL is a close second but it seems that OpenGL+SDL is a very common cross-platform method with SDL handling input/timing/audio and OpenGL handling video.

You know what? How about this. Put Wumpus away for a few weeks and just see if I can install and compile a few OpenGL versions of "hello world" and see how I feel about it.