⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printf.c

📁 yampp 7 USB firmware v 2.44 checked, working, latest
💻 C
字号:
/* ***********************************************************************
**
**  Copyright (C) 2002  Jesper Hansen <jesperh@telia.com> 
**
**
**  Yampp-7 - output formating routines
**
**  File printf.c
**
*************************************************************************
**
**   This file is part of the yampp system.
**
**  This program is free software; you can redistribute it and/or
**  modify it under the terms of the GNU General Public License
**  as published by the Free Software Foundation; either version 2
**  of the License, or (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software Foundation, 
**  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
*************************************************************************
**
**  Revision History
**
**  when         what  who	why
**
**  2002-03-15   1.0   Jesper   initial release
**  2003-01-01   1.1   MIS      added compatibility with new versions of GCC
**
*********************************************************************** */

#define _SFR_ASM_COMPAT 1
#include "Constants.h"

#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "types.h"
#include "yampp7lib.h"

void _p_puts(char const *p)
{
	u08 b;
	while (p && (b = pgm_read_byte(p++)))
		lcd_putchar(b);
}

int _p_vprintf(char const *format,va_list ap)
{
  u08 	scratch[16];
  u08 	format_flag;
  u32 	u_val=0;
  u08	base;
  u08	*ptr;
  u08	width;
  u08 	fill;

  while(1)
  {
  
  	width = 0;
  	fill = ' ';
    while ((format_flag = pgm_read_byte(format++)) != '%')
	{  
      if (!format_flag)
	  {
		  va_end (ap); 
		  return (0);
	  }
      lcd_putchar(format_flag);
    }


	// check for zero pad
	format_flag = pgm_read_byte(format) - '0'; 
	if (format_flag == 0)	// zero pad
	{
		fill = '0';
		format++;
	}
	
	// check for width spec
	format_flag = pgm_read_byte(format) - '0'; 
	if (format_flag > 0 && format_flag <= 9)	// width set
	{
		width = format_flag;
		format++;	
	}
	
    switch (format_flag = pgm_read_byte(format++))
	{
		case 'c':
		  format_flag = va_arg(ap,int);

		  //fall through
		  
		default:
		  lcd_putchar(format_flag);
		  continue;
		  
		case 'S':
		case 's':
		  ptr = va_arg(ap,char *);
		  while (*ptr)
			lcd_putchar(*ptr++);
		  continue;

		case 'u':
		  base = 10;
		  goto CONVERSION_LOOP;
		  
		case 'x':
		  base = 16;

CONVERSION_LOOP:
	  	  u_val = va_arg(ap,unsigned int);
		  ptr = scratch + 16;
		  *--ptr = 0;
		  do
		  {
			char ch = u_val % base + '0';
			if (ch > '9')
			  ch += 'a' - '9' - 1;
			*--ptr = ch;
			u_val /= base;

			if (width)
				width--;
		
	      } while (u_val);

		  while (width--)
		  	*--ptr = fill; 		      
	      
		  while (*ptr)
			lcd_putchar(*ptr++);
    }
  }

}

int _p_printf(char const *format, ...)
{
  	va_list ap;
  	va_start (ap, format);
  	return _p_vprintf(format,ap);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -