Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, 9 October 2014

Testing Arduino Code

There are a couple of ways that you can test your Arduino code, not least of which is compiling your sketch and then uploading it to your microprocessor and making sure it runs OK … however, that doesn’t help in revealing any problems in the code.

If there are major problems then, sure, the IDE will whine and you’ll hear about it. But what if there’s a silent problem in your code that you can’t necessarily find sooner?

I’m currently looking at using a C language IDE (CodeLite) as a testing ground for my code. I haven’t gone too far yet, so there are likely to be some issues (for instance, the byte data type is missing in GCC … apparently, so I’m using the char data type instead).

My thinking is that I should be able to use the C IDE to reveal any problems in my code by sending the data that I want to monitor to stdout and use stepping to inspect the values and variables as it goes along.

Other things to consider … a standard C application starts with int main() rather than void setup() and you have to explicitly define and call the loop() function, oh there are other differences that I’m sure to find along the way, but for simply testing function blocks to make sure that the data that my function is sending (and to a lesser extent, the function structure and flow), can be inspected discretely.

Playing with my Shift Register code, I created the following main.c file

code_grab

and then I ran the code …

cmd_grab

showing that the values being passed are correct.

I can also create a couple of different functions that do the same so that I can then have a look at them from an efficiency, memory and timing perspective to decide on which way is the best for my application.

Anyway, it’s all learning, poking and prodding. I’m not sure how others approach testing their Arduino code … but then, I’m a software tester by profession.

Tuesday, 11 December 2012

Grid on a .NET form

I needed to place a grid on the main form in a .NET application, the grid is a height field that I am generating for a random terrain generator.

When the form is loaded, the nMap class is instantiated with some definition stuff. The nMap class is where I process the grid of data representing the map cells. There is some processing stuff in the class that sets the RGB values for each of the cells.

Public Sub drawGrid(Optional ByVal minLines As Boolean = True, Optional ByVal majLines As Boolean = True)
        Dim cellRef As Long = 1024
        PictureBox1.Image = New Bitmap((CInt(nMap.width) * 10) + 20, (CInt(nMap.height) * 10) + 20)
        Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
            Dim grayPen As New Drawing.Pen(Color.Gray)
            Dim blackPen As New Drawing.Pen(Color.Black)
            'cellcolour
            For i As Long = 0 To (nMap.width * nMap.height) - 1
                cellRef = i
                Dim nColour As New Color
                'Dim nAry As cellRGB = nMap.grid(i)
                'nColour = Color.FromArgb(nAry.R, nAry.G, nAry.B)
                Dim nAry() As String = nMap.grid(i).ToString.Split(":")
                nColour = Color.FromArgb(nAry(0), nAry(1), nAry(2))

                Dim nBrush = New SolidBrush(nColour)
                Dim X1, Y1 As Long
                X1 = 10 + ((cellRef - (Int(cellRef / nMap.width) * nMap.width)) * 10)
                Y1 = 10 + (Int(cellRef / nMap.width) * 10)
                g.FillRectangle(nBrush, New Rectangle(X1, Y1, 10, 10))
            Next

            Dim x As Integer
            Dim y As Integer

            Dim intSpacing As Integer = 10
            x = PictureBox1.Width

            'gridlines - minor gridlines
            If minLines Then
                For y = 10 To PictureBox1.Height - 10 Step intSpacing
                    g.DrawLine(grayPen, New Point(10, y), New Point(x - 10, y))
                Next
                y = PictureBox1.Height
                For x = 10 To PictureBox1.Width - 10 Step intSpacing
                    g.DrawLine(grayPen, New Point(x, 10), New Point(x, y - 10))
                Next
            End If
            'gridlines - major gridlines
            intSpacing = 100

            If majLines Then
                For y = 10 To PictureBox1.Height - 10 Step intSpacing
                    g.DrawLine(blackPen, New Point(10, y), New Point(x - 10, y))
                Next
                y = PictureBox1.Height
                For x = 10 To PictureBox1.Width - 10 Step intSpacing
                    g.DrawLine(blackPen, New Point(x, 10), New Point(x, y - 10))
                Next
            End If
        End Using
    End Sub

The Major and Minor gridlines can be turned on and off as suits. This happens in the redraw function.

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.