コード例 #1
0
struct termios *
__get_termios(struct fd *fd)
{
	struct termios *tios = NULL;

	if(FLAG_IS_SET(fd->fd_Flags,FDF_IS_SOCKET)) /* Network socket. Remote terminal? */
	{
		__set_errno(ENODEV);
		goto out;
	}

	if(FLAG_IS_CLEAR(fd->fd_Flags,FDF_IS_INTERACTIVE))
	{
		__set_errno(ENOTTY);
		goto out;
	}

	if(FLAG_IS_SET(fd->fd_Flags,FDF_TERMIOS))
		tios = fd->fd_Aux;
	else
		tios = get_console_termios(fd);

 out:

	return(tios);
}
コード例 #2
0
int
tcgetattr(int file_descriptor,struct termios *user_tios)
{
	int result = ERROR;
	struct fd *fd = NULL;
	struct termios *tios;

	__stdio_lock();

	if(user_tios == NULL)
	{
		__set_errno(EFAULT);
		goto out;
	}

	fd = __get_file_descriptor(file_descriptor);
	if(fd == NULL)
	{
		__set_errno(EBADF);
		goto out;
	}

	__fd_lock(fd);

	if(FLAG_IS_SET(fd->fd_Flags,FDF_TERMIOS))
	{
		assert(fd->fd_Aux != NULL);

		memcpy(user_tios,fd->fd_Aux,sizeof(struct termios));
	}
	else
	{
		tios = __get_termios(fd);
		if(tios == NULL)
			goto out;

		memcpy(user_tios,tios,sizeof(struct termios));
	}

	result = OK;

 out:

	__fd_unlock(fd);

	__stdio_unlock();

	return(result);
}
コード例 #3
0
static struct termios *
get_console_termios(struct fd *fd)
{
	struct termios *tios;

	/* Create a new, fresh termios. TODO: Actually query some values,
	   e.g. ICANON and ECHO should not be set for RAW consoles. */
	tios = malloc(sizeof(*tios));
	if(tios == NULL)
	{
		__set_errno(ENOMEM);
		goto out;
	}

	memset(tios,0,sizeof(*tios));

	/* Set up the initial termios state. */
	tios->c_iflag = 0;
	tios->c_oflag = 0;
	tios->c_cflag = CS8|CLOCAL;

	if(FLAG_IS_SET(fd->fd_Flags,FDF_READ))
		SET_FLAG(tios->c_cflag,CREAD);

	tios->c_lflag = ISIG|ICANON|ECHO;

	memcpy(tios->c_cc,def_console_cc,NCCS);

	tios->c_ispeed = B9600;	/* A default as good as any? */
	tios->c_ospeed = B9600;

	tios->type = TIOST_CONSOLE;
	tios->flags = 0;

	/* Change the hook. */
	fd->fd_Action = __termios_console_hook;
	fd->fd_Aux = tios;

	SET_FLAG(fd->fd_Flags,FDF_TERMIOS);

 out:

	return(tios);
}
コード例 #4
0
/**************************************************************************
    Main
***************************************************************************/
int main (void) 
{
	uint8_t color = UNIQUE_COLORS -1;
	uint8_t row = 0;
	uint8_t col = 0;
	uint8_t quad = 0;
	uint8_t binary_cnt = 0;
	uint8_t wdt_cnt = 0;
	uint8_t update_cnt = 0;
	
	initialize_AVR();

	SET_FLAG(SET_LEDS);
	chase_sequence = SMILEY;
	DISABLE_SERVOS();

	quad_flags = 0xFF;

	turn_off_matrices();

	// Turn On TWI
	TWI_RESET_WITH_ACK();


	/**********************************************
	 *	MAIN LOOP
	 *	- Handle Chase Sequences
	 **********************************************/
	for(;;)
	{
		//-------------------------
		// Handle Color Loops
		//-------------------------
		if (FLAG_IS_SET(INCREMENT_COLOR)) {
			if (++color == UNIQUE_COLORS)
				color = 1;
			CLEAR_FLAG(INCREMENT_COLOR);
		}
		else if (FLAG_IS_SET(DECREMENT_COLOR)) {
			if (--color == 0)
				color = UNIQUE_COLORS -1;
			CLEAR_FLAG(DECREMENT_COLOR);
		}

		//-------------------------
		// Reset Chase Sequence
		//-------------------------
		if (FLAG_IS_SET(RESET_CHASE)) {
			wdt_cnt = 0;
			chase_sequence = LOOP_QUAD;
			CLEAR_FLAG(RESET_CHASE);
			CLEAR_FLAG(PASSIVE_MODE);
			ENABLE_SERVOS();
		}

		//-------------------------
		// Handle Chase Sequences
		//-------------------------
		switch (chase_sequence) {

			//-------------------------
			// Constant On at the last color
			//-------------------------
			case ALL_CONSTANT:
				break;

			//-------------------------
			// Loop through all colors
			//-------------------------
			case LOOP_ALL:
				SET_FLAG(DECREMENT_COLOR);
				set_matrix(0, color);
				set_matrix(1, color);
				_delay_ms(100);
				break;
			
			//-------------------------
			// Color wheel - one color transistion per revolution
			//-------------------------
			case QUAD_WHEEL:
				if (quad == 0) {
					set_quadrant(0, 3, COL_BLACK);
					set_quadrant(1, 3, COL_BLACK);
				}
				else {
					set_quadrant(0, quad-1, COL_BLACK);
					set_quadrant(1, quad-1, COL_BLACK);
				}
				set_quadrant(0, quad, color);
				set_quadrant(1, quad, color);
				if (++quad == QUADS) {
					SET_FLAG(DECREMENT_COLOR);
					quad = 0;
				}
				_delay_ms(50);
				break;

			//-------------------------
			// Color wheel - change colors with quadrants
			//-------------------------
			case QUAD_WHEEL2:
				SET_FLAG(DECREMENT_COLOR);
				if (quad == 0) {
					set_quadrant(0, 3, COL_BLACK);
					set_quadrant(1, 3, COL_BLACK);
				}
				else {
					set_quadrant(0, quad-1, COL_BLACK);
					set_quadrant(1, quad-1, COL_BLACK);
				}
				set_quadrant(0, quad, color);
				set_quadrant(1, quad, color);
				if (++quad == QUADS) {
					quad = 0;
				}
				_delay_ms(50);
				
				break;

			//-------------------------
			// Loop through all colors in Quadrant(s)
			//-------------------------
			case LOOP_QUAD:
				SET_FLAG(DECREMENT_COLOR);
				set_quadrants(color);
				_delay_ms(100);
				break;

			//-------------------------
			// Binary Counter - by Columns
			//-------------------------
			case BINARY_COLS:
				for (col = 0; col < COLUMNS; ++col) {
					if (binary_cnt & _BV(col))
						set_column(0, col, COL_BLUE);
					else
						set_column(0, col, COL_BLACK);
				}
				++binary_cnt;
				_delay_ms(250);
				break;

			//-------------------------
			// Binary Counter - by Row
			//-------------------------
			case BINARY_ROWS:
				for (row = 0; row < ROWS; ++row) {
					if (binary_cnt & _BV(row))
						set_row(0, row, COL_RED);
					else
						set_row(0, row, COL_BLACK);
				}
				++binary_cnt;
				_delay_ms(250);
				break;

			//-------------------------
			// Display all colors
			//-------------------------
			case ALL_COLORS:
				if (FLAG_IS_SET(SET_LEDS)) {
					for (col = 0; col < COLUMNS; ++col) {
						for (row = 0; row < ROWS; ++row) {
							if (color < UNIQUE_COLORS) {
								set_led(0, col, row, color);
								++color;
							}
							else
								set_led(0, col, row, COL_BLACK);
						}
					}
					CLEAR_FLAG(SET_LEDS);
				}
				break;

			//-------------------------
			// Smiley Faces
			//-------------------------
			case SMILEY:
				if (FLAG_IS_SET(SET_LEDS)) {
					turn_off_matrices();

					// Border
					#define FACE_COLOR	COL_OLIVE
					set_row(0, 0, FACE_COLOR);
					set_row(0, 7, FACE_COLOR);
					set_column(0, 0, FACE_COLOR);
					set_column(0, 7, FACE_COLOR);

					set_row(1, 0, FACE_COLOR);
					set_row(1, 7, FACE_COLOR);
					set_column(1, 0, FACE_COLOR);
					set_column(1, 7, FACE_COLOR);

					// Eyes
					set_led(0, 1, 1, COL_WHITE);
					set_led(0, 1, 2, COL_WHITE);
					set_led(0, 2, 1, COL_WHITE);
					set_led(0, 2, 2, COL_BLUE);

					set_led(0, 1, 5, COL_WHITE);
					set_led(0, 1, 6, COL_WHITE);
					set_led(0, 2, 5, COL_WHITE);
					set_led(0, 2, 6, COL_BLUE);

					set_led(1, 1, 1, COL_WHITE);
					set_led(1, 1, 2, COL_WHITE);
					set_led(1, 2, 2, COL_WHITE);
					set_led(1, 2, 1, COL_GREEN);

					set_led(1, 1, 5, COL_WHITE);
					set_led(1, 1, 6, COL_WHITE);
					set_led(1, 2, 6, COL_WHITE);
					set_led(1, 2, 5, COL_GREEN);

					// Nose
					set_led(0, 3, 4, FACE_COLOR);
					set_led(0, 4, 3, FACE_COLOR);
					set_led(0, 4, 4, FACE_COLOR);

					set_led(1, 3, 3, FACE_COLOR);
					set_led(1, 4, 3, FACE_COLOR);
					set_led(1, 4, 4, FACE_COLOR);

					// Mouth
					set_led(0, 5, 1, COL_CORAL);
					set_led(0, 6, 2, COL_CORAL);
					set_led(0, 6, 3, COL_CORAL);
					set_led(0, 6, 4, COL_CORAL);
					set_led(0, 6, 5, COL_CORAL);
					set_led(0, 5, 6, COL_CORAL);

					set_led(1, 5, 1, COL_CORAL);
					set_led(1, 6, 2, COL_CORAL);
					set_led(1, 6, 3, COL_CORAL);
					set_led(1, 6, 4, COL_CORAL);
					set_led(1, 6, 5, COL_CORAL);
					set_led(1, 5, 6, COL_CORAL);
					CLEAR_FLAG(SET_LEDS);
					SET_FLAG(UPDATE_LEDS);
				}

					// Look back and forth
				if (FLAG_IS_SET(UPDATE_LEDS)) {
					set_led(0, 2, 2, COL_WHITE);
					set_led(0, 2, 1, COL_BLUE);
					set_led(0, 2, 6, COL_WHITE);
					set_led(0, 2, 5, COL_BLUE);
					set_led(1, 2, 1, COL_WHITE);
					set_led(1, 2, 2, COL_GREEN);
					set_led(1, 2, 5, COL_WHITE);
					set_led(1, 2, 6, COL_GREEN);
					_delay_ms(200);
					set_led(0, 2, 1, COL_WHITE);
					set_led(0, 2, 2, COL_BLUE);
					set_led(0, 2, 5, COL_WHITE);
					set_led(0, 2, 6, COL_BLUE);
					set_led(1, 2, 2, COL_WHITE);
					set_led(1, 2, 1, COL_GREEN);
					set_led(1, 2, 6, COL_WHITE);
					set_led(1, 2, 5, COL_GREEN);
					update_cnt = SMILEY_EYE_DELAY;
					CLEAR_FLAG(UPDATE_LEDS);
				}

				break;

			//-------------------------
			// Set matrix to white
			//-------------------------
			case ALL_WHITE:
				if (FLAG_IS_SET(SET_LEDS)) {
					set_matrix(0, COL_WHITE);
					CLEAR_FLAG(SET_LEDS);
				}
				break;

			//-------------------------
			// Turn off all LEDs
			//-------------------------
			case ALL_OFF:
			default:
				if (FLAG_IS_SET(SET_LEDS)) {
					turn_off_matrices();
					CLEAR_FLAG(SET_LEDS);
				}
				break;
			
			//-------------------------
			// Test - Test corner LEDs
			//-------------------------
			case TEST_CORNERS:
				set_led(0, 7, 0, COL_BLACK);
				set_led(0, 0, 0, COL_RED);
				set_led(0, 4, 4, COL_RED);
				set_led(1, 7, 7, COL_BLACK);
				set_led(1, 0, 7, COL_RED);
				set_led(1, 4, 3, COL_RED);
				_delay_ms(200);
				set_led(0, 0, 0, COL_BLACK);
				set_led(0, 0, 7, COL_BLUE);
				set_led(0, 4, 4, COL_BLUE);
				set_led(1, 0, 7, COL_BLACK);
				set_led(1, 0, 0, COL_BLUE);
				set_led(1, 4, 3, COL_BLUE);
				_delay_ms(200);
				set_led(0, 0, 7, COL_BLACK);
				set_led(0, 7, 7, COL_YELLOW);
				set_led(0, 4, 4, COL_YELLOW);
				set_led(1, 0, 0, COL_BLACK);
				set_led(1, 7, 0, COL_YELLOW);
				set_led(1, 4, 3, COL_YELLOW);
				_delay_ms(200);
				set_led(0, 7, 7, COL_BLACK);
				set_led(0, 7, 0, COL_GREEN);
				set_led(0, 4, 4, COL_GREEN);
				set_led(1, 7, 0, COL_BLACK);
				set_led(1, 7, 7, COL_GREEN);
				set_led(1, 4, 3, COL_GREEN);
				_delay_ms(200);
				break;
		}

		//-------------------------
		// Update Timer - 10ms increments
		//-------------------------
		if (update_cnt > 0) {
			_delay_ms(10);
			if (--update_cnt == 0) {
				SET_FLAG(UPDATE_LEDS);
			}
		}


		//-------------------------
		// WatchDog Timer
		//-------------------------
		if (wdt_cnt < WDT_MAX) {
			if (++wdt_cnt == WDT_MAX) {
				DISABLE_SERVOS();
				SET_FLAG(SET_LEDS);
				SET_FLAG(PASSIVE_MODE);
				chase_sequence = SMILEY;
			}
		}
		

	}	// End of Main Loop
}	// End of Main
コード例 #5
0
ファイル: stdio.c プロジェクト: ChenZewei/NyuziHPS-old
/*
 *	 % flags width .precision prefix format
 */
int vfprintf(FILE *f, const char *format, va_list args)
{
	int flags = 0;
	int prefixes = 0;
	int width = 0;
	int precision = 0;
	
	enum {
		kScanText,
		kScanFlags,
		kScanWidth,
		kScanPrecision,
		kScanPrefix,
		kScanFormat
	} state = kScanText;

	while (*format) {
		switch (state) {
			case kScanText:
				if (*format == '%') {
					format++;
					state = kScanFlags;
					flags = 0;				/* reset attributes */
					prefixes = 0;
					width = 0;
					precision = 0;
				} else
					fputc(*format++, f);
				
				break;
				
			case kScanFlags: {
				const char *c;
				
				if (*format == '%') {
					fputc(*format++, f);
					state = kScanText;
					break;
				}
			
				c = strchr(kFlagCharacters, *format);
				if (c) {
					SET_FLAG(*format);
					format++;
				} else
					state = kScanWidth;
				
				break;
			}

			case kScanWidth:
				if (isdigit(*format))
					width = width * 10 + *format++ - '0';
				else if (*format == '.') {
					state = kScanPrecision;
					format++;
				} else
					state = kScanPrefix;
					
				break;
				
			case kScanPrecision:
				if (isdigit(*format))
					precision = precision * 10 + *format++ - '0';
				else
					state = kScanPrefix;
					
				break;
				
			case kScanPrefix: {
				const char *c = strchr(kPrefixCharacters, *format);
				if (c) {
					SET_PREFIX(*format);
					format++;
				} else
					state = kScanFormat;

				break;
			}

			case kScanFormat: {
				char temp_string[64];
				int index;
				char pad_char;
				int pad_count;
				int radix = 10;
			
				switch (*format) {
					case 'p':	/* pointer */
						width = 8;
						SET_FLAG('0');
						 
						/* falls through */

					case 'x':
					case 'X':	/* unsigned hex */
					case 'o':	/* octal */
					case 'u':	/* Unsigned decimal */
					case 'd':
					case 'i':	{ /* Signed decimal */
						unsigned int value;
						value = va_arg(args, unsigned);		/* long */

						/* figure out base */
						if (*format == 'o')
							radix = 8;
						else if (*format == 'x' || *format == 'X' || *format == 'p')
							radix = 16;
						else
							radix = 10;

						/* handle sign */
						if ((*format == 'd' || *format == 'i')) {
							if ((long) value < 0) {
								value = (unsigned) (- (long) value);
								fputc('-', f);
							}
						}

						/* write out the string backwards */
						index = 63;
						for (;;) {
							temp_string[index] = kHexDigits[value % radix];
							value /= radix;
							if (value == 0)
								break;
						
							if (index == 0)
								break;
								
							index--;
						}

						/* figure out width pad char */						
						if (FLAG_IS_SET('0'))
							pad_char = '0';
						else
							pad_char = ' ';

						/* write width padding */
						for (pad_count = (64 - index); pad_count < width; pad_count++)
							fputc(pad_char, f);
						
						/* write precision padding */
						for (; pad_count < precision; pad_count++)
							fputc('0', f);

						/* write the string */
						while (index < 64)
							fputc(temp_string[index++], f);
				
						break;
					}


					case 'c':	/* Single character */
						fputc(va_arg(args, int), f);
						break;
				
					case 's': {	/* string */
						int index = 0;
						int max_width;
						char *c = va_arg(args, char*);
						
						if (precision == 0)
							max_width = 0x7fffffff;
						else
							max_width = precision;

						for (index = 0; index < max_width && *c; index++)
							fputc(*c++, f);

						while (index < MIN(width, max_width)) {
							fputc(' ', f);
							index++;
						}

						break;
					}
					
					case 'f':
					case 'g':
					{
						// Printing floating point numbers accurately is a tricky problem.
						// This implementation is simple and buggy.
						// See "How to Print Floating Point Numbers Accurately" by Guy L. Steele Jr.
						// and Jon L. White for the gory details.
						// XXX does not handle inf and NaN
						float floatval = va_arg(args, float);
						int wholePart;
						float frac;

						if (floatval < 0.0f)
						{
							fputc('-', f);
							floatval = -floatval;
						}

						wholePart = (int) floatval;
						frac = floatval - wholePart;
		
						// Print the whole part (XXX ignores padding)
						if (wholePart == 0)
							fputc('0', f);
						else
						{
							char wholeStr[20];
							unsigned int wholeOffs = sizeof(wholeStr);
							while (wholePart > 0)
							{
								int digit = wholePart % 10;
								wholeStr[--wholeOffs] = digit + '0';
								wholePart /= 10;
							}

							while (wholeOffs < sizeof(wholeStr))
								fputc(wholeStr[wholeOffs++], f);
						}
		
						fputc('.', f);

						// Print the fractional part, not especially accurately
						int maxDigits = precision > 0 ? precision : 7;
						do
						{
							frac = frac * 10;	
							int digit = (int) frac;
							frac -= digit;
							fputc((digit + '0'), f);
						}
						while (frac > 0.0f && maxDigits-- > 0);
						
						break;
					}						
				}
				
				format++;
				state = kScanText;
				break;				
			}
		}
	}

	return 0;
}
コード例 #6
0
ファイル: mmsearch.c プロジェクト: AIDman/Kaldi
void DoAllSearches(int verb, char pre, int nregs, int MACC, int lat, int nb,
                   int ku, char *outfile)
{
   char ln[256];
   ATL_mmnode_t *mmg, *mme, *mmp;
   double mf;

/*
 * First, find best generated (emit_mm) kernel
 */
   printf("\nINVOKING GMMSEARCH.C, PRE='%c'\n", pre);
   sprintf(ln, "make RunGMMSearch pre=%c\n", pre);
   assert(!system(ln));
   mmg = ReadMMFileWithPath(pre, "res", "guMMRES.sum");
   if (mmg)
   {
      if (mmg->mflop[0] <= 0)  /* need to retime */
      {
         mmg->mflop[0] = TimeGMMKernel(verb, 0, pre, mmg->muladd, mmg->lat,
                                       1, mmg->nbB, mmg->mu, mmg->nu, mmg->ku,
                                       mmg->fftch, mmg->iftch, mmg->nftch,
                                       FLAG_IS_SET(mmg->flag, MMF_LDCTOP),
                                       mmg->pref, -1, -1);
      }
      WriteMMFileWithPath(pre, "res", "guMMRES.sum", mmg);
   }
   else
   {
      mmg = ReadMMFileWithPath(pre, "res", "gMMRES.sum");
      assert(mmg);
   }
   printf("\nDONE GMMSEARCH.C, PRE='%c'\n\n", pre);
/*
 * Get results of all external searches
 */
   printf("\nRUNNING EXTERNAL SEARCHES, PRE='%c', NB=%d:\n", pre, mmg->nbB);
   sprintf(ln, "make RunUMMSearch pre=%c nb=%d", pre, mmg->nbB);
   assert(!system(ln));
   mme = ReadMMFileWithPath(pre, "res", "eMMRES.sum");
   assert(mme);
/*
 * If user-written kernel is noticably better than generated, use it;
 * If it changes NB< we will need to retune the generateed case to match new NB!
 */
   if (mme->mflop[0] > 1.03*mmg->mflop[0])
   {
      if (mme->nbB != mmg->nbB)
      {
         printf("\nFORCING NB=%d GMMSEARCH.C, PRE='%c'\n", mme->nbB, pre);
         sprintf(ln, "rm res/%cguMMRES.sum", pre);
         system(ln);
         sprintf(ln, "make res/%cguMMRES.sum pre=%c, nb=%d\n", pre, pre,
                 mme->nbB);
         assert(!system(ln));
         KillAllMMNodes(mmg);
         mmg = ReadMMFileWithPath(pre, "res", "guMMRES.sum");
         assert(mmg);
         assert(mmg->nbB == mme->nbB);
      }
      else
         WriteMMFileWithPath(pre, "res", "guMMRES.sum", mmg);
   }
   else  /* generated kernel is just as good */
   {
      WriteMMFileWithPath(pre, "res", "guMMRES.sum", mmg);
      KillAllMMNodes(mme);
      mme = NULL;
   }
   mmg->next = mme;
   WriteMMFileWithPath(pre, "res", "MMRES.sum", mmg);
/*
 * Find no-copy code
 */
   sprintf(ln, "./xmmcuncpsearch -p %c -R -6\n", pre);
   assert(!system(ln));
/*
 * Find cleanup code
 */
   sprintf(ln, "./xmmcuncpsearch -p %c -R -3\n", pre);
   assert(!system(ln));
   if (!mmg->next)
   {
      printf("\n\nFor this run, the best parameters found were MACC=%d, lat=%d, NB=%d, MU=%d, NU=%d, KU=%d\n",
             mmg->muladd, mmg->lat, mmg->nbB, mmg->mu, mmg->nu, mmg->ku);
      mf = mmg->mflop[0];
   }
   else
   {
      mf = mmg->next->mflop[0];
       printf("\n\nFor this run, the best case found was NB=%d user case %d\n",
              mmg->next->nbB, mmg->next->ID);
       if (mmg->next->auth)
          printf("written by %s", mmg->next->auth);
       if (mmg->next->ID > 0 && mmg->next->rout)
          printf(", filename='%s'.\n", mmg->next->rout);
       else if (mmg->next->auth)
          printf("\n");
   }
   printf("This gave a performance = %f MFLOP.\n", mf);
   printf("The necessary files have been created.  If you are happy with\n");
   printf("the above mflops for your system, type 'make %cinstall'.\n\n", pre);
   KillAllMMNodes(mmg);
}