Пример #1
0
/* Visible cgetchar() that does not advance the cursor */
int vcgetchar(void)
{
  int ch;

  ch = cgetchar();
  putchar(isprint(ch) ? ch : ' ');
  putchar('\b');
  fflush(stdout);

  return ch;
}
Пример #2
0
/*
 * Check if Ctrl-Break was pressed during the last calls
 */
int chkCBreak(int mode)
{
  static int leaveAll = 0;      /* leave all batch files */
  int c;

  switch (mode)
  {
    case BREAK_ENDOFBATCHFILES:
      leaveAll = 0;
      return 0;

    case 0:
      if (!bc)
        goto justCheck;

    case BREAK_BATCHFILE:
      if (leaveAll)
        return 1;
      if (!ctrlBreak)
        return 0;

      /* we need to be sure the string arrives on the screen! */
      do
        cprintf("\r\nCtrl-Break pressed.\r\nCancel batch file '%s'? (Yes/No/All) ", bc && bc->bfnam ? bc->bfnam : "");
      while (!strchr("YNA\3", c = toupper(cgetchar())) || !c);

      cputs("\r\n");

      if (c == 'N')
        return ctrlBreak = 0;   /* ignore */

      leaveAll = c == 'A' || c == CTL_C; /* leave all batch files */

      break;

    justCheck:
    case BREAK_FORCMD:         /* FOR commands are part of batch processing */
      if (leaveAll)
        return 1;
      /* fall through */

    case BREAK_INPUT:
      if (!ctrlBreak)
        return 0;
      break;
  }

  ctrlBreak = 0;                /* state processed */
  return 1;
}
Пример #3
0
int cgetchar_timed(int secs)
{
	struct dostime_t start;
	struct dostime_t now;

	start.second = 60;	/* force decrement secs first time in loop */
	++secs;
	while(!keypressed()) {
		_dos_gettime(&now);
		if(now.second != start.second) {
			if(!--secs)
				return 0;
			memcpy(&start, &now, sizeof(now));
			outnum(secs);
		}
		delay(100);
	}
		/* Because "keypressed()" returned true, getchar() does not block */
	return cgetchar();
}
Пример #4
0
int
iBSP430cliConsoleBufferProcessInput ()
{
  int rv;
  int c;

  rv = 0;
  if (NULL == cbEnd_) {
    cbEnd_ = consoleBuffer_;
  }
  while (0 <= ((c = cgetchar()))) {
    if (KEY_BS == c) {
      if (cbEnd_ == consoleBuffer_) {
        cputchar(KEY_BEL);
      } else {
        --cbEnd_;
        cputtext("\b \b");
      }
#if (configBSP430_CLI_COMMAND_COMPLETION - 0)
    } else if (KEY_HT == c) {
      rv |= eBSP430cliConsole_DO_COMPLETION;
      break;
#endif /* configBSP430_CLI_COMMAND_COMPLETION */
    } else if (KEY_ESC == c) {
      rv |= eBSP430cliConsole_PROCESS_ESCAPE;
      break;
    } else if (KEY_FF == c) {
      cputchar(c);
      rv |= eBSP430cliConsole_REPAINT;
      break;
    } else if (KEY_CR == c) {
      cputchar('\n');
      rv |= eBSP430cliConsole_READY;
      break;
    } else if (KEY_KILL_LINE == c) {
      cprintf("\e[%uD\e[K", (unsigned int)(cbEnd_ - consoleBuffer_));
      cbEnd_ = consoleBuffer_;
      *cbEnd_ = 0;
    } else if (KEY_KILL_WORD == c) {
Пример #5
0
/* read in a command line */
void readcommandEnhanced(char * const str, const int maxlen)
{
        unsigned char insert = 1;
        unsigned ch;
#ifdef FEATURE_FILENAME_COMPLETION
        unsigned lastch = 0;
#endif
#ifdef FEATURE_HISTORY
        int histLevel = 0;
        char prvLine[MAX_INTERNAL_COMMAND_SIZE];
#endif
        unsigned curx;
        unsigned cury;
        int count;
        unsigned current = 0;
        unsigned charcount = 0;

        assert(str);
        assert(maxlen <= MAX_INTERNAL_COMMAND_SIZE);

        /* if echo off, don't print prompt */
        if(echo)
                printprompt();

        orgx = wherex();
        orgy = wherey();
        memset(str, 0, maxlen);

        _setcursortype(_NORMALCURSOR);

#ifdef FEATURE_HISTORY
        histGet(histLevel - 1, prvLine, sizeof(prvLine));
#endif

        do {
                ch = cgetchar();

                if(cbreak)
                        ch = KEY_CTL_C;

                switch(ch) {
                case KEY_BS:               /* delete character to left of cursor */

                        if(current > 0 && charcount > 0) {
                          if(current == charcount) {     /* if at end of line */
                                str[current - 1] = 0;
                                if (wherex() != 1)
                                  outs("\b \b");
                                else
                                {
                                  goxy(MAX_X, wherey() - 1);
                                  outblank();
                                  goxy(MAX_X, wherey() - 1);
                                }
                          }
                          else
                          {
                                for (count = current - 1; count < charcount; count++)
                                  str[count] = str[count + 1];
                                if (wherex() != 1)
                                  goxy(wherex() - 1, wherey());
                                else
                                  goxy(MAX_X, wherey() - 1);
                                curx = wherex();
                                cury = wherey();
                                outsblank(&str[current - 1]);
                                goxy(curx, cury);
                          }
                          charcount--;
                          current--;
                        }
                        break;

                case KEY_INSERT:           /* toggle insert/overstrike mode */
                        insert ^= 1;
                        if (insert)
                          _setcursortype(_NORMALCURSOR);
                        else
                          _setcursortype(_SOLIDCURSOR);
                        break;

                case KEY_DELETE:           /* delete character under cursor */

                        if (current != charcount && charcount > 0)
                        {
                          for (count = current; count < charcount; count++)
                                str[count] = str[count + 1];
                          charcount--;
                          curx = wherex();
                          cury = wherey();
                          outsblank(&str[current]);
                          goxy(curx, cury);
                        }
                        break;

                case KEY_HOME:             /* goto beginning of string */

                        if (current != 0)
                        {
                          goxy(orgx, orgy);
                          current = 0;
                        }
                        break;

                case KEY_END:              /* goto end of string */

                        if (current != charcount)
                        {
                          goxy(orgx, orgy);
                          outs(str);
                          current = charcount;
                        }
                        break;

#ifdef FEATURE_FILENAME_COMPLETION
                case KEY_TAB:            /* expand current file name */
                        if(current == charcount) {      /* only works at end of line */
                          if(lastch != KEY_TAB) { /* if first TAB, complete filename */
                                complete_filename(str, charcount);
                                charcount = strlen(str);
                                current = charcount;

                                goxy(orgx, orgy);
                                outs(str);
                                if ((strlen(str) > (MAX_X - orgx)) && (orgy == MAX_Y + 1))
                                  orgy--;
                          } else {                 /* if second TAB, list matches */
                                if (show_completion_matches(str, charcount))
                                {
                                  printprompt();
                                  orgx = wherex();
                                  orgy = wherey();
                                  outs(str);
                                }
                          }
                        }
                        else
                          beep();
                        break;
#endif

                case KEY_ENTER:            /* end input, return to main */

#ifdef FEATURE_HISTORY
                        if(str[0])
                          histSet(0, str);      /* add to the history */
#endif

                        outc('\n');
                        break;

                case KEY_CTL_C:                 /* ^C */
                case KEY_ESC:              /* clear str  Make this callable! */

                        clrcmdline(str, maxlen, orgx, orgy);
                        current = charcount = 0;

                        if(ch == KEY_CTL_C && !echo) {
                          /* enable echo to let user know that's this
                                is the command line */
                          echo = 1;
                          printprompt();
                        }
                        break;

                case KEY_RIGHT:            /* move cursor right */

                        if (current != charcount)
                        {
                          current++;
                          if (wherex() == MAX_X)
                                goxy(1, wherey() + 1);
                          else
                                goxy(wherex() + 1, wherey());
                                break;
                        }
                        /* cursor-right at end of string grabs the next character
                                from the previous line */
                        /* FALL THROUGH */

#ifndef FEATURE_HISTORY
                        break;
#else
                case KEY_F1:       /* get character from last command buffer */
                          if (current < strlen(prvLine)) {
                                 outc(str[current] = prvLine[current]);
                                 charcount = ++current;
                          }
                          break;

                case KEY_F3:               /* get previous command from buffer */
                        if(charcount < strlen(prvLine)) {
                                outs(strcpy(&str[charcount], &prvLine[charcount]));
                           current = charcount = strlen(str);
                   }
                   break;

                case KEY_UP:               /* get previous command from buffer */
                        if(!histGet(--histLevel, prvLine, sizeof(prvLine)))
                                ++histLevel;            /* failed -> keep current command line */
                        else {
                                clrcmdline(str, maxlen, orgx, orgy);
                                strcpy(str, prvLine);
                                current = charcount = strlen(str);
                                outs(str);
                                histGet(histLevel - 1, prvLine, sizeof(prvLine));
                        }
                        break;

                case KEY_DOWN:             /* get next command from buffer */
                        if(histLevel) {
                                clrcmdline(str, maxlen, orgx, orgy);
                                strcpy(prvLine, str);
                                histGet(++histLevel, str, maxlen);
                                current = charcount = strlen(str);
                                outs(str);
                        }
                        break;

                case KEY_F5: /* keep cmdline in F3/UP buffer and move to next line */
                        strcpy(prvLine, str);
                        clrcmdline(str, maxlen, orgx, orgy);
                        outc('@');
                        if(orgy >= MAX_Y) {
                                outc('\n');                     /* Force scroll */
                                orgy = MAX_Y;
                        } else {
                                ++orgy;
                        }
                        goxy(orgx, orgy);
                        current = charcount = 0;

                        break;

#endif

                case KEY_LEFT:             /* move cursor left */
                        if(current > 0) {
                          current--;
                          if (wherex() == 1)
                                goxy(MAX_X, wherey() - 1);
                          else
                                goxy(wherex() - 1, wherey());
                        }
                        break;


                case KEY_CTRL_LEFT:     /* move cursor left to begin of word */
                        while(current > 0) {
                          current--;
                          if (wherex() == 1)
                                goxy(MAX_X, wherey() - 1);
                          else
                                goxy(wherex() - 1, wherey());

                          if(isworddelimiter(str[current-1])    /* ignore current == 0 */
                           && !isworddelimiter(str[current]))
                             break;
                        }
                        break;

                case KEY_CTRL_RIGHT:    /* move cursor right to begin of word */
                        while(current < charcount) {
                          current++;
                          if (wherex() == MAX_X)
                                goxy(1, wherey() + 1);
                          else
                                goxy(wherex() + 1, wherey());

                          if(isworddelimiter(str[current-1])
                           && !isworddelimiter(str[current]))
                             break;
                        }
                        break;

                default:                 /* insert character into string... */

                        if ((ch >= 32 && ch <= 255) && (charcount != (maxlen - 2)))
                        {
                          if (insert && current != charcount)
                          {
                                for (count = charcount; count > current; count--)
                                  str[count] = str[count - 1];
                                str[current++] = ch;
                                curx = wherex() + 1;
                                cury = wherey();
                                outs(&str[current - 1]);
                                if ((strlen(str) > (MAX_X - orgx)) && (orgy == MAX_Y + 1))
                                  cury--;
                                goxy(curx, cury);
                                charcount++;
                          }
                          else
                          {
                                if (current == charcount)
                                  charcount++;
                                str[current++] = ch;
                                outc(ch);
                          }
                          if ((strlen(str) > (MAX_X - orgx)) && (orgy == MAX_Y + 1))
                                orgy--;
                        }
                        else
                          beep();
                        break;
                }
#ifdef FEATURE_FILENAME_COMPLETION
                lastch = ch;
#endif
        } while(ch != KEY_ENTER);

        _setcursortype(_NORMALCURSOR);
}
Пример #6
0
INT cmd_replace (LPTSTR param)
{
    LPTSTR *arg;
    INT argc, i,filesReplaced = 0, nFiles, srcIndex = -1, destIndex = -1;
    DWORD dwFlags = 0;
    TCHAR szDestPath[MAX_PATH], szSrcPath[MAX_PATH], tmpSrcPath[MAX_PATH];
    BOOL doMore = TRUE;

    /* Help wanted? */
    if (!_tcsncmp (param, _T("/?"), 2))
    {
        ConOutResPaging(TRUE,STRING_REPLACE_HELP1);
        return 0;
    }

    /* Divide the argument in to an array of c-strings */
    arg = split (param, &argc, FALSE, FALSE);
    nFiles = argc;

    /* Read options */
    for (i = 0; i < argc; i++)
    {
        if (arg[i][0] == _T('/'))
        {
            if (_tcslen(arg[i]) == 2)
            {
                switch (_totupper(arg[i][1]))
                {
                case _T('A'):
                    dwFlags |= REPLACE_ADD;
                    break;
                case _T('P'):
                    dwFlags |= REPLACE_CONFIRM;
                    break;
                case _T('R'):
                    dwFlags |= REPLACE_READ_ONLY;
                    break;
                case _T('S'):
                    dwFlags |= REPLACE_SUBDIR;
                    break;
                case _T('W'):
                    dwFlags |= REPLACE_DISK;
                    break;
                case _T('U'):
                    dwFlags |= REPLACE_UPDATE;
                    break;
                default:
                    invalid_switch(arg[i]);
                    return 0;
                }
            }
            else
            {
                invalid_switch(arg[i]);
                freep(arg);
                return 0;
            }
            nFiles--;
        }
        else
        {
            if (srcIndex == -1)
            {
                srcIndex = i;
            }
            else if (destIndex == -1)
            {
                destIndex = i;
            }
            else
            {
                invalid_switch(arg[i]);
                freep(arg);
                return 0;
            }
        }
    }

    /* See so that at least source is there */
    if (nFiles < 1)
    {
        ConOutResPaging(TRUE,STRING_REPLACE_HELP2);
        ConOutResPaging(TRUE,STRING_REPLACE_HELP3);
        freep(arg);
        return 1;
    }
    /* Check so that not both update and add switch is added and subdir */
    if ((dwFlags & REPLACE_UPDATE || dwFlags & REPLACE_SUBDIR) && (dwFlags & REPLACE_ADD))
    {
        ConOutResPaging(TRUE,STRING_REPLACE_ERROR4);
        ConOutResPaging(TRUE,STRING_REPLACE_HELP7);
        freep(arg);
        return 1;
    }

    /* If we have a destination get the full path */
    if (destIndex != -1)
    {
        if (_tcslen(arg[destIndex]) == 2 && arg[destIndex][1] == ':')
            GetRootPath(arg[destIndex],szDestPath,MAX_PATH);
        else
        {
            /* Check for wildcards in destination directory */
            if (_tcschr (arg[destIndex], _T('*')) != NULL ||
                _tcschr (arg[destIndex], _T('?')) != NULL)
            {
                ConOutResPrintf(STRING_REPLACE_ERROR2,arg[destIndex]);
                ConOutResPaging(TRUE,STRING_REPLACE_HELP3);
                freep(arg);
                return 1;
            }
            getPath(szDestPath, arg[destIndex]);
            /* Make sure that destination exists */
            if (!IsExistingDirectory(szDestPath))
            {
                ConOutResPrintf(STRING_REPLACE_ERROR2, szDestPath);
                ConOutResPaging(TRUE,STRING_REPLACE_HELP3);
                freep(arg);
                return 1;
            }
        }
    }
    else
    {
        /* Dest is current dir */
        GetCurrentDirectory(MAX_PATH,szDestPath);
    }

    /* Get the full source path */
    if (!(_tcslen(arg[srcIndex]) == 2 && arg[srcIndex][1] == ':'))
        getPath(szSrcPath, arg[srcIndex]);
    else
        _tcscpy(szSrcPath,arg[srcIndex]);

    /* Source does not have wildcards */
    if (_tcschr (arg[srcIndex], _T('*')) == NULL &&
        _tcschr (arg[srcIndex], _T('?')) == NULL)
    {
        /* Check so that source is not a directory, because that is not allowed */
        if (IsExistingDirectory(szSrcPath))
        {
            ConOutResPrintf(STRING_REPLACE_ERROR6, szSrcPath);
            ConOutResPaging(TRUE,STRING_REPLACE_HELP3);
            freep(arg);
            return 1;
        }
        /* Check if the file exists */
        if (!IsExistingFile(szSrcPath))
        {
            ConOutResPaging(TRUE,STRING_REPLACE_HELP3);
            freep(arg);
            return 1;
        }
    }
    /* /w switch is set so wait for any key to be pressed */
    if (dwFlags & REPLACE_DISK)
    {
        msg_pause();
        cgetchar();
    }

    /* Add an extra \ to the destination path if needed */
    if (szDestPath[_tcslen(szDestPath) -  1] != _T('\\'))
        _tcscat(szDestPath, _T("\\"));

    /* Save source path */
    _tcscpy(tmpSrcPath,szSrcPath);
    /* Replace in dest dir */
    filesReplaced += recReplace(dwFlags, tmpSrcPath, szDestPath, &doMore);
    /* If subdir switch is set replace in the subdirs to */
    if (dwFlags & REPLACE_SUBDIR && doMore)
    {
        filesReplaced += recFindSubDirs(dwFlags, szSrcPath,  szDestPath, &doMore);
    }

    /* If source == dest write no more */
    if (filesReplaced != -1)
    {
        /* No files replaced */
        if (filesReplaced==0)
        {
            /* Add switch dependent output */
            if (dwFlags & REPLACE_ADD)
                ConOutResPaging(TRUE,STRING_REPLACE_HELP7);
            else
                ConOutResPaging(TRUE,STRING_REPLACE_HELP3);
        }
        /* Some files replaced */
        else
        {
            /* Add switch dependent output */
            if (dwFlags & REPLACE_ADD)
                ConOutResPrintf(STRING_REPLACE_HELP8, filesReplaced);
            else
                ConOutResPrintf(STRING_REPLACE_HELP4, filesReplaced);
        }
    }
    /* Return memory */
    freep(arg);
    return 1;
}
Пример #7
0
void main ()
{
  hBSP430halSERIAL i2c;
  sBSP430bq24210 bq24210;
  union {
    sBQ27510 state;
    uint16_t raw[1];
  } u;
  const int nwords = sizeof(u.state)/sizeof(u.raw[0]);
  unsigned long resample_interval_utt;
  unsigned long resample_wake_utt;
  unsigned int flags;
  int rc;

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();

  cprintf("\nbattpack " __DATE__ " " __TIME__ "\n");

  bq24210.chg_port = xBSP430hplLookupPORT(APP_CHGn_PORT_PERIPH_HANDLE);
  bq24210.en_port = xBSP430hplLookupPORT(APP_ENn_PORT_PERIPH_HANDLE);
  bq24210.pg_port = xBSP430hplLookupPORT(APP_PGn_PORT_PERIPH_HANDLE);
  bq24210.chg_bit = APP_CHGn_PORT_BIT;
  bq24210.en_bit = APP_ENn_PORT_BIT;
  bq24210.pg_bit = APP_PGn_PORT_BIT;

  cprintf("CHGn on %s.%u\n",
          xBSP430portName(xBSP430periphFromHPL(bq24210.chg_port)),
          iBSP430portBitPosition(bq24210.chg_bit));
  cprintf("ENn on %s.%u\n",
          xBSP430portName(xBSP430periphFromHPL(bq24210.en_port)),
          iBSP430portBitPosition(bq24210.en_bit));
  cprintf("PGn on %s.%u\n",
          xBSP430portName(xBSP430periphFromHPL(bq24210.pg_port)),
          iBSP430portBitPosition(bq24210.pg_bit));
  if (! (bq24210.chg_port && bq24210.en_port && bq24210.pg_port)) {
    cprintf("One of the ports is missing\n");
    return;
  }

  /* Charge signal is an input (active low) to the MCU.  Configure as
   * input with internal pull-up. */
  bq24210.chg_port->dir &= ~bq24210.chg_bit;
  bq24210.chg_port->out |= bq24210.chg_bit;
  bq24210.chg_port->ren |= bq24210.chg_bit;

  /* Power-good signal is an input (active low) to the MCU.  Configure
   * as input with internal pull-up. */
  bq24210.pg_port->dir &= ~bq24210.pg_bit;
  bq24210.pg_port->out |= bq24210.pg_bit;
  bq24210.pg_port->ren |= bq24210.pg_bit;

  /* Enable signal is an output (active low) from the MCU.  Start
   * active. */
  bq24210.en_port->out &= ~bq24210.en_bit;
  bq24210.en_port->dir |= bq24210.en_bit;

  cprintf("I2C on %s at address 0x%02x\nPins: %s\n",
          xBSP430serialName(APP_BQ27510_I2C_PERIPH_HANDLE),
          APP_BQ27510_I2C_ADDRESS,
          xBSP430platformPeripheralHelp(APP_BQ27510_I2C_PERIPH_HANDLE, BSP430_PERIPHCFG_SERIAL_I2C));

  /* NOTE: At default BSP430_SERIAL_I2C_BUS_SPEED_HZ 400kHz this
   * devices supports only single-byte write operations.  Further,
   * ensure a 66us delay between packets. */
  i2c = hBSP430serialOpenI2C(hBSP430serialLookup(APP_BQ27510_I2C_PERIPH_HANDLE),
                             BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(UCMST),
                             0, 0);
  if (! i2c) {
    cprintf("I2C open failed\n");
    return;
  }
  (void)iBSP430i2cSetAddresses_rh(i2c, -1, APP_BQ27510_I2C_ADDRESS);

  resample_interval_utt = BSP430_UPTIME_MS_TO_UTT(1000UL * RESAMPLE_INTERVAL_S);
  resample_wake_utt = ulBSP430uptime_ni();
  flags = FLG_DUMP_STATE | FLG_UPDATE_INTERVAL;

  BSP430_CORE_ENABLE_INTERRUPT();
  while (1) {
    char astext_buf[BSP430_UPTIME_AS_TEXT_LENGTH];
    uint16_t temperature_dC;

    if (FLG_DUMP_STATE & flags) {
      memset(&u, 0, sizeof(u));
      rc = readBQ27510(i2c, 0, nwords, u.raw);
      cprintf("Device ID %04x rc %d\n", u.state.device_id, rc);
      cprintf("%.30s = %d\n", "atRate_mA", u.state.atRate_mA);
      cprintf("%.30s = %u\n", "atRatetimeToEmpty_min", u.state.atRatetimeToEmpty_min);
      cprintf("%.30s = %u\n", "temperature_dK", u.state.temperature_dK);
      cprintf("%.30s = %u\n", "voltage_mV", u.state.voltage_mV);
      cprintf("%.30s = 0x%04x\n", "flags", u.state.flags);
      cprintf("%.30s = %u\n", "nominalAvailableCapacity_mAh", u.state.nominalAvailableCapacity_mAh);
      cprintf("%.30s = %u\n", "fullAvailableCapacity_mAh", u.state.fullAvailableCapacity_mAh);
      cprintf("%.30s = %u\n", "remainingCapacity_mAh", u.state.remainingCapacity_mAh);
      cprintf("%.30s = %u\n", "fullChargeCapacity_mAh", u.state.fullChargeCapacity_mAh);
      cprintf("%.30s = %d\n", "averageCurrent_mA", u.state.averageCurrent_mA);
      cprintf("%.30s = %u\n", "timeToEmpty_min", u.state.timeToEmpty_min);
      cprintf("%.30s = %d\n", "standbyCurrent_mA", u.state.standbyCurrent_mA);
      cprintf("%.30s = %u\n", "standbyTimeToEmpty_min", u.state.standbyTimeToEmpty_min);
      cprintf("%.30s = %u\n", "stateOfHealth_ppcpx", u.state.stateOfHealth_ppcpx);
      cprintf("%.30s = %u\n", "cycleCount", u.state.cycleCount);
      cprintf("%.30s = %u\n", "stateOfCharge_ppc", u.state.stateOfCharge_ppc);
      cprintf("%.30s = %d\n", "instantaneousCurrent_mA", u.state.instantaneousCurrent_mA);
      cprintf("%.30s = %u\n", "internalTemperature_dK", u.state.internalTemperature_dK);
      cprintf("%.30s = %u\n", "reistanceScale", u.state.reistanceScale);
      cprintf("%.30s = %u\n", "operationConfiguration", u.state.operationConfiguration);
      cprintf("%.30s = %u\n", "designCapacity_mAh", u.state.designCapacity_mAh);
      cprintf("flags %02x ; ENn state %d\n", flags, (bq24210.en_port->out & bq24210.en_bit));
      flags &= ~FLG_DUMP_STATE;
    }
    if (FLG_TOGGLE_ENABLE & flags) {
      bq24210.en_port->out ^= bq24210.en_bit;
      flags &= ~FLG_TOGGLE_ENABLE;
    }
    vBSP430ledSet(BSP430_LED_GREEN, !(bq24210.en_port->out & bq24210.en_bit));

    rc = readBQ27510(i2c, 0, nwords, u.raw);
    temperature_dC = u.state.temperature_dK - 2733;

    cprintf("%s: %c%c%c % 2d.%dC  %4dmV ; SoC %u%% ; Cap %4d / %4d ; %dmA ~ %dmA / %u\n",
            xBSP430uptimeAsText(ulBSP430uptime(), astext_buf),
            (bq24210.en_port->out & bq24210.en_bit) ? ' ' : 'E',
            (bq24210.chg_port->in & bq24210.chg_bit) ? ' ' : 'C',
            (bq24210.pg_port->in & bq24210.pg_bit) ? ' ' : 'P',
            (temperature_dC / 10), (temperature_dC % 10),
            u.state.voltage_mV,
            u.state.stateOfCharge_ppc,
            u.state.remainingCapacity_mAh,
            u.state.fullAvailableCapacity_mAh,
            u.state.instantaneousCurrent_mA,
            u.state.averageCurrent_mA,
            u.state.cycleCount
            );
    if (FLG_UPDATE_INTERVAL & flags) {
      resample_wake_utt += resample_interval_utt;
      flags &= ~FLG_UPDATE_INTERVAL;
    }
    flags = 0;
    while (! flags) {
      iBSP430consoleFlush();
      if (0 >= lBSP430uptimeSleepUntil(resample_wake_utt, LPM3_bits)) {
        flags |= FLG_UPDATE_INTERVAL;
      }
      while (0 <= ((rc = cgetchar()))) {
        if ('!' == rc) {
          flags |= FLG_TOGGLE_ENABLE;
        } else if (' ' == rc) {
          flags |= FLG_DUMP_STATE;
        }
      }
    }

  }
}
Пример #8
0
void main ()
{
  const char * command;
  int flags;

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();
  vBSP430cliSetDiagnosticFunction(iBSP430cliConsoleDiagnostic);
  cprintf("\ncli example " __DATE__ " " __TIME__ "\n");
#if configBSP430_CLI_COMMAND_COMPLETION - 0
  cprintf("Command completion is available.\n");
#endif /* configBSP430_CLI_COMMAND_COMPLETION */
  vBSP430ledSet(0, 1);
  cprintf("\nLED lit when not awaiting input\n");

  /* NOTE: The control flow in this is a bit tricky, as we're trying
   * to leave interrupts enabled during the main body of the loop,
   * while they must be disabled when processing input to recognize a
   * command.  The flags variable preserves state across multiple loop
   * iterations until all relevant activities have completed. */
  commandSet = LAST_COMMAND;
  command = NULL;
  flags = eBSP430cliConsole_REPAINT;

  BSP430_CORE_ENABLE_INTERRUPT();
  while (1) {
    if (flags & eBSP430cliConsole_ANY_ESCAPE) {
      int c;
      while (0 <= ((c = cgetchar()))) {
        cprintf("escape char 0x%02x (%u) '%c'\n", c, c, isprint(c) ? c : '.');
        /* Technically CSI is a single character 0x9b representing
         * ESC+[.  In the two-character mode accepted here, we use the
         * value for the second character. */
#define KEY_CSI '['
        if ((KEY_CSI == c) && (flags & eBSP430cliConsole_PROCESS_ESCAPE)) {
          flags &= ~eBSP430cliConsole_PROCESS_ESCAPE;
          flags |= eBSP430cliConsole_IN_ESCAPE;
        } else if ((64 <= c) && (c <= 126)) {
          flags &= ~eBSP430cliConsole_ANY_ESCAPE;
          cprintf("Leaving escape mode\n");
          break;
        }
      }
    }
    if (flags & eBSP430cliConsole_DO_COMPLETION) {
      flags &= ~eBSP430cliConsole_DO_COMPLETION;
      flags |= iBSP430cliConsoleBufferCompletion(commandSet, &command);
    }
    if (flags & eBSP430cliConsole_READY) {
      int rv;

      rv = iBSP430cliExecuteCommand(commandSet, 0, command);
      if (0 != rv) {
        cprintf("Command execution returned %d\n", rv);
      }
      /* Ensure prompt is rewritten, but not the command we just
       * ran */
      flags |= eBSP430cliConsole_REPAINT;
      command = NULL;
    }
    if (flags & eBSP430cliConsole_REPAINT) {
      /* Draw the prompt along with whatever's left in the command
       * buffer.  Note use of leading carriage return in case an edit
       * left material on the current line. */
      cprintf("\r> %s", command ? command : "");
      flags &= ~eBSP430cliConsole_REPAINT;
    }
    if (flags & eBSP430cliConsole_REPAINT_BEL) {
      cputchar('\a');
      flags &= ~eBSP430cliConsole_REPAINT_BEL;
    }
    BSP430_CORE_DISABLE_INTERRUPT();
    do {
      if (flags & eBSP430cliConsole_READY) {
        /* Clear the command we just completed */
        vBSP430cliConsoleBufferClear_ni();
        flags &= ~eBSP430cliConsole_READY;
      }
      do {
        /* Unless we're processing application-specific escape
         * characters let iBSP430cliConsoleBufferProcessInput_ni()
         * process any input characters that have already been
         * received. */
        if (! (flags & eBSP430cliConsole_ANY_ESCAPE)) {
          flags |= iBSP430cliConsoleBufferProcessInput_ni();
        }
        if (0 == flags) {
          /* Sleep until something wakes us, such as console input.
           * Then turn off interrupts and loop back to read that
           * input. */
          vBSP430ledSet(0, 0);
          BSP430_CORE_LPM_ENTER_NI(LPM0_bits);
          BSP430_CORE_DISABLE_INTERRUPT();
          vBSP430ledSet(0, 1);
        }
        /* Repeat if still nothing to do */
      } while (! flags);

      /* Got something to do; get the command contents in place so
       * we can update the screen. */
      command = xBSP430cliConsoleBuffer_ni();
    } while (0);
    BSP430_CORE_ENABLE_INTERRUPT();
  }
}