
bubbles example
Fork of dm_bubbles by
Revision 0:70db0a4adfce, committed 2014-05-20
- Comitter:
- displaymodule
- Date:
- Tue May 20 15:33:57 2014 +0000
- Child:
- 1:e286034c8f6a
- Commit message:
- First version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BubbleDemo.cpp Tue May 20 15:33:57 2014 +0000 @@ -0,0 +1,170 @@ +/****************************************************************************** + * Includes + *****************************************************************************/ + +#include "mbed.h" + +#include "BubbleDemo.h" +#include "DmTftBase.h" +#include "wchar.h" + +/****************************************************************************** + * Typedefs and defines + *****************************************************************************/ + +#define PI 3.1415926535897932384626433832795 + +/* Red color mask, 565 mode */ +#define REDMASK 0xF800 +/* Red shift value, 565 mode */ +#define REDSHIFT 11 +/* Green color mask, 565 mode */ +#define GREENMASK 0x07E0 +/* Green shift value, 565 mode */ +#define GREENSHIFT 5 +/* Blue color mask, 565 mode */ +#define BLUEMASK 0x001F +/* Blue shift value, 565 mode */ +#define BLUESHIFT 0 + +/* Number of colors in 565 mode */ +#define NUM_COLORS 65536 +/* Number of red colors in 565 mode */ +#define RED_COLORS 0x20 +/* Number of green colors in 565 mode */ +#define GREEN_COLORS 0x40 +/* Number of blue colors in 565 mode */ +#define BLUE_COLORS 0x20 + +/****************************************************************************** + * Local variables + *****************************************************************************/ + + +/****************************************************************************** + * External variables + *****************************************************************************/ + + +/****************************************************************************** + * Local functions + *****************************************************************************/ + + +void BubbleDemo::initialize() { + float radian; + const float phase1 = 2 * PI / 3; + const float phase2 = 4 * PI / 3; + + for(i = 0; i < NumBalls; i++) { + x[i] = this->windowX / 2; + y[i] = this->windowY / 2; + r[i] = i * 2 + 10; + + oldX[i] = x[i]; + oldY[i] = y[i]; + + velX[i] = 1; + velY[i] = 1; + + radian = i * 2 * PI / NumBalls; + red[i] = cos(radian) * RED_COLORS / 2 + (RED_COLORS / 2 - 1); + green[i] = cos(radian + phase2) * GREEN_COLORS / 2 + (GREEN_COLORS / 2 - 1); + blue[i] = cos(radian + phase1) * BLUE_COLORS / 2 + (BLUE_COLORS / 2 - 1); + } +} + +void BubbleDemo::collision() { + float disX = x[j] - x[i]; + float disY = y[j] - y[i]; + float d2 = disX * disX + disY * disY; + + if(d2 != 0) { + float rij = r[i] + r[j]; + float rij2 = rij * rij; + + if(d2 < rij2) { + float ii = (disX * velX[i] + disY * velY[i]) / d2; + float ji = (disX * velY[i] - disY * velX[i]) / d2; + float ij = (disX * velX[j] + disY * velY[j]) / d2; + float jj = (disX * velY[j] - disY * velX[j]) / d2; + float ratio = rij / sqrt(d2); + + velX[i] = ij * disX - ii * disY; + velY[i] = ij * disY + ii * disX; + velX[j] = ji * disX - jj * disY; + velY[j] = ji * disY + jj * disX; + + disX *= (ratio - 1) / 2; + disY *= (ratio - 1) / 2; + + x[j] += disX; + y[j] += disY; + x[i] -= disX; + y[i] -= disY; + } + } +} + +void BubbleDemo::borders() { + if(x[i] >= this->windowX - r[i] - 1) { + x[i] = this->windowX - r[i] - 1; + velX[i] = -velX[i]; + } else if(x[i] <= r[i]) { + x[i] = r[i]; + velX[i] = -velX[i]; + } + + if(y[i] >= this->windowY - r[i] - 1) { + y[i] = this->windowY - r[i] - 1; + velY[i] = -velY[i]; + } else if(y[i] <= r[i]) { + y[i] = r[i]; + velY[i] = -velY[i]; + } +} + +void BubbleDemo::draw() { + tft->drawCircle(oldX[i], oldY[i], r[i], BLACK); + tft->drawCircle(x[i], y[i], r[i], (red[i] << REDSHIFT) + (green[i] << GREENSHIFT) + (blue[i] << BLUESHIFT)); + + oldX[i] = x[i]; + oldY[i] = y[i]; +} + + +/****************************************************************************** + * Public functions + *****************************************************************************/ + +BubbleDemo::BubbleDemo(DmTftBase* display, uint16_t dispWidth, uint16_t dispHeight) { + + this->windowX = dispWidth; + this->windowY = dispHeight; + tft = display; + + initialize(); +} + +void BubbleDemo::run(uint32_t loops, uint32_t delayMs) { + + tft->clearScreen(); + + for(int32_t n=0;n<loops;n++) { + + for(i = 0; i < NumBalls; i++) { + x[i] += velX[i]; + y[i] += velY[i]; + + for(j = i + 1; j < NumBalls; j++) + collision(); + + borders(); + + if((int)x[i] != (int)oldX[i] || (int)y[i] != (int)oldY[i]) + draw(); + } + + wait_ms(delayMs); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BubbleDemo.h Tue May 20 15:33:57 2014 +0000 @@ -0,0 +1,68 @@ + +#ifndef BUBBLEDEMO_H +#define BUBBLEDEMO_H + +#include "DmTftBase.h" + +class BubbleDemo { +public: + + enum Constants { + NumBalls = 4 // 17 + }; + + /** Set the address of the frame buffer to use. + * + * It is the content of the frame buffer that is shown on the + * display. All the drawing on the frame buffer can be done + * 'offline' and whenever it should be shown this function + * can be called with the address of the offline frame buffer. + * + * @param pFrameBuf Pointer to the frame buffer, which must be + * 3 times as big as the frame size (for tripple + * buffering). + * dispWidth The width of the display (in pixels). + * dispHeight The height of the display (in pixels). + * loops Number of loops in the demo code. + * delayMs Delay in milliseconds between schreen updates. + * + * @returns + * none + */ + BubbleDemo(DmTftBase* display, uint16_t dispWidth, uint16_t dispHeight); + + void run(uint32_t loops, uint32_t delayMs); + +private: + int32_t windowX; + int32_t windowY; + + DmTftBase* tft; + + uint8_t i; + uint8_t j; + + float x[NumBalls]; + float y[NumBalls]; + uint8_t r[NumBalls]; + + float oldX[NumBalls]; + float oldY[NumBalls]; + + float velX[NumBalls]; + float velY[NumBalls]; + + uint8_t red[NumBalls]; + uint8_t green[NumBalls]; + uint8_t blue[NumBalls]; + + + void initialize(); + void collision(); + void borders(); + void draw(); +}; + +#endif /* BUBBLEDEMO_H */ + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DmTftLibrary.lib Tue May 20 15:33:57 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/displaymodule/code/DmTftLibrary/#59be7fca4581
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 20 15:33:57 2014 +0000 @@ -0,0 +1,83 @@ +/****************************************************************************** + * Includes + *****************************************************************************/ + +#include "mbed.h" + +#include "BubbleDemo.h" + +#include "DmTftHX8353C.h" +#include "DmTftS6D0164.h" +#include "DmTftIli9325.h" +#include "DmTftIli9341.h" +#include "DmTftSsd2119.h" + +/****************************************************************************** + * Typedefs and defines + *****************************************************************************/ + +#define RESET_FLAG \ + do { \ + if (abortTest) { \ + abortTest = false; \ + wait(0.04); \ + } \ + } while(false) + + +#if 1 /* Displays without adapter */ +#define DM_PIN_SPI_MOSI D11 +#define DM_PIN_SPI_MISO D12 +#define DM_PIN_SPI_SCLK D13 + +#define DM_PIN_CS_TOUCH D4 +#define DM_PIN_CS_TFT D10 +#define DM_PIN_CS_SDCARD D8 +#define DM_PIN_CS_FLASH D6 +#else /* Displays with adapter */ +#define DM_PIN_SPI_MOSI A0 +#define DM_PIN_SPI_MISO D9 +#define DM_PIN_SPI_SCLK A1 + +#define DM_PIN_CS_TOUCH D8 +#define DM_PIN_CS_TFT A3 +#define DM_PIN_CS_SDCARD D10 +#endif + +/****************************************************************************** + * Local variables + *****************************************************************************/ + +static InterruptIn buttonInterrupt(P2_10); +static DigitalOut led(LED1); + +//DmTftHX8353C tft; /* DM_TFT18_101 */ +//DmTftS6D0164 tft; /* DM_TFT22_102 */ +//DmTftIli9325 tft; /* DM_TFT28_103 and DM_TFT24_104 */ +DmTftIli9341 tft; /* DM_TFT28_105 */ +//DmTftSsd2119 tft; /* DM_TFT35_107 */ + +DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1); +DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1); +DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1); +#ifdef DM_PIN_CS_FLASH + DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1); +#endif + +/****************************************************************************** + * Global variables + *****************************************************************************/ + +/****************************************************************************** + * Main + *****************************************************************************/ + +int main (void) +{ + tft.init(); + + BubbleDemo bubbleDemo(&tft, tft.width(), tft.height()); + while (1) { + bubbleDemo.run(750, 20); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue May 20 15:33:57 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877 \ No newline at end of file