|
| 1 | +#define USE_SEMAPHORE_DMA1 |
| 2 | +#include <MapleFreeRTOS821.h> |
| 3 | +#include <SPI.h> |
| 4 | +#include <Adafruit_GFX.h> |
| 5 | +#include <TFT_ILI9163C.h> |
| 6 | + |
| 7 | +#define __CS 8 |
| 8 | +#define __RST 9 |
| 9 | +#define __DC 10 |
| 10 | + |
| 11 | +TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST); |
| 12 | + |
| 13 | +xSemaphoreHandle xDisplayFree; |
| 14 | + |
| 15 | + |
| 16 | +const float sin_d[] = { |
| 17 | + 0, 0.17, 0.34, 0.5, 0.64, 0.77, 0.87, 0.94, 0.98, 1, 0.98, 0.94, |
| 18 | + 0.87, 0.77, 0.64, 0.5, 0.34, 0.17, 0, -0.17, -0.34, -0.5, -0.64, |
| 19 | + -0.77, -0.87, -0.94, -0.98, -1, -0.98, -0.94, -0.87, -0.77, |
| 20 | + -0.64, -0.5, -0.34, -0.17 |
| 21 | +}; |
| 22 | +const float cos_d[] = { |
| 23 | + 1, 0.98, 0.94, 0.87, 0.77, 0.64, 0.5, 0.34, 0.17, 0, -0.17, -0.34, |
| 24 | + -0.5, -0.64, -0.77, -0.87, -0.94, -0.98, -1, -0.98, -0.94, -0.87, |
| 25 | + -0.77, -0.64, -0.5, -0.34, -0.17, 0, 0.17, 0.34, 0.5, 0.64, 0.77, |
| 26 | + 0.87, 0.94, 0.98 |
| 27 | +}; |
| 28 | +const float d = 5; |
| 29 | +float cube1_px[] = { |
| 30 | + -d, d, d, -d, -d, d, d, -d |
| 31 | +}; |
| 32 | +float cube1_py[] = { |
| 33 | + -d, -d, d, d, -d, -d, d, d |
| 34 | +}; |
| 35 | +float cube1_pz[] = { |
| 36 | + -d, -d, -d, -d, d, d, d, d |
| 37 | +}; |
| 38 | + |
| 39 | +float cube1_p2x[] = { |
| 40 | + 0, 0, 0, 0, 0, 0, 0, 0 |
| 41 | +}; |
| 42 | +float cube1_p2y[] = { |
| 43 | + 0, 0, 0, 0, 0, 0, 0, 0 |
| 44 | +}; |
| 45 | + |
| 46 | +int cube1_r[] = { |
| 47 | + 0, 0, 0 |
| 48 | +}; |
| 49 | +const float d2 = 10; |
| 50 | +float cube2_px[] = { |
| 51 | + -d2, d2, d2, -d2, -d2, d2, d2, -d2 |
| 52 | +}; |
| 53 | +float cube2_py[] = { |
| 54 | + -d2, -d2, d2, d2, -d2, -d2, d2, d2 |
| 55 | +}; |
| 56 | +float cube2_pz[] = { |
| 57 | + -d2, -d2, -d2, -d2, d2, d2, d2, d2 |
| 58 | +}; |
| 59 | + |
| 60 | +float cube2_p2x[] = { |
| 61 | + 0, 0, 0, 0, 0, 0, 0, 0 |
| 62 | +}; |
| 63 | +float cube2_p2y[] = { |
| 64 | + 0, 0, 0, 0, 0, 0, 0, 0 |
| 65 | +}; |
| 66 | + |
| 67 | +int cube2_r[] = { |
| 68 | + 0, 0, 0 |
| 69 | +}; |
| 70 | + |
| 71 | +uint16 cube1_x, cube1_y, cube2_x, cube2_y, cube1_color, cube2_color; |
| 72 | + |
| 73 | +static void vLEDFlashTask(void *pvParameters) { |
| 74 | + for (;;) { |
| 75 | + vTaskDelay(1000); |
| 76 | + digitalWrite(BOARD_LED_PIN, HIGH); |
| 77 | + vTaskDelay(50); |
| 78 | + digitalWrite(BOARD_LED_PIN, LOW); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +static void vCube1LoopTask(void *pvParameters) { |
| 83 | + while (1) { |
| 84 | + if ( xSemaphoreTake( xDisplayFree, ( portTickType ) 10 ) == pdTRUE ) |
| 85 | + { |
| 86 | + cube(cube1_px, cube1_py, cube1_pz, cube1_p2x, cube1_p2y, cube1_r, &cube1_x, &cube1_y, &cube1_color); |
| 87 | + xSemaphoreGive( xDisplayFree ); |
| 88 | + vTaskDelay(15); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +static void vCube2LoopTask(void *pvParameters) { |
| 94 | + while (1) { |
| 95 | + if ( xSemaphoreTake( xDisplayFree, ( portTickType ) 10 ) == pdTRUE ) |
| 96 | + { |
| 97 | + cube(cube2_px, cube2_py, cube2_pz, cube2_p2x, cube2_p2y, cube2_r, &cube2_x, &cube2_y, &cube2_color); |
| 98 | + xSemaphoreGive( xDisplayFree ); |
| 99 | + vTaskDelay(40); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +void cube(float *px, float *py, float *pz, float *p2x, float *p2y, int *r, uint16 *x, uint16 *y, uint16 *color) { |
| 106 | + |
| 107 | + for (int i = 0; i < 3; i++) { |
| 108 | + tft.drawLine(p2x[i], p2y[i], p2x[i + 1], p2y[i + 1], WHITE); |
| 109 | + tft.drawLine(p2x[i + 4], p2y[i + 4], p2x[i + 5], p2y[i + 5], WHITE); |
| 110 | + tft.drawLine(p2x[i], p2y[i], p2x[i + 4], p2y[i + 4], WHITE); |
| 111 | + } |
| 112 | + tft.drawLine(p2x[3], p2y[3], p2x[0], p2y[0], WHITE); |
| 113 | + tft.drawLine(p2x[7], p2y[7], p2x[4], p2y[4], WHITE); |
| 114 | + tft.drawLine(p2x[3], p2y[3], p2x[7], p2y[7], WHITE); |
| 115 | + |
| 116 | + r[0] = r[0] + 1; |
| 117 | + r[1] = r[1] + 1; |
| 118 | + if (r[0] == 36) r[0] = 0; |
| 119 | + if (r[1] == 36) r[1] = 0; |
| 120 | + if (r[2] == 36) r[2] = 0; |
| 121 | + for (int i = 0; i < 8; i++) |
| 122 | + { |
| 123 | + float px2 = px[i]; |
| 124 | + float py2 = cos_d[r[0]] * py[i] - sin_d[r[0]] * pz[i]; |
| 125 | + float pz2 = sin_d[r[0]] * py[i] + cos_d[r[0]] * pz[i]; |
| 126 | + |
| 127 | + float px3 = cos_d[r[1]] * px2 + sin_d[r[1]] * pz2; |
| 128 | + float py3 = py2; |
| 129 | + float pz3 = -sin_d[r[1]] * px2 + cos_d[r[1]] * pz2; |
| 130 | + |
| 131 | + float ax = cos_d[r[2]] * px3 - sin_d[r[2]] * py3; |
| 132 | + float ay = sin_d[r[2]] * px3 + cos_d[r[2]] * py3; |
| 133 | + float az = pz3 - 190; |
| 134 | + |
| 135 | + p2x[i] = *x + ax * 500 / az; |
| 136 | + p2y[i] = *y + ay * 500 / az; |
| 137 | + } |
| 138 | + |
| 139 | + for (int i = 0; i < 3; i++) { |
| 140 | + tft.drawLine(p2x[i], p2y[i], p2x[i + 1], p2y[i + 1], *color); |
| 141 | + tft.drawLine(p2x[i + 4], p2y[i + 4], p2x[i + 5], p2y[i + 5], *color); |
| 142 | + tft.drawLine(p2x[i], p2y[i], p2x[i + 4], p2y[i + 4], *color); |
| 143 | + } |
| 144 | + tft.drawLine(p2x[3], p2y[3], p2x[0], p2y[0], *color); |
| 145 | + tft.drawLine(p2x[7], p2y[7], p2x[4], p2y[4], *color); |
| 146 | + tft.drawLine(p2x[3], p2y[3], p2x[7], p2y[7], *color); |
| 147 | +} |
| 148 | + |
| 149 | +static void vSqrtTask(void *pvParameters) { |
| 150 | + while (1){ |
| 151 | + Serial.println ("Starting Sqrt calculations..."); |
| 152 | + uint16 x = 0; |
| 153 | + uint16 ixx[1001]; |
| 154 | + // Library Sqrt |
| 155 | + uint32_t t0 = millis(); |
| 156 | + for (uint32_t n = 247583650 ; n > 247400000 ; n--) { |
| 157 | + x = sqrt (n); |
| 158 | + } |
| 159 | + uint32_t t1 = millis() - t0; |
| 160 | + Serial.print ("Sqrt calculations took (ms): "); |
| 161 | + Serial.println (t1); |
| 162 | + delay (5000); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +void setup() { |
| 167 | + // initialize the digital pin as an output: |
| 168 | + Serial.begin(9600); |
| 169 | + delay (5000); |
| 170 | + Serial.println ("Running..."); |
| 171 | + pinMode(BOARD_LED_PIN, OUTPUT); |
| 172 | + tft.begin(); |
| 173 | + tft.fillScreen(WHITE); |
| 174 | + cube1_x = ((tft.width()) / 4); |
| 175 | + cube1_y = ((tft.height()) / 4); |
| 176 | + cube2_x = ((tft.width()) / 2); |
| 177 | + cube2_y = ((tft.height()) / 2); |
| 178 | + cube1_color = BLACK; |
| 179 | + cube2_color = RED; |
| 180 | + vSemaphoreCreateBinary(xDisplayFree); |
| 181 | + xTaskCreate(vLEDFlashTask, |
| 182 | + "Task1", |
| 183 | + configMINIMAL_STACK_SIZE, |
| 184 | + NULL, |
| 185 | + tskIDLE_PRIORITY + 2, |
| 186 | + NULL); |
| 187 | + xTaskCreate(vCube1LoopTask, |
| 188 | + "Cube1", |
| 189 | + configMINIMAL_STACK_SIZE, |
| 190 | + NULL, |
| 191 | + tskIDLE_PRIORITY + 2, |
| 192 | + NULL); |
| 193 | + xTaskCreate(vCube2LoopTask, |
| 194 | + "Cube2", |
| 195 | + configMINIMAL_STACK_SIZE, |
| 196 | + NULL, |
| 197 | + tskIDLE_PRIORITY+1, |
| 198 | + NULL); |
| 199 | + xTaskCreate(vSqrtTask, |
| 200 | + "Sqrt", |
| 201 | + configMINIMAL_STACK_SIZE, |
| 202 | + NULL, |
| 203 | + tskIDLE_PRIORITY, |
| 204 | + NULL); |
| 205 | + vTaskStartScheduler(); |
| 206 | +} |
| 207 | + |
| 208 | +void loop() { |
| 209 | + // Do not write any code here, it would not execute. |
| 210 | +} |
| 211 | + |
| 212 | + |
0 commit comments