This is the latest working repository used in our demo video for the Maxim to display temperature readings on Bluetooth

Dependencies:   USBDevice

Committer:
darienf
Date:
Sat Apr 10 03:05:42 2021 +0000
Revision:
3:36de8b9e4b1a
Parent:
HSP/RpcServer/StringInOut.cpp@0:832122ce6748
ayoooo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darienf 0:832122ce6748 1 /*******************************************************************************
darienf 0:832122ce6748 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
darienf 0:832122ce6748 3 *
darienf 0:832122ce6748 4 * Permission is hereby granted, free of charge, to any person obtaining a
darienf 0:832122ce6748 5 * copy of this software and associated documentation files (the "Software"),
darienf 0:832122ce6748 6 * to deal in the Software without restriction, including without limitation
darienf 0:832122ce6748 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
darienf 0:832122ce6748 8 * and/or sell copies of the Software, and to permit persons to whom the
darienf 0:832122ce6748 9 * Software is furnished to do so, subject to the following conditions:
darienf 0:832122ce6748 10 *
darienf 0:832122ce6748 11 * The above copyright notice and this permission notice shall be included
darienf 0:832122ce6748 12 * in all copies or substantial portions of the Software.
darienf 0:832122ce6748 13 *
darienf 0:832122ce6748 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
darienf 0:832122ce6748 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
darienf 0:832122ce6748 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
darienf 0:832122ce6748 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
darienf 0:832122ce6748 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
darienf 0:832122ce6748 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
darienf 0:832122ce6748 20 * OTHER DEALINGS IN THE SOFTWARE.
darienf 0:832122ce6748 21 *
darienf 0:832122ce6748 22 * Except as contained in this notice, the name of Maxim Integrated
darienf 0:832122ce6748 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
darienf 0:832122ce6748 24 * Products, Inc. Branding Policy.
darienf 0:832122ce6748 25 *
darienf 0:832122ce6748 26 * The mere transfer of this software does not imply any licenses
darienf 0:832122ce6748 27 * of trade secrets, proprietary technology, copyrights, patents,
darienf 0:832122ce6748 28 * trademarks, maskwork rights, or any other form of intellectual
darienf 0:832122ce6748 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
darienf 0:832122ce6748 30 * ownership rights.
darienf 0:832122ce6748 31 *******************************************************************************
darienf 0:832122ce6748 32 */
darienf 0:832122ce6748 33 #include "mbed.h"
darienf 0:832122ce6748 34 #include "USBSerial.h"
darienf 0:832122ce6748 35 #include "RpcFifo.h"
darienf 0:832122ce6748 36 #include "RpcServer.h"
darienf 0:832122ce6748 37 #include "StringInOut.h"
darienf 0:832122ce6748 38 #include "Peripherals.h"
darienf 0:832122ce6748 39
darienf 0:832122ce6748 40 /// a running index that keeps track of where an incoming string has been
darienf 0:832122ce6748 41 /// buffered to
darienf 0:832122ce6748 42 static int lineBuffer_index = 0;
darienf 0:832122ce6748 43 /// a flag that keeps track of the state of accumulating a string
darienf 0:832122ce6748 44 static int getLine_State = GETLINE_WAITING;
darienf 0:832122ce6748 45
darienf 0:832122ce6748 46 extern USBSerial *usbSerialPtr;
darienf 0:832122ce6748 47
darienf 0:832122ce6748 48 /**
darienf 0:832122ce6748 49 * @brief Place incoming USB characters into a fifo
darienf 0:832122ce6748 50 * @param data_IN buffer of characters
darienf 0:832122ce6748 51 * @param len length of data
darienf 0:832122ce6748 52 */
darienf 0:832122ce6748 53 int fifoIncomingChars(uint8_t data_IN[], unsigned int len) {
darienf 0:832122ce6748 54 unsigned int i;
darienf 0:832122ce6748 55 for (i = 0; i < len; i++) {
darienf 0:832122ce6748 56 fifo_put8(GetUSBIncomingFifo(), data_IN[i]);
darienf 0:832122ce6748 57 }
darienf 0:832122ce6748 58 return 0;
darienf 0:832122ce6748 59 }
darienf 0:832122ce6748 60
darienf 0:832122ce6748 61 /**
darienf 0:832122ce6748 62 * @brief Check the USB incoming fifo to see if there is data to be read
darienf 0:832122ce6748 63 * @return 1 if there is data to be read, 0 if data is not available
darienf 0:832122ce6748 64 */
darienf 0:832122ce6748 65 int isReadReady(void) {
darienf 0:832122ce6748 66 if (fifo_empty(GetUSBIncomingFifo()) == 0)
darienf 0:832122ce6748 67 return 1;
darienf 0:832122ce6748 68 return 0;
darienf 0:832122ce6748 69 }
darienf 0:832122ce6748 70
darienf 0:832122ce6748 71 /**
darienf 0:832122ce6748 72 * @brief Clear the incoming USB read fifo
darienf 0:832122ce6748 73 */
darienf 0:832122ce6748 74 void clearOutReadFifo(void) { fifo_clear(GetUSBIncomingFifo()); }
darienf 0:832122ce6748 75
darienf 0:832122ce6748 76 /**
darienf 0:832122ce6748 77 * @brief Block until a character can be read from the USB
darienf 0:832122ce6748 78 * @return the character read
darienf 0:832122ce6748 79 */
darienf 0:832122ce6748 80 char getch(void) {
darienf 0:832122ce6748 81 uint8_t ch;
darienf 0:832122ce6748 82 // block until char is ready
darienf 0:832122ce6748 83 while (isReadReady() == 0) {
darienf 0:832122ce6748 84 }
darienf 0:832122ce6748 85 // read a char from buffer
darienf 0:832122ce6748 86 fifo_get8(GetUSBIncomingFifo(), &ch);
darienf 0:832122ce6748 87 return ch;
darienf 0:832122ce6748 88 }
darienf 0:832122ce6748 89
darienf 0:832122ce6748 90 /**
darienf 0:832122ce6748 91 * @brief Place incoming USB characters into a fifo
darienf 0:832122ce6748 92 * @param lineBuffer buffer to place the incoming characters
darienf 0:832122ce6748 93 * @param bufferLength length of buffer
darienf 0:832122ce6748 94 * @return GETLINE_WAITING if still waiting for a CRLF, GETLINE_DONE
darienf 0:832122ce6748 95 */
darienf 0:832122ce6748 96 int getLine(char *lineBuffer, int bufferLength) {
darienf 0:832122ce6748 97 uint8_t ch;
darienf 0:832122ce6748 98
darienf 0:832122ce6748 99 //USBSerial *serial = Peripherals::usbSerial();
darienf 0:832122ce6748 100 if (getLine_State == GETLINE_DONE) {
darienf 0:832122ce6748 101 getLine_State = GETLINE_WAITING;
darienf 0:832122ce6748 102 }
darienf 0:832122ce6748 103 if (usbSerialPtr->available() != 0) {
darienf 0:832122ce6748 104 ch = usbSerialPtr->_getc();
darienf 0:832122ce6748 105 if (ch != 0x0A && ch != 0x0D) {
darienf 0:832122ce6748 106 lineBuffer[lineBuffer_index++] = ch;
darienf 0:832122ce6748 107 }
darienf 0:832122ce6748 108 if (ch == 0x0D) {
darienf 0:832122ce6748 109 lineBuffer[lineBuffer_index++] = 0;
darienf 0:832122ce6748 110 lineBuffer_index = 0;
darienf 0:832122ce6748 111 getLine_State = GETLINE_DONE;
darienf 0:832122ce6748 112 }
darienf 0:832122ce6748 113 if (lineBuffer_index > bufferLength) {
darienf 0:832122ce6748 114 lineBuffer[bufferLength - 1] = 0;
darienf 0:832122ce6748 115 getLine_State = GETLINE_DONE;
darienf 0:832122ce6748 116 }
darienf 0:832122ce6748 117 }
darienf 0:832122ce6748 118 return getLine_State;
darienf 0:832122ce6748 119 }
darienf 0:832122ce6748 120
darienf 0:832122ce6748 121 /**
darienf 0:832122ce6748 122 * @brief Block until a fixed number of characters has been accumulated from the
darienf 0:832122ce6748 123 * incoming USB
darienf 0:832122ce6748 124 * @param lineBuffer buffer to place the incoming characters
darienf 0:832122ce6748 125 * @param maxLength length of buffer
darienf 0:832122ce6748 126 */
darienf 0:832122ce6748 127 void getStringFixedLength(uint8_t *lineBuffer, int maxLength) {
darienf 0:832122ce6748 128 uint8_t ch;
darienf 0:832122ce6748 129 int index = 0;
darienf 0:832122ce6748 130 // block until maxLength is captured
darienf 0:832122ce6748 131 while (1) {
darienf 0:832122ce6748 132 ch = getch();
darienf 0:832122ce6748 133 lineBuffer[index++] = ch;
darienf 0:832122ce6748 134 if (index == maxLength)
darienf 0:832122ce6748 135 return;
darienf 0:832122ce6748 136 }
darienf 0:832122ce6748 137 }
darienf 0:832122ce6748 138
darienf 0:832122ce6748 139 /**
darienf 0:832122ce6748 140 * @brief Outut an array of bytes out the USB serial port
darienf 0:832122ce6748 141 * @param data buffer to output
darienf 0:832122ce6748 142 * @param length length of buffer
darienf 0:832122ce6748 143 */
darienf 0:832122ce6748 144 int putBytes(uint8_t *data, uint32_t length) {
darienf 0:832122ce6748 145 int sent;
darienf 0:832122ce6748 146 int send;
darienf 0:832122ce6748 147 bool status;
darienf 0:832122ce6748 148 uint8_t *ptr;
darienf 0:832122ce6748 149 ptr = data;
darienf 0:832122ce6748 150 //
darienf 0:832122ce6748 151 // break up the string into chunks
darienf 0:832122ce6748 152 //
darienf 0:832122ce6748 153 sent = 0;
darienf 0:832122ce6748 154 do {
darienf 0:832122ce6748 155 send = MAX_PACKET_SIZE_EPBULK;
darienf 0:832122ce6748 156 if ((sent + MAX_PACKET_SIZE_EPBULK) > (int)length) {
darienf 0:832122ce6748 157 send = length - sent;
darienf 0:832122ce6748 158 }
darienf 0:832122ce6748 159 status = usbSerialPtr->writeBlock(ptr,send);
darienf 0:832122ce6748 160 if (status != true) {
darienf 0:832122ce6748 161 while (1) ;
darienf 0:832122ce6748 162 }
darienf 0:832122ce6748 163 sent += send;
darienf 0:832122ce6748 164 ptr += send;
darienf 0:832122ce6748 165 } while (sent < (int)length);
darienf 0:832122ce6748 166 return 0;
darienf 0:832122ce6748 167 }
darienf 0:832122ce6748 168
darienf 0:832122ce6748 169 /**
darienf 0:832122ce6748 170 * @brief Output a string out the USB serial port
darienf 0:832122ce6748 171 * @param str to output
darienf 0:832122ce6748 172 */
darienf 0:832122ce6748 173 int putStr(const char *str) {
darienf 0:832122ce6748 174 int length;
darienf 0:832122ce6748 175 uint8_t *ptr;
darienf 0:832122ce6748 176 ptr = (uint8_t *)str;
darienf 0:832122ce6748 177 length = strlen(str);
darienf 0:832122ce6748 178 putBytes(ptr, length);
darienf 0:832122ce6748 179 return 0;
darienf 0:832122ce6748 180 }