Skip to content

ViSi-Genie Magic: Arduino How to append to a File

Introduction

This codebase example presents a working application for demonstrating how to perform a file append operation. To understand this code example more quickly, please see first the code example:

ViSi-Genie: Magic: Arduino How to Read a File

This codebase example has two parts - the ViSi-Genie Magic project and the Arduino sketch. The attached ViSi-Genie Magic project is a modified version of the example project "FileAccess" found in Workshop4 Pro go to File -> Samples -> ViSi-Genie Magic(PICASO/DIABLO). FileAccess contains a working example of a Magic Object that handles and responds to file access instructions from a host. The Magic Object example can be edited and customised by users.

The ViSi-Genie-Arduino library, on the other hand, has been updated to include a function for sending messages to a Magic Object. The library also has a function for responding to messages from a Magic Object.

Note

Worskhop 4 PRO is needed for this codebase example.

Prerequisites

This codebase example assumes the reader can program the 4D Systems display module using Workshop4 IDE ViSi-Genie environment. Beginners are advised to read the following aplication notes.

Instructions

Below were the steps involved in creating this codebase example:

  1. Compile the attached ViSi-Genie Magic project and upload the program to a uLCD-32PTU or any 4D Systems displays with PICASO, DIABLO-16, PIXXI-28 and PIXXI-44 graphics processors. Copy the supporting files to a uSD card mounted to the PC. Properly unmount the uSD card from the PC and mount it to the target display.

    Note

    1. The difference between the attached ViSi-Genie Magic project and the FileAccess example in Workshop4 Pro is the inclusion of delays into the Magic Object code of the latter. Please see the image files "GenieMagicObjectCodeInsertedDelay_1.png" and "GenieMagicObjectCodeInsertedDelay_2.png" inside the attached folder "images". Users are free to customise further the code for MagicObject0 to suit their application.

    2. The file "data.log" needs to be copied to the uSD card of the display module. This filename is hard coded in the attached sketch. Open the attached folder "uSD card file" and copy the content to the uSD card.

  2. Compile and upload the attached sketch to an Arduino Uno. The sketch uses a software serial port for the display. See the program flow summary further below.

  3. Properly connect the Arduino Uno to the target display. See the application notes ViSi-Genie: Connecting a 4D Display to an Arduino for this.

  4. Screenshot image files of the expected serial terminal output are inside the attached folder "images".

    • expectedOutputSteps1and2.png
    • expectedOutputStep3.png

If without an Arduino host, you can still run this code example by performing step 1. Then use the GTX tool to send and receive messages to and from the display module.

Below is the logic used in writing the Arduino sketch for this code example.

Process:
Step 1. Do a file read and print the data.
Step 2. Do a file append.
Step 3. Do a second file read and print the data (to prove that step 2 was successful).

Note

  1. The first command sent to the display is a file read command, and it is assumed that the first file read reply received is a reply to the first command sent.
  2. This process will run once only at the start.

setup:

firstReadDoneFlag = false;      //if this flag is set, print a success message for first file read in the 50-ms loop
appendDoneFlag = false;         //if this flag is set, print success message for file append in the 50-ms loop
secondReadDoneFlag = false;     //if this flag is set, print a success message for second file read in the 50-ms loop
processDoneFlag = false;        //flag to indicate if the process is ended

send the first file read command

mainloop:

    DoEvents(): this executes myGenieMagicHandler(...){...} and other internal processes
        if reply to first file read command is received && processDoneFlag == false
            firstReadDoneFlag = true
        else if reply to file append command is received && processDoneFlag == false
            appendDoneFlag = true
        else if reply to second file read command is received && processDoneFlag == false
            secondReadDoneFlag = true


    50-ms loop: this is the conditional block "if (millis() > lastRun){...}" inside the main loop
        if firstReadDoneFlag == true
            print success message for first file read
            send file append command
            firstReadDoneFlag = false (to make this case run once only)

        else if appendDoneFlag == true
            print success message for file append
            send second file read command    
            appendDoneFlag = false (to make this case run once only)

        else if secondReadDoneFlag == true
            print success message for second file read
            print "process ended"
            secondReadDoneFlag = false (to make this case run once only)
            processDoneFlag = true (to make the whole process run once only)

back to main loop

Screenshot images of the expected serial terminal output.

Expected Output Step 1 and 2

expected-output-1-2


Expected Output Step 3

expected-output-3

Attachment

Project File