Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | CRPS Treatment | | About Us
 

Forward Logo (image)      

pfodParserC Library
for PIC and other Non-Arduino based micros

by Matthew Ford 1st August 2016 (originally posted 25th July)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.

pfodParserC Libraries V1.4
(Protocol For Operations Discovery)
Libraries for PIC and other Non-Arduino microprocessors.

This page describes the pfodParserC library for Non-Arduino microprocessors. This library supplies the support files that when combined with the output from pfodDesignerV2 will let you parse pfod messages sent from the pfodApp.

The code generated by pfodDesignerV2 V2.0.2181+ needs pfodParserC library V1.4+. The supporting pfodParserC files are here. See below for how to use and configure them.
See pfod™ for Non-Arduino based microprocessors for further information and detailed examples using MPLAB

The Arduino versions of these libraries are also available.

Installation and Configuration:

a) Then download the pfodParserC.zip and unzip into you C/C++ project directory. These files are compatible with C++ compilers.

b) Modify the pfodParserStream.c file to connect the parser to your chip's I/O. pfodParserStream.c has four (4) methods to read and write chars to the I/O and to test if characters are available to read or write. The included sample methods are for use with MPLAB's UART code (PIC18F14K22 and similar)

c) The included sample pfodMenu files just display an empty menu. Use pfodDesignerV2 to design your own menu and generate the pfodMenu.c and pfodMenu.h files.

d) If you C compiler does not support uint8_t (C99) then add this typedef to the header files
typedef unsigned char uint8_t;

pfodParserStream.c

Sample implementations are shown below and are included in the pfodPareserC.zip. These methods are for use with MPLAB's UART code. You need to modify them to suit your own I/O setup.

/**
 * return space available for write();
 * @return bytes free
 */
size_t pfodParser_TXfree(void) {
    return eusartTxBufferRemaining;
} 

int pfodParser_write(uint8_t c) {
    if (!pfodParser_connected) {
        return 1;
    }
    while (pfodParser_TXfree() == 0) {
        // spin
    }
    putch((char)c); // spins if no space available
    return 1;
}

size_t pfodParser_RXavailable(void) {
    return eusartRxCount;
}

int pfodParser_read(void) {
    while (pfodParser_RXavailable() == 0) {
        // spin
    }
    return (int)getch(); // spins if not data available
}

Calling the pfodParser

In you main file, include pfodMenu.h and add a call to
pfodParser_setup();
and in your processing loop call
pfodParser_parse();

e.g.

#include "pfodMenu.h"
/*
   Main application
 */
void main(void) {
    // Initialization
    pfodParser_setup();
    while (1) {
        pfodParser_parse(); // parse cmds, set vars, send menus
       // your other code here.  You can also access the values your menus have set.        
    }
}

Library Reference:

pfodParser

Description
pfodParser for Arduino, Parses commands of the form {ver : cmd} or {ver : cmd ` arg1`arg2 ... } or {ver : cmd ~ arg1 ~ arg2 … }
The ver : and the args are optional.

This is a complete parser for ALL commands a pfodApp will send to a pfodDevice
pfodParser uses about 84 bytes of RAM. Program usage depends on compiler (about 2K using MPLAB's free, non-optimizing, XC8 compiler).

Methods

Public Methods and Variables

void pfodParser_setup(void)initialized the parser and sets it version. Intializes parser's global variables. Call this from your setup code.

void pfodParser_parse(void)reads available bytes from input via pfodParserStream methods, parses commands, interprets the commands and sets the global variables. Call this in your processing loop

uint8_t pfodParser_sendDataFlag – uint8_t variable, set this to true in your code to send a CSV data record on the next call to pfodParser_parse(). Set this in your code, when you want data to be sent.

uint8_t pfodParser_connected – uint8_t variable, set to true when {.} has been parsed and set to false when {!} is received.

Other variables are created by pfodDesignerV2 to access the data being sent to and from pfodApp, plot_1_var, cmd_A_var, etc

Utility Public Print Fns
These functions provide a minimal set of print capability for sending menus and responses. All of these methods send nothing if the pfodParser is not connected and block if there is not enough space in the outgoing buffer. The pfodParser_TXfree() can be used to check the available space in the TX buffer

int pfodParser_println(void)send \r\n to underlying TX buffer, does not send data if pfodParser is not connected.

int pfodParser_printCh(char c)sends a char c to the underlying TX buffer, does not send data if pfodParser is not connected.

int pfodParser_printStr(const char *str)send the string to the underlying TX buffer, does not send data if pfodParser is not connected.

int pfodParser_printLong(const long l)converts a long to a string and send it to the underlying TX buffer, does not send data if pfodParser is not connected.

Private Methods and Variables
These methods are used to parse the commands. You generally will not need to use these.

void pfodParser_pfodParser(const char* vers)initialized the parser and sets it version

uint8_t pfodParser_parse_RX() – reads available bytes from input via pfodParserStream methods and parses commands.
Return: return 0 if complete message not found yet else return first char of cmd when see closing } or ignore msg if pfodMaxCmdLen bytes after {
On non-zero return args[] contains the cmd null terminated followed by the args null terminated argsCount is the number of args
Check pfodParser_isRefresh() to see if this is a refresh command.

uint8_t pfodParser_isRefresh() – returns uint8_t, true or false. Returns true if last command started with {: indicating pfodApp is requesting a refresh of the menu associated with the cmd and the requested version matches the parsers version. See the pfodSpecification.pdf for more details

uint8_t* pfodParser_getCmd() – returns pointer to null terminated parsed command.

uint8_t* pfodParser_getFirstArg() – returns pointer to null terminated first arg if any, else if no args returns pointer to null.

uint8_t* pfodParser_getNextArg(uint8_t*) – returns pointer to start of next arg or pointer to null is reached end of args. Need to call getFirstArg() to get uint8_t* to pass to this method.

uint8_t pfodParser_getArgsCount() – returns the number of args in the last message parsed.

void pfodParser_parseLong(uint8_t* idxPtr, long *result) – parses a null terminated bytes into a long, returns pointer to next byte after terminating null. Long result is returned via long *



Examples

pfod™ for Non-Arduino based microprocessors contains a detailed PIC18 MPLAB example to turn a LED on and off and send and plot data on your Android.



AndroidTM is a trademark of Google Inc, For use of the Arduino name see http://arduino.cc/en/Main/FAQ


The General Purpose Android/Arduino Control App.
pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.


Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2024 Forward Computing and Control Pty. Ltd. ACN 003 669 994