Several examples run on only mbed-os5.13.0 (not 5.14.0)
Dependencies: BD_SD_DISCO_F769NI BSP_DISCO_F769NI LCD_DISCO_F769NI TS_DISCO_F769NI USBHost_F769NI
z_example/7_Mandelbrot.cpp@4:0f4affc00183, 2019-10-14 (annotated)
- Committer:
- kenjiArai
- Date:
- Mon Oct 14 08:55:15 2019 +0000
- Revision:
- 4:0f4affc00183
Several Examples
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 4:0f4affc00183 | 1 | //--------------------------------------------------------------- |
kenjiArai | 4:0f4affc00183 | 2 | // Mandelbrot set generator |
kenjiArai | 4:0f4affc00183 | 3 | // |
kenjiArai | 4:0f4affc00183 | 4 | // Mandelbrot set is displayed using 3 kinds of color pattern. |
kenjiArai | 4:0f4affc00183 | 5 | // Tap the Mandelbrot set on the screen, expanded figure by a |
kenjiArai | 4:0f4affc00183 | 6 | // factor of two around the tapped position is displayed. |
kenjiArai | 4:0f4affc00183 | 7 | // |
kenjiArai | 4:0f4affc00183 | 8 | // 2016/02/22, Copyright (c) 2016 MIKAMI, Naoki |
kenjiArai | 4:0f4affc00183 | 9 | //--------------------------------------------------------------- |
kenjiArai | 4:0f4affc00183 | 10 | // https://os.mbed.com/users/MikamiUitOpen/code/F746_Mandelbrot/ |
kenjiArai | 4:0f4affc00183 | 11 | // |
kenjiArai | 4:0f4affc00183 | 12 | // Modified by JH1PJL/K.Arai April 26th, 2018 for DISCO-F469NI |
kenjiArai | 4:0f4affc00183 | 13 | // Modified by JH1PJL/K.Arai October 14th, 2019 for DISCO-F769NI |
kenjiArai | 4:0f4affc00183 | 14 | |
kenjiArai | 4:0f4affc00183 | 15 | #include "select_program.h" |
kenjiArai | 4:0f4affc00183 | 16 | //#define EXAMPLE_7_MANDEL |
kenjiArai | 4:0f4affc00183 | 17 | #ifdef EXAMPLE_7_MANDEL |
kenjiArai | 4:0f4affc00183 | 18 | |
kenjiArai | 4:0f4affc00183 | 19 | #include "button_group.hpp" |
kenjiArai | 4:0f4affc00183 | 20 | #include "mandelbrot.hpp" |
kenjiArai | 4:0f4affc00183 | 21 | |
kenjiArai | 4:0f4affc00183 | 22 | using namespace Mikami; |
kenjiArai | 4:0f4affc00183 | 23 | |
kenjiArai | 4:0f4affc00183 | 24 | struct PointF |
kenjiArai | 4:0f4affc00183 | 25 | { |
kenjiArai | 4:0f4affc00183 | 26 | PointF() {} |
kenjiArai | 4:0f4affc00183 | 27 | PointF(float x0, float y0) : x(x0), y(y0) {} |
kenjiArai | 4:0f4affc00183 | 28 | float x, y; |
kenjiArai | 4:0f4affc00183 | 29 | }; |
kenjiArai | 4:0f4affc00183 | 30 | |
kenjiArai | 4:0f4affc00183 | 31 | TS_DISCO_F769NI ts_; |
kenjiArai | 4:0f4affc00183 | 32 | LCD_DISCO_F769NI lcd_; |
kenjiArai | 4:0f4affc00183 | 33 | Serial pc(USBTX, USBRX, 115200); |
kenjiArai | 4:0f4affc00183 | 34 | |
kenjiArai | 4:0f4affc00183 | 35 | const int X0_ = 0; // origin of x axis |
kenjiArai | 4:0f4affc00183 | 36 | const int Y0_ = 0; // origin of y axis |
kenjiArai | 4:0f4affc00183 | 37 | |
kenjiArai | 4:0f4affc00183 | 38 | const int NX_ = 670; // number of pixels for horizon |
kenjiArai | 4:0f4affc00183 | 39 | const int NY_ = 542; // number of pixels for vertical |
kenjiArai | 4:0f4affc00183 | 40 | |
kenjiArai | 4:0f4affc00183 | 41 | void Redraw(MandelbrotBase *mandel, |
kenjiArai | 4:0f4affc00183 | 42 | float wH, float hH, PointF xy, int level, |
kenjiArai | 4:0f4affc00183 | 43 | float &x1, float &x2, float &y1, float &y2) |
kenjiArai | 4:0f4affc00183 | 44 | { |
kenjiArai | 4:0f4affc00183 | 45 | x1 = xy.x - wH; |
kenjiArai | 4:0f4affc00183 | 46 | x2 = xy.x + wH; |
kenjiArai | 4:0f4affc00183 | 47 | y1 = xy.y - hH; |
kenjiArai | 4:0f4affc00183 | 48 | y2 = xy.y + hH; |
kenjiArai | 4:0f4affc00183 | 49 | |
kenjiArai | 4:0f4affc00183 | 50 | char str[16]; |
kenjiArai | 4:0f4affc00183 | 51 | lcd_.SetTextColor(LCD_COLOR_WHITE); |
kenjiArai | 4:0f4affc00183 | 52 | lcd_.DisplayStringAt(520, 424, (uint8_t *)str, LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 53 | sprintf(str, "x:%7.3f", xy.x); |
kenjiArai | 4:0f4affc00183 | 54 | lcd_.DisplayStringAt(520, 440, (uint8_t *)str, LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 55 | sprintf(str, "y:%7.3f", -xy.y); |
kenjiArai | 4:0f4affc00183 | 56 | lcd_.DisplayStringAt(520, 456, (uint8_t *)str, LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 57 | mandel->Display(x1, x2, y1, y2); |
kenjiArai | 4:0f4affc00183 | 58 | } |
kenjiArai | 4:0f4affc00183 | 59 | |
kenjiArai | 4:0f4affc00183 | 60 | int main() |
kenjiArai | 4:0f4affc00183 | 61 | { |
kenjiArai | 4:0f4affc00183 | 62 | const int LEVEL = 18; |
kenjiArai | 4:0f4affc00183 | 63 | PointF xy[LEVEL]; // corresponding to touched position |
kenjiArai | 4:0f4affc00183 | 64 | |
kenjiArai | 4:0f4affc00183 | 65 | uint32_t backColor = 0xFF006A6C; // teal green |
kenjiArai | 4:0f4affc00183 | 66 | uint32_t inActive = backColor & 0xE0FFFFFF; // color for inactive button |
kenjiArai | 4:0f4affc00183 | 67 | |
kenjiArai | 4:0f4affc00183 | 68 | // region to be displayed |
kenjiArai | 4:0f4affc00183 | 69 | float x1 = -2.3f; |
kenjiArai | 4:0f4affc00183 | 70 | float x2 = 0.9f; |
kenjiArai | 4:0f4affc00183 | 71 | float y1 = -1.3f; |
kenjiArai | 4:0f4affc00183 | 72 | float y2 = 1.3f; |
kenjiArai | 4:0f4affc00183 | 73 | |
kenjiArai | 4:0f4affc00183 | 74 | float wH = (x2 - x1)/2.0f; // half width of region |
kenjiArai | 4:0f4affc00183 | 75 | float hH = (y2 - y1)/2.0f; // half height of region |
kenjiArai | 4:0f4affc00183 | 76 | |
kenjiArai | 4:0f4affc00183 | 77 | xy[0] = PointF(x1 + wH, y1 + hH); |
kenjiArai | 4:0f4affc00183 | 78 | |
kenjiArai | 4:0f4affc00183 | 79 | pc.printf("\x1b[2J\x1b[H %s\r\n %s %s (UTC)\r\n", |
kenjiArai | 4:0f4affc00183 | 80 | __FILE__, __DATE__, __TIME__); |
kenjiArai | 4:0f4affc00183 | 81 | printf(" Mandelbrot picture for DISCO-F769NI:\r\n"); |
kenjiArai | 4:0f4affc00183 | 82 | |
kenjiArai | 4:0f4affc00183 | 83 | lcd_.Clear(backColor); |
kenjiArai | 4:0f4affc00183 | 84 | |
kenjiArai | 4:0f4affc00183 | 85 | MandelbrotBase *mandel[3]; |
kenjiArai | 4:0f4affc00183 | 86 | mandel[0] = new MandelbrotBW(lcd_, X0_, Y0_, NX_, NY_- 60); |
kenjiArai | 4:0f4affc00183 | 87 | mandel[1] = new MandelbrotColor1(lcd_, X0_, Y0_, NX_, NY_- 60); |
kenjiArai | 4:0f4affc00183 | 88 | mandel[2] = new MandelbrotColor2(lcd_, X0_, Y0_, NX_, NY_- 60); |
kenjiArai | 4:0f4affc00183 | 89 | |
kenjiArai | 4:0f4affc00183 | 90 | const string strButton[3] = {"B & W", "Color1", "Color2"}; |
kenjiArai | 4:0f4affc00183 | 91 | ButtonGroup bGroup(lcd_, ts_, 680, Y0_, 120, 80, |
kenjiArai | 4:0f4affc00183 | 92 | LCD_COLOR_BLUE, backColor, |
kenjiArai | 4:0f4affc00183 | 93 | 3, strButton, 0, 5, 1, Font24); |
kenjiArai | 4:0f4affc00183 | 94 | Button back(lcd_, ts_, 680, 390+Y0_, 120, 80, |
kenjiArai | 4:0f4affc00183 | 95 | LCD_COLOR_BLUE, backColor, "Back", Font24); |
kenjiArai | 4:0f4affc00183 | 96 | back.Draw(inActive, LCD_COLOR_GRAY); |
kenjiArai | 4:0f4affc00183 | 97 | |
kenjiArai | 4:0f4affc00183 | 98 | |
kenjiArai | 4:0f4affc00183 | 99 | // area of Mandelbrot set |
kenjiArai | 4:0f4affc00183 | 100 | Button area(lcd_, ts_, X0_, Y0_, NX_, NY_, backColor, backColor); |
kenjiArai | 4:0f4affc00183 | 101 | |
kenjiArai | 4:0f4affc00183 | 102 | lcd_.SetTextColor(LCD_COLOR_WHITE); |
kenjiArai | 4:0f4affc00183 | 103 | lcd_.SetFont(&Font24); |
kenjiArai | 4:0f4affc00183 | 104 | lcd_.DisplayStringAt(280, 460, (uint8_t *)"Mandelbrot set", LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 105 | |
kenjiArai | 4:0f4affc00183 | 106 | int pattern = -1; |
kenjiArai | 4:0f4affc00183 | 107 | int level = 0; |
kenjiArai | 4:0f4affc00183 | 108 | bool first = true; |
kenjiArai | 4:0f4affc00183 | 109 | while (true){ |
kenjiArai | 4:0f4affc00183 | 110 | int num = -1; |
kenjiArai | 4:0f4affc00183 | 111 | // select color pattern and display |
kenjiArai | 4:0f4affc00183 | 112 | if (bGroup.GetTouchedNumber(num) && (pattern != num)){ |
kenjiArai | 4:0f4affc00183 | 113 | bGroup.Draw(num, LCD_COLOR_DARKBLUE); |
kenjiArai | 4:0f4affc00183 | 114 | mandel[num]->Display(x1, x2, y1, y2); |
kenjiArai | 4:0f4affc00183 | 115 | pattern = num; |
kenjiArai | 4:0f4affc00183 | 116 | bGroup.Redraw(num); |
kenjiArai | 4:0f4affc00183 | 117 | |
kenjiArai | 4:0f4affc00183 | 118 | if (first){ |
kenjiArai | 4:0f4affc00183 | 119 | lcd_.SetBackColor(backColor); |
kenjiArai | 4:0f4affc00183 | 120 | lcd_.SetFont(&Font20); |
kenjiArai | 4:0f4affc00183 | 121 | lcd_.DisplayStringAt(680, LINE(16), (uint8_t *)"Tap to", |
kenjiArai | 4:0f4affc00183 | 122 | LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 123 | lcd_.DisplayStringAt(680, LINE(17), (uint8_t *)"Zoom in.", |
kenjiArai | 4:0f4affc00183 | 124 | LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 125 | first = false; |
kenjiArai | 4:0f4affc00183 | 126 | } |
kenjiArai | 4:0f4affc00183 | 127 | } |
kenjiArai | 4:0f4affc00183 | 128 | |
kenjiArai | 4:0f4affc00183 | 129 | // expand twice around tapped point |
kenjiArai | 4:0f4affc00183 | 130 | if (area.Touched() && (pattern != -1)){ |
kenjiArai | 4:0f4affc00183 | 131 | TS_StateTypeDef state = Button::GottenState(); |
kenjiArai | 4:0f4affc00183 | 132 | |
kenjiArai | 4:0f4affc00183 | 133 | uint16_t x = state.touchX[0] - X0_; |
kenjiArai | 4:0f4affc00183 | 134 | uint16_t y = state.touchY[0] - Y0_; |
kenjiArai | 4:0f4affc00183 | 135 | |
kenjiArai | 4:0f4affc00183 | 136 | lcd_.SetBackColor(backColor); |
kenjiArai | 4:0f4affc00183 | 137 | if (++level < LEVEL){ |
kenjiArai | 4:0f4affc00183 | 138 | xy[level] = PointF(mandel[pattern]->Fx(x), |
kenjiArai | 4:0f4affc00183 | 139 | mandel[pattern]->Fy(y)); |
kenjiArai | 4:0f4affc00183 | 140 | wH = wH/2.0f; |
kenjiArai | 4:0f4affc00183 | 141 | hH = hH/2.0f; |
kenjiArai | 4:0f4affc00183 | 142 | |
kenjiArai | 4:0f4affc00183 | 143 | Redraw(mandel[pattern], wH, hH, |
kenjiArai | 4:0f4affc00183 | 144 | xy[level], level, x1, x2, y1, y2); |
kenjiArai | 4:0f4affc00183 | 145 | } else { |
kenjiArai | 4:0f4affc00183 | 146 | lcd_.SetFont(&Font24); |
kenjiArai | 4:0f4affc00183 | 147 | lcd_.DisplayStringAt(X0_+76, 260, |
kenjiArai | 4:0f4affc00183 | 148 | (uint8_t *)" No more expand! ", |
kenjiArai | 4:0f4affc00183 | 149 | LEFT_MODE); |
kenjiArai | 4:0f4affc00183 | 150 | level = LEVEL - 1 ; |
kenjiArai | 4:0f4affc00183 | 151 | } |
kenjiArai | 4:0f4affc00183 | 152 | back.Redraw(); |
kenjiArai | 4:0f4affc00183 | 153 | } |
kenjiArai | 4:0f4affc00183 | 154 | |
kenjiArai | 4:0f4affc00183 | 155 | // "Back" button tapped |
kenjiArai | 4:0f4affc00183 | 156 | if (back.Touched() && level > 0){ |
kenjiArai | 4:0f4affc00183 | 157 | back.Draw(LCD_COLOR_DARKBLUE); |
kenjiArai | 4:0f4affc00183 | 158 | lcd_.SetBackColor(backColor); |
kenjiArai | 4:0f4affc00183 | 159 | if (level > 0){ |
kenjiArai | 4:0f4affc00183 | 160 | level--; |
kenjiArai | 4:0f4affc00183 | 161 | wH = wH*2.0f; |
kenjiArai | 4:0f4affc00183 | 162 | hH = hH*2.0f; |
kenjiArai | 4:0f4affc00183 | 163 | |
kenjiArai | 4:0f4affc00183 | 164 | Redraw(mandel[pattern], wH, hH, xy[level], |
kenjiArai | 4:0f4affc00183 | 165 | level, x1, x2, y1, y2); |
kenjiArai | 4:0f4affc00183 | 166 | if (level > 0){ |
kenjiArai | 4:0f4affc00183 | 167 | back.Redraw(); |
kenjiArai | 4:0f4affc00183 | 168 | } else { |
kenjiArai | 4:0f4affc00183 | 169 | back.Draw(inActive, LCD_COLOR_GRAY); |
kenjiArai | 4:0f4affc00183 | 170 | } |
kenjiArai | 4:0f4affc00183 | 171 | } |
kenjiArai | 4:0f4affc00183 | 172 | } |
kenjiArai | 4:0f4affc00183 | 173 | ThisThread::sleep_for(100); |
kenjiArai | 4:0f4affc00183 | 174 | } |
kenjiArai | 4:0f4affc00183 | 175 | } |
kenjiArai | 4:0f4affc00183 | 176 | #endif |