Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of frdm_echo by
Diff: shell.cpp
- Revision:
- 15:a32db434af65
- Parent:
- 14:73ef945b8def
--- a/shell.cpp Thu Apr 24 23:27:17 2014 +0000 +++ b/shell.cpp Fri Apr 25 16:22:54 2014 +0000 @@ -159,6 +159,7 @@ if(findString("turn")) parseLEDState(); if(findString("generate")) parseGenerate(); + if(findString("get")) parseGet(); } //----------------------------------------------------------------------------- @@ -232,6 +233,13 @@ } //----------------------------------------------------------------------------- +void Shell::parseGet() +{ + if(findString("pi")) generatePi(); + else sendText(" -- get what?"); +} +//----------------------------------------------------------------------------- + void Shell::reportPushButtonPress(ePushButton thisPushButton) { switch(thisPushButton) { @@ -242,52 +250,47 @@ } //----------------------------------------------------------------------------- -#define n 1000//1125 -#define len 10*n/3 - void Shell::generatePi() { - int i, j, k, q, x, nines, predigit, digits; - int a[len]; - - sendText(" -- Pi = "); + const int n = 1000, len = 10 * n / 3 + 1; + int i, j, k, q = 0, x, nines = 0, predigit = 0, digits = 0, a[len]; - digits=0; - for (j=1; j<=len; j++) - a[j] = 2; - nines = 0; - predigit = 0;// {First predigit is a 0} - for (j=1; j<=n; j++) { + decimalCounter = 0; + for(j = 0; j < len; j++) a[j] = 2; + for(j = 0; j < n; j++) { q = 0; - for(i=len; i>0; i--) { - x = 10*a[i] + q*i; - a[i] = x % (2*i - 1); - q = x / (2*i - 1); - + for(i = len; i > 0; i--) { + x = 10 * a[i] + q * i; + a[i] = x % (2 * i - 1); + q = x / (2 * i - 1); } a[1] = q % 10; - q = q / 10; - if (q == 9) { - nines++; - } else { - if (q == 10) { - usbSerial->printf("%d",predigit+1); - for (k=1; k<=nines; k++) // to nines do - usbSerial->printf("0");// {zeros} + q /= 10; + if(q == 9) nines++; + else { + if(q == 10) { + sendDecimal(predigit + 1); + for(k = 0; k < nines; k++) sendDecimal(0); predigit = 0; - nines = 0; } else { - usbSerial->printf("%d",predigit); + sendDecimal(predigit); predigit = q; - if (nines != 0) { // then - for(k=1; k<=nines; k++) // do - usbSerial->printf("9"); - nines = 0; - } + for(k = 0; k < nines; k++) sendDecimal(9); } - digits++; - digits+=nines; + nines = 0; + digits += nines + 1; } } } //----------------------------------------------------------------------------- + +void Shell::sendDecimal(uint8_t decimal) +{ + switch(decimalCounter) { + case 0: break; + case 1: usbSerial->printf(" -- pi = %d.", decimal); break; + default: usbSerial->printf("%d", decimal); + } + decimalCounter++; +} +//-----------------------------------------------------------------------------