vsprintf in PROS?

I’m working on creating a library of LCD functions and I’d like to be able to pass format instructions like printf(char * fmt, …) in my function. Rather than reinvent printf, I’ve found vsprintf to pass the variable arguments list to the “printf”-type function. However, including <stdio.h> breaks the PROS API, which I expected considering a lot of the stdio functions have been rewritten. Any thoughts on how to get vsprintf to work or an alternative method? I’ve thought about copying an implementation of vsprintf, but it appears to be a couple thousand of lines code.

bool printText(char * stringFormat, textJustifications justification, unsigned char line, ...)
{
	va_list args;
	va_start(args, stringFormat);
	char *string;
	vsprintf(string, stringFormat, args);
	va_end(args);
	.
	.
	.
}

My previous method of accomplishing a formatted string was a bit more tedious where printText took a string rather than string formatting instructions.

char *str;
sprintf(str, "hello world %d", 10);
printText(str, Left, 1);

and I’d like to simplify to:


printText("hello world %d", Left, 1, 10);

There was a post here that touched on this issue.
https://vexforum.com/showpost.php?p=358272&postcount=285

Most of us use a derivation of the code created by Georges Menie when using printf in embedded systems. Take a look at what I did for ConVEX, it does have a lot of stuff specific to my OS and also workaround for the cortex serial channel issues (ie. throttling data rate) but the core of the code is Goerge’s.

https://github.com/jpearman/convex/blob/master/convex/cortex/fw/vexprintf.c

and a link to his website.
http://www.menie.org/georges/embedded/#printf

Go figure, you guys have already talked about this. :slight_smile: Thanks for the insight! If what I gather from Jabibi’s post is correct, vfprintf should be included in PROS (but not vprintf). I tried searching for vfprintf and can’t find it. I think that your response to Cody hit the issue pretty squarely over the head. I don’t think I’d be able to switch to anything else since we’re already pretty deep into the year, and switching entire libraries for a convenience factor such as this one is a bit overkill, and rewriting some of these functions are bit too far in the frontier (heh) for me, still.

I have not touched PROS for a while (or ConVEX for that matter, been far too busy). Let me see what I can figure out next week.

Elliot - is this for use with the Vex serial LCD? If so have you looked into the lcdPrint() function included in the PROS API? link


void lcdPrint ( FILE * lcdPort,
	unsigned char 	line,
	const char * 	formatString,
 	... 
)

Yes, this is for the LCD display and yes, I’ve seen lcdPrint. Currently, I’m basically writing a trumped up lcdPrint that has text justification and scrolling (if required), but makes the assumption that the LCD Display is on UART1, which is fine for my robots. I’ll need to figure out the final string that’ll be displayed so that I can manipulate it to determine if it needs to scroll and/or where to put it on the LCD display, if the text justification isn’t left.

I created a crippled version of vex_printf from my ConVEX code, it’s not thread safe etc. I will post it somewhere tomorrow when I’ve had a chance to test it. It only implements sprintf and vsprintf (as vex_sprintf and vex_vsprintf) but should be ok for what you need as long as you only call it from a single thread. You could make it thread safe if needed but I don’t really feel like looking up the PROS API for semaphores or mutexs tonight.

I uploaded something that may do what you want here.
https://app.box.com/s/mnt61fni79axftib5nsl

Simplified code based on vex_vsprintf from ConVEX.

The code I used to test it was. I assume you really want much more than left and right justification because it’s unnecessary for that.

/** @file opcontrol.c
 * @brief File for operator control code
 *
 * This file should contain the user operatorControl() function and any functions related to it.
 *
 * Copyright (c) 2011-2014, Purdue University ACM SIG BOTS.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Purdue University ACM SIG BOTS nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL PURDUE UNIVERSITY ACM SIG BOTS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Purdue Robotics OS contains FreeRTOS (http://www.freertos.org) whose source code may be
 * obtained from http://sourceforge.net/projects/freertos/files/ or on request.
 */

#include "main.h"
#include <stdarg.h>
#include "vexprintf.h"        // header


typedef enum {
    kTextLeftJustified,
    kTextRightJustified
}textJustifications;

bool
printText(textJustifications justification, unsigned char line, char * stringFormat, ...)
{
    char string[128];

    va_list args;
    va_start(args, stringFormat);
    vex_vsprintf(string, stringFormat, args);
    va_end(args);

    switch( justification )
        {
        case    kTextLeftJustified:
            lcdPrint( uart1, line, "%-16s", string );
            break;

        case    kTextRightJustified:
            lcdPrint( uart1, line, "%16s", string );
            break;
        }
    return(true);
}

void operatorControl() {
    int     counter = 0;

    lcdInit(uart1);
    lcdSetBacklight(uart1, true);

    while (1) {
        printText( kTextLeftJustified,  1, "Hello %d", counter );
        printText( kTextRightJustified, 2, "Hello %d", counter );
        counter++;
        delay(20);
    }
}

I haven’t had time to test it yet (finals next week), but I wanted to preemptively say thanks for sharing this. The library will be doing more than just left and right justification. (center justification, too! \s)