Показаны различия между двумя версиями страницы.
| Предыдущая версия справа и слеваПредыдущая версияСледующая версия | Предыдущая версия | ||
| products:laboratory_iot_c:exp53 [2024/12/13 20:04] – [Эксперимент 53. Классы ящиков, человека и цели] labuser30 | products:laboratory_iot_c:exp53 [2024/12/15 17:03] (текущий) – [Программный код эксперимента] labuser30 | ||
|---|---|---|---|
| Строка 160: | Строка 160: | ||
| ==== Программный код эксперимента ==== | ==== Программный код эксперимента ==== | ||
| - | < | + | < |
| - | from machine import Pin, SPI | + | #include <SPI.h> |
| - | from tft import TFT_GREEN | + | #include < |
| - | _init() | + | #include " |
| - | machine.freq(160000000) | + | |
| + | #define PIN_CS | ||
| + | #define PIN_DC | ||
| + | #define PIN_RST 5 | ||
| - | dc | + | Adafruit_ST7735 tft = Adafruit_ST7735(PIN_CS, |
| - | cs = Pin(2, Pin.OUT) | + | LittleFS_ImageReader reader; |
| - | rst = Pin(5, Pin.OUT) | + | |
| - | spi = SPI(1, baudrate=40000000, | + | |
| - | # TFT object, this is ST7735R green tab version | + | bool Map[10][8] = { |
| - | tft = TFT_GREEN(128, 160, spi, dc, cs, rst, rotate=0) | + | {1, |
| + | {0,1,1,1,1,1,1,0}, | ||
| + | {1, | ||
| + | {0, | ||
| + | {0, | ||
| + | {1, | ||
| + | {1, | ||
| + | {1, | ||
| + | {1, | ||
| + | {1, | ||
| + | }; | ||
| - | Map = [ | + | struct Pos { |
| - | [1,1,0, | + | int x = 0; |
| - | [0, | + | int y = 0; |
| - | [1, | + | |
| - | [0, | + | |
| - | [0, | + | |
| - | [1, | + | |
| - | [1, | + | |
| - | [1, | + | |
| - | [1, | + | |
| - | [1, | + | |
| - | ] | + | |
| - | Gates = [] | + | bool operator |
| - | Boxes = [] | + | |
| + | } | ||
| + | }; | ||
| - | class Box: | + | class Box { |
| - | | + | private: |
| - | | + | |
| - | | + | |
| - | | + | Pos pos; |
| - | self.picture = 'box.bmp' | + | |
| - | | + | |
| - | | + | bool on_gate |
| - | self.draw() | + | |
| - | def draw(self): | + | public: |
| - | | + | Box(Adafruit_ST7735 *_tft_ptr, LittleFS_ImageReader *_reader_ptr, |
| - | self.tft.draw_bmp(self.x * 16,self.y * 16, self.picture_onGate) | + | tft_ptr = _tft_ptr; |
| - | else: | + | reader_ptr = _reader_ptr; |
| - | self.tft.draw_bmp(self.x * 16,self.y * 16, self.picture) | + | pos.x = x; |
| + | pos.y = y; | ||
| + | } | ||
| + | |||
| + | void draw() { | ||
| + | if (on_gate) reader_ptr-> | ||
| + | else reader_ptr-> | ||
| + | } | ||
| + | |||
| + | void setOnGate(bool state) { | ||
| + | on_gate = state; | ||
| + | } | ||
| + | |||
| + | bool getOnGate() const { | ||
| + | return on_gate; | ||
| + | } | ||
| + | |||
| + | Pos getPos() const { | ||
| + | return pos; | ||
| + | } | ||
| + | |||
| + | void setPos(Pos _pos) { | ||
| + | pos.x = _pos.x; | ||
| + | pos.y = _pos.y; | ||
| + | draw(); | ||
| + | } | ||
| + | }; | ||
| - | def setOnGate(self, | + | class Gate { |
| - | | + | private: |
| + | | ||
| + | LittleFS_ImageReader *reader_ptr; | ||
| + | Pos pos; | ||
| + | String picture | ||
| - | def getOnGate(self): | + | public: |
| - | | + | Gate(Adafruit_ST7735 *_tft_ptr, LittleFS_ImageReader *_reader_ptr, |
| + | | ||
| + | reader_ptr = _reader_ptr; | ||
| + | pos.x = x; | ||
| + | pos.y = y; | ||
| + | } | ||
| - | | + | |
| - | | + | |
| + | } | ||
| - | | + | |
| - | | + | |
| - | | + | } |
| - | | + | }; |
| - | class Gate: | + | class Man { |
| - | | + | private: |
| - | | + | |
| - | | + | |
| - | | + | Pos pos; |
| - | self.picture = 'gate.bmp' | + | |
| - | self.draw() | + | |
| - | def draw(self): | + | public: |
| - | | + | Man(Adafruit_ST7735 |
| + | Serial.println(" | ||
| + | tft_ptr = _tft_ptr; | ||
| + | reader_ptr = _reader_ptr; | ||
| + | pos.x = x; | ||
| + | pos.y = y; | ||
| + | } | ||
| - | | + | |
| - | | + | |
| + | } | ||
| - | class Man: | + | Pos getPos() const { |
| - | def __init__(self, tft, x, y): | + | |
| - | | + | } |
| - | | + | |
| - | self.y = y | + | |
| - | self.picture = ' | + | |
| - | self.draw() | + | |
| - | | + | |
| - | | + | |
| + | pos.x = _pos.x; | ||
| + | pos.y = _pos.y; | ||
| + | draw(); | ||
| + | } | ||
| + | }; | ||
| - | def getPos(self): | + | const int boxes_number = 3; |
| - | | + | Box boxes[boxes_number] = { |
| + | {&tft, & | ||
| + | {&tft, & | ||
| + | {&tft, & | ||
| + | }; | ||
| - | def setPos(self, x, y): | + | const int gates_number = 3; |
| - | self.tft.rect(self.x * 16, self.y * 16, 16, 16, tft.COLOR_BLACK) | + | Gate gates[gates_number] = { |
| - | | + | {&tft, & |
| - | self.y = y | + | |
| - | | + | |
| + | }; | ||
| - | # init TFT | + | Man man(&tft, &reader, 5, 6); |
| - | tft.initr(tft.BGR) # tft.initr(tft.RGB) #Если вместо синего цвета отображается красный, а вместо красного синий | + | |
| - | tft.clear(tft.COLOR_BLACK) #b, g, r | + | |
| - | x = 0 | + | void setup() { |
| - | y = 0 | + | |
| + | Serial.println(); | ||
| + | Serial.println(" | ||
| + | LittleFS.begin(); | ||
| + | tft.initR(INITR_BLACKTAB); | ||
| + | tft.setRotation(2); | ||
| + | tft.fillScreen(ST77XX_BLACK); | ||
| - | for row in Map: | + | |
| - | for col in row: | + | for (int x = 0; x < 10; x++) { |
| - | if col: | + | if (Map[y][x]) reader.drawBMP("/ |
| - | tft.draw_bmp(x * 16, y * 16,' | + | |
| - | x+=1 | + | } |
| - | | + | |
| - | | + | for (int i = 0; i < boxes_number; |
| + | | ||
| + | } | ||
| - | Boxes.append(Box(tft, 3,4)) | + | for (int i = 0; i < gates_number; |
| - | Boxes.append(Box(tft, 4,6)) | + | |
| - | Boxes.append(Box(tft, 2,7)) | + | } |
| + | |||
| + | man.draw(); | ||
| + | } | ||
| - | Gates.append(Gate(tft, 6,3)) | + | void loop() { |
| - | Gates.append(Gate(tft, | + | } |
| - | Gates.append(Gate(tft, | + | |
| - | + | ||
| - | man = Man(tft, 5, 6) | + | |
| </ | </ | ||
| - | Для хранения объектов | + | Создаем массив объектов |
| - | < | + | < |
| - | Gates = [] | + | const int boxes_number |
| - | Boxes = [] | + | Box boxes[boxes_number] = { |
| + | {&tft, & | ||
| + | {&tft, & | ||
| + | {&tft, & | ||
| + | }; | ||
| </ | </ | ||
| - | Создаем | + | Создаем |
| - | < | + | < |
| - | Boxes.append(Box(tft, 3,4)) | + | const int gates_number = 3; |
| - | Boxes.append(Box(tft, 4,6)) | + | Gate gates[gates_number] = { |
| - | Boxes.append(Box(tft, 2,7)) | + | {&tft, & |
| + | | ||
| + | | ||
| + | }; | ||
| </ | </ | ||
| - | Оператор '' | + | Создаем кладовщика: |
| - | + | < | |
| - | Создаем | + | Man man(&tft, &reader, 5, 6); |
| - | < | + | |
| - | Gates.append(Gate(tft, 6,3)) | + | |
| - | Gates.append(Gate(tft, 6,4)) | + | |
| - | Gates.append(Gate(tft, | + | |
| </ | </ | ||
| - | Создаем кладовщика: | + | Вызываем |
| - | < | + | < |
| - | man = Man(tft, 5, 6) | + | for (int i = 0; i < boxes_number; |
| + | boxes[i].draw(); | ||
| + | } | ||
| + | |||
| + | for (int i = 0; i < gates_number; | ||
| + | gates[i].draw(); | ||
| + | } | ||
| + | |||
| + | man.draw(); | ||
| </ | </ | ||
| Теперь мы имеем игровое поле с лабиринтом, | Теперь мы имеем игровое поле с лабиринтом, | ||