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:
- 14:73ef945b8def
- Parent:
- 13:019e24491f32
- Child:
- 15:a32db434af65
--- a/shell.cpp Fri Apr 18 21:33:57 2014 +0000 +++ b/shell.cpp Thu Apr 24 23:27:17 2014 +0000 @@ -158,6 +158,7 @@ if(findString("Ralph")) sendText(" -- Hey, that's me!"); if(findString("turn")) parseLEDState(); + if(findString("generate")) parseGenerate(); } //----------------------------------------------------------------------------- @@ -224,6 +225,13 @@ } //----------------------------------------------------------------------------- +void Shell::parseGenerate() +{ + if(findString("pi")) generatePi(); + else sendText(" -- generate what?"); +} +//----------------------------------------------------------------------------- + void Shell::reportPushButtonPress(ePushButton thisPushButton) { switch(thisPushButton) { @@ -232,4 +240,54 @@ } sendText(" pressed\n> "); } -//----------------------------------------------------------------------------- \ No newline at end of file +//----------------------------------------------------------------------------- + +#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 = "); + + 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++) { + 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); + + } + 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} + predigit = 0; + nines = 0; + } else { + usbSerial->printf("%d",predigit); + predigit = q; + if (nines != 0) { // then + for(k=1; k<=nines; k++) // do + usbSerial->printf("9"); + nines = 0; + } + } + digits++; + digits+=nines; + } + } +} +//-----------------------------------------------------------------------------