Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Wen-Liang Lin
Published © GPL3+

Nilakantha series

Calculate Pi with Nilakantha series at Hexiwear

BeginnerProtip1 hour5,020
Nilakantha series

Things used in this project

Hardware components

Hexiwear
NXP Hexiwear
×1

Hand tools and fabrication machines

mbed
Arm mbed

Story

Read more

Code

Hexi_OLED_Pi_calculate

C/C++
Editor from Hexi_OLED_Text_Example
#include "mbed.h"
#include "Hexi_OLED_SSD1351.h"
#include "string.h"

int main()
{
    char text[40];  /* Text Buffer */
    Timer time;     /* Instantiate Time */

    /* Instantiate the SSD1351 OLED Driver */
    SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */

    /* Get OLED Class Default Text Properties */
    oled_text_properties_t textProperties = {0};
    oled.GetTextProperties(&textProperties);

    /* Turn on the backlight of the OLED Display */
    oled.DimScreenON();

    /* Fills the screen with solid black */
    oled.FillScreen(COLOR_BLACK);

    /* Display Text at (x=7,y=0) */
    strcpy((char *) text,"Nilakantha");
    oled.Label((uint8_t *)text,7,0);

    /* Change font color to GREEN */
    textProperties.fontColor   = COLOR_GREEN;
    oled.SetTextProperties(&textProperties);

    /* Display text at (x=5,y=25) */
    strcpy(text,"Pi=");
    oled.Label((uint8_t *)text,5,25);
    /* Display text at (x=5,y=55) */  
    strcpy(text,"Time use=");
    oled.Label((uint8_t *)text,5,55);
    /* Set text properties to white and right aligned for the dynamic text */
    textProperties.fontColor = COLOR_WHITE;
    textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
    oled.SetTextProperties(&textProperties);


    time.start(); /* start timer */
    double pi=3.0;
    long i=1;

    while (true) {

        /* Format the time reading */
        sprintf(text,"%f",pi);

        /* Display time reading in 90px by 15px textbox at(x=5, y=40) */
        oled.TextBox((uint8_t *)text,5,40,90,15); //Increase textbox for more digits

        sprintf(text,"%f",time.read());

        /* Display time reading in 90px by 15px textbox at(x=5, y=70) */
        oled.TextBox((uint8_t *)text,5,70,90,15); //Increase textbox for more digits

        Thread::wait(1000);
        //pi calculate
        if((i%2)==1) {
            pi = pi + 4.0/(2.0*i*(2.0*i+1.0)*(2.0*i+2.0));
        } else {
            pi = pi - 4.0/(2.0*i*(2.0*i+1.0)*(2.0*i+2.0));
        }
        i++;

    }
}

Credits

Wen-Liang Lin

Wen-Liang Lin

29 projects • 35 followers
Hi, I am momososo

Comments