/*! * \brief Write data from program space to a stream. * * Similar to fwrite() except that the data is located in program memory. * * \param data Pointer to items in program space to be written. * \param size Item size in bytes. * \param count Number of items to write. * \param stream Pointer to a previously opened stream. * * \return The number of items written, which may be less than the * specified number. * * \warning The function will not check, if the stream pointer points * to a valid stream. */ size_t fwrite_P(PGM_P data, size_t size, size_t count, FILE * stream) { size_t rc; NUTASSERT(stream != NULL); if (size > 1) count *= size; if ((int) (rc = (size_t) _write_P(stream->iob_fd, data, count)) <= 0) return 0; if (size > 1) rc /= size; return rc; }
/* * UART sample. * * Some functions do not work with ICCAVR. */ int main(void) { int got; int userchoice; char *cp; u_long baud = 115200; #ifdef STDIO_FLOATING_POINT float dval = 0.0; #endif /* * Each device must be registered. We do this by referencing the * device structure of the driver. The advantage is, that only * those device drivers are included in our flash code, which we * really need. * * The uart0 device is the first one on the ATmega chip. So it * has no configurable base address or interrupt and we set both * parameters to zero. */ NutRegisterDevice(&DEV_UART, 0, 0); /* * Now, as the device is registered, we can open it. The fopen() * function returns a pointer to a FILE structure, which we use * for subsequent reading and writing. */ uart = fopen(DEV_UART_NAME, "r+"); freopen("uart0", "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); /* * Before doing the first read or write, we set the baudrate. * This low level function doesn't know about FILE structures * and we use _fileno() to get the low level file descriptor * of the stream. * * The short sleep allows the UART to settle after the baudrate * change. */ _ioctl(_fileno(uart), UART_SETSPEED, &baud); /* Initialize the LCd */ NutRegisterDevice(&devLcd, 0, 0); /* Initialize UROM */ NutRegisterDevice(&devUrom, 0, 0); NutRegisterDevice(&devDebug0, 0, 0); /* * Stream devices can use low level read and write functions. * Writing program space data is supported too. */ _write(_fileno(uart), banner, strlen(banner)); { lcdPrint(banner); _write_P(_fileno(uart), presskey_P, sizeof(presskey_P)); } /* * Stream devices do buffered I/O. That means, nothing will be * passed to the hardware device until either the output buffer * is full or we do a flush. With stream I/O we typically use * fflush(), but low level writing a null pointer will also flush * the output buffer. */ _write(_fileno(uart), 0, 0); /* * The low level function read() will grab all available bytes * from the input buffer. If the buffer is empty, the call will * block until something is available for reading. */ got = _read(_fileno(uart), inbuf, sizeof(inbuf)); _write(_fileno(uart), inbuf, got); /* * Nut/OS never expects a thread to return. So we enter an * endless loop here. */ do { fflush(uart); lcdPrint("Main Menu"); userchoice = 0; /* * A bit more advanced input routine is able to read a string * up to and including the first newline character or until a * specified maximum number of characters, whichever comes first. */ puts("\n*************"); puts("\n* Main Menu *"); puts("\n*************\n"); puts("\n1. Generate speech\n"); puts("\n2. IRC\n"); puts("\n3. Settings\n"); puts("\n4. Help\n"); puts("\n5. Play wav\n"); puts("\nEnter your choice: "); readLine(inbuf, sizeof(inbuf)); /* * Streams support formatted output as well as printing strings * from program space. */ if (inbuf[0]) { puts("\n\n"); userchoice = atoi(inbuf); switch (userchoice) { case 1: speechSynthesize(uart); break; case 2: runIRC(uart); break; case 3: openSettings(uart); break; case 4: showHelp(uart); break; case 5: playWAV(uart); break; default: puts("Invalid option selected\n"); } } /* * Just to demonstrate formatted floating point output. * In order to use this, we need to link the application * with nutcrtf instead of nutcrt for pure integer. */ #ifdef STDIO_FLOATING_POINT dval += 1.0125; fprintf(uart, "FP %f\n", dval); #endif } while (1); }
/*! * \brief Write a string from progam memory to a stream. * * Similar to fputs() except that the string is located in program * space. * * \param stream Pointer to a previously opened stream. * \param string String in program memory to write. * * \return A non-negative value if successful or EOF to indicate an error. * * \warning The function will not check, if the stream pointer points * to a valid stream. */ int fputs_P(PGM_P string, FILE * stream) { return _write_P(stream->iob_fd, string, strlen_P(string)); }
static int NutPppWrite_P(NUTFILE * fp, PGM_P buffer, int len) { return _write_P(((PPPDCB *) (fp->nf_dev->dev_dcb))->dcb_fd, buffer, len); }
/*! * \brief Write a string from progam memory to a stream. * * Similar to fputs() except that the string is located in program * space. * * \param stream Pointer to a previously opened stream. * \param string String in program memory to write. * * \return A non-negative value if successful or EOF to indicate an error. * * \warning The function will not check, if the stream pointer points * to a valid stream. */ int fputs_P(PGM_P string, FILE * stream) { NUTASSERT(stream != NULL); return _write_P(stream->iob_fd, string, strlen_P(string)); }