/*
 * picoph.h
 * Library for interface with picopH
 *
 *  Created on: Jul 28, 2022
 *      Author: irene
 */

#ifndef PICOPH_H_
#define PICOPH_H_


#include "system.h"
#include "uartstdio.h"
#include "user_io.h"
#include "uart3.h"

#define PICO_BAUD 19200
#define PICO_STARTUP_MS 1000 // time to wait after startup before sending command. IH 8/19/22 900 ms does not work, 1s seems to work
#define PICO_TIMEOUT_MS 800 // timeout for receiving command after sending one.  OCR is 1600; down to 200 ms works
#define PICO_GETMEASCMD "MEA 1 47\r"

/*
 * picoph.c function prototypes:
 */

// initialize and uart wrapper functions
void picoph_init(void);
void picoph_open(void);
void picoph_close(void);


unsigned char picoph_getc(void);
void picoph_putc(unsigned char c);
int picoph_rxBytesAvail(void);

void picoph_flushRx(void);
void picoph_flushTx(bool bDiscard);
int picoph_write(const char *pcBuf, uint32_t ui32Len);

void picoph_tx_str(void);
int picoph_buf_size(void);
int picoph_buf_write(unsigned char inChar);

// workhorse functions
int picoph_stateMachine(int picoState);
uint32_t picoph_qa_test(void);
void picoph_comm_test(void);
void picoph_oneMeas(void);

int picoph_parseData(void);     // FUTURE

struct picophDriver
{
             int state;
    unsigned int old_tick10ms;      // for timeout
    unsigned int new_tick10ms;
    unsigned int bufIndx;
};

#endif /* PICOPH_H_ */
