Skip to content

Commit

Permalink
Multi-Volume. Select Media for LVGL (MarlinFirmware#21344)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhapsodyv authored Apr 13, 2021
1 parent a5f0075 commit 138340e
Show file tree
Hide file tree
Showing 29 changed files with 540 additions and 273 deletions.
9 changes: 9 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,15 @@
// Enable if SD detect is rendered useless (e.g., by using an SD extender)
//#define NO_SD_DETECT

// Multiple volume support - EXPERIMENTAL.
//#define MULTI_VOLUME
#if ENABLED(MULTI_VOLUME)
#define VOLUME_SD_ONBOARD
#define VOLUME_USB_FLASH_DRIVE
#define DEFAULT_VOLUME SD_ONBOARD
#define DEFAULT_SHARED_VOLUME USB_FLASH_DRIVE
#endif

#endif // SDSUPPORT

/**
Expand Down
18 changes: 9 additions & 9 deletions Marlin/src/HAL/DUE/usb/sd_mmc_spi_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Ctrl_status sd_mmc_spi_test_unit_ready() {
Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector) {
if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.isMounted())
return CTRL_NO_PRESENT;
*nb_sector = card.getSd2Card().cardSize() - 1;
*nb_sector = card.diskIODriver()->cardSize() - 1;
return CTRL_GOOD;
}

Expand Down Expand Up @@ -74,24 +74,24 @@ Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
#endif

// Start reading
if (!card.getSd2Card().readStart(addr))
if (!card.diskIODriver()->readStart(addr))
return CTRL_FAIL;

// For each specified sector
while (nb_sector--) {

// Read a sector
card.getSd2Card().readData(sector_buf);
card.diskIODriver()->readData(sector_buf);

// RAM -> USB
if (!udi_msc_trans_block(true, sector_buf, SD_MMC_BLOCK_SIZE, nullptr)) {
card.getSd2Card().readStop();
card.diskIODriver()->readStop();
return CTRL_FAIL;
}
}

// Stop reading
card.getSd2Card().readStop();
card.diskIODriver()->readStop();

// Done
return CTRL_GOOD;
Expand All @@ -113,24 +113,24 @@ Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) {
}
#endif

if (!card.getSd2Card().writeStart(addr, nb_sector))
if (!card.diskIODriver()->writeStart(addr, nb_sector))
return CTRL_FAIL;

// For each specified sector
while (nb_sector--) {

// USB -> RAM
if (!udi_msc_trans_block(false, sector_buf, SD_MMC_BLOCK_SIZE, nullptr)) {
card.getSd2Card().writeStop();
card.diskIODriver()->writeStop();
return CTRL_FAIL;
}

// Write a sector
card.getSd2Card().writeData(sector_buf);
card.diskIODriver()->writeData(sector_buf);
}

// Stop writing
card.getSd2Card().writeStop();
card.diskIODriver()->writeStop();

// Done
return CTRL_GOOD;
Expand Down
38 changes: 25 additions & 13 deletions Marlin/src/HAL/STM32/msc_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,66 @@

class Sd2CardUSBMscHandler : public USBMscHandler {
public:
DiskIODriver* diskIODriver() {
#if ENABLED(MULTI_VOLUME)
#if SHARED_VOLUME_IS(SD_ONBOARD)
return &card.media_sd_spi;
#elif SHARED_VOLUME_IS(USB_FLASH_DRIVE)
return &card.media_usbFlashDrive;
#endif
#else
return diskIODriver();
#endif
}

bool GetCapacity(uint32_t *pBlockNum, uint16_t *pBlockSize) {
*pBlockNum = card.getSd2Card().cardSize();
*pBlockNum = diskIODriver()->cardSize();
*pBlockSize = BLOCK_SIZE;
return true;
}

bool Write(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
auto sd2card = card.getSd2Card();
auto sd2card = diskIODriver();
// single block
if (blkLen == 1) {
watchdog_refresh();
sd2card.writeBlock(blkAddr, pBuf);
sd2card->writeBlock(blkAddr, pBuf);
return true;
}

// multi block optmization
sd2card.writeStart(blkAddr, blkLen);
sd2card->writeStart(blkAddr, blkLen);
while (blkLen--) {
watchdog_refresh();
sd2card.writeData(pBuf);
sd2card->writeData(pBuf);
pBuf += BLOCK_SIZE;
}
sd2card.writeStop();
sd2card->writeStop();
return true;
}

bool Read(uint8_t *pBuf, uint32_t blkAddr, uint16_t blkLen) {
auto sd2card = card.getSd2Card();
auto sd2card = diskIODriver();
// single block
if (blkLen == 1) {
watchdog_refresh();
sd2card.readBlock(blkAddr, pBuf);
sd2card->readBlock(blkAddr, pBuf);
return true;
}

// multi block optmization
sd2card.readStart(blkAddr);
sd2card->readStart(blkAddr);
while (blkLen--) {
watchdog_refresh();
sd2card.readData(pBuf);
sd2card->readData(pBuf);
pBuf += BLOCK_SIZE;
}
sd2card.readStop();
sd2card->readStop();
return true;
}

bool IsReady() {
return card.isMounted();
return diskIODriver()->isReady();
}
};

Expand Down Expand Up @@ -105,8 +117,8 @@ USBMscHandler *pSingleMscHandler = &usbMscHandler;
void MSC_SD_init() {
USBDevice.end();
delay(200);
USBDevice.begin();
USBDevice.registerMscHandlers(1, &pSingleMscHandler, Marlin_STORAGE_Inquirydata);
USBDevice.begin();
}

#endif // __STM32F1__ && HAS_SD_HOST_DRIVE
2 changes: 1 addition & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
TERN_(SDSUPPORT, card.manage_media());

// Handle USB Flash Drive insert / remove
TERN_(USB_FLASH_DRIVE_SUPPORT, Sd2Card::idle());
TERN_(USB_FLASH_DRIVE_SUPPORT, card.diskIODriver()->idle());

// Announce Host Keepalive state (if any)
TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
#define _CUTTER_POWER_PERCENT 2
#define _CUTTER_POWER_RPM 3
#define _CUTTER_POWER(V) _CAT(_CUTTER_POWER_, V)
#define CUTTER_UNIT_IS(V) (_CUTTER_POWER(CUTTER_POWER_UNIT) == _CUTTER_POWER(V))
#define CUTTER_UNIT_IS(V) (_CUTTER_POWER(CUTTER_POWER_UNIT) == _CUTTER_POWER(V))
#endif

// Add features that need hardware PWM here
Expand Down
9 changes: 9 additions & 0 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@
#define SD_DETECT_STATE LOW
#endif
#endif

#if DISABLED(USB_FLASH_DRIVE_SUPPORT) || BOTH(MULTI_VOLUME, VOLUME_SD_ONBOARD)
#if ENABLED(SDIO_SUPPORT)
#define NEED_SD2CARD_SDIO 1
#else
#define NEED_SD2CARD_SPI 1
#endif
#endif

#endif

#if ANY(HAS_GRAPHICAL_TFT, LCD_USE_DMA_FSMC, HAS_FSMC_GRAPHICAL_TFT, HAS_SPI_GRAPHICAL_TFT) || !PIN_EXISTS(SD_DETECT)
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/extui/lib/mks_ui/draw_keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
public_buf_l[6] = 0x00;
raw_send_to_wifi((uint8_t*)public_buf_l, 6);

last_disp_state = KEY_BOARD_UI;
last_disp_state = KEYBOARD_UI;
lv_clear_keyboard();
wifi_tips_type = TIPS_TYPE_JOINING;
lv_draw_wifi_tips();
Expand Down Expand Up @@ -216,7 +216,7 @@ static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
}

void lv_draw_keyboard() {
scr = lv_screen_create(KEY_BOARD_UI, "");
scr = lv_screen_create(KEYBOARD_UI, "");

// Create styles for the keyboard
static lv_style_t rel_style, pr_style;
Expand Down
73 changes: 73 additions & 0 deletions Marlin/src/lcd/extui/lib/mks_ui/draw_media_select.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "../../../../inc/MarlinConfigPre.h"

#if BOTH(HAS_TFT_LVGL_UI, MULTI_VOLUME)

#include "draw_ui.h"
#include <lv_conf.h>

#include "../../../../inc/MarlinConfig.h"
#include "../../../../sd/cardreader.h"

extern lv_group_t *g;
static lv_obj_t *scr;

enum {
ID_T_USB_DISK = 1,
ID_T_SD_DISK,
ID_T_RETURN
};

#if ENABLED(MKS_TEST)
extern uint8_t curent_disp_ui;
#endif

static void event_handler(lv_obj_t *obj, lv_event_t event) {
if (event != LV_EVENT_RELEASED) return;
lv_clear_media_select();
switch (obj->mks_obj_id) {
case ID_T_USB_DISK: card.changeMedia(&card.media_usbFlashDrive); break;
case ID_T_SD_DISK: card.changeMedia(&card.media_sd_spi); break;
case ID_T_RETURN:
TERN_(MKS_TEST, curent_disp_ui = 1);
lv_draw_ready_print();
return;
}
lv_draw_print_file();
}

void lv_draw_media_select() {
scr = lv_screen_create(MEDIA_SELECT_UI);
lv_big_button_create(scr, "F:/bmp_sd.bin", media_select_menu.sd_disk, INTERVAL_V, titleHeight, event_handler, ID_T_SD_DISK);
lv_big_button_create(scr, "F:/bmp_usb_disk.bin", media_select_menu.usb_disk, BTN_X_PIXEL + INTERVAL_V * 2, titleHeight, event_handler, ID_T_USB_DISK);
lv_big_button_create(scr, "F:/bmp_return.bin", common_menu.text_back, BTN_X_PIXEL * 3 + INTERVAL_V * 4, BTN_Y_PIXEL + INTERVAL_H + titleHeight, event_handler, ID_T_RETURN);
}

void lv_clear_media_select() {
#if HAS_ROTARY_ENCODER
if (gCfgItems.encoder_enable) lv_group_remove_all_objs(g);
#endif
lv_obj_del(scr);
}

#endif // HAS_TFT_LVGL_UI
33 changes: 33 additions & 0 deletions Marlin/src/lcd/extui/lib/mks_ui/draw_media_select.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once

#ifdef __cplusplus
extern "C" { /* C-declarations for C++ */
#endif

extern void lv_draw_media_select();
extern void lv_clear_media_select();

#ifdef __cplusplus
} /* C-declarations for C++ */
#endif
3 changes: 2 additions & 1 deletion Marlin/src/lcd/extui/lib/mks_ui/draw_print_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static void event_handler(lv_obj_t *obj, lv_event_t event) {
}
else {
lv_clear_print_file();
lv_draw_ready_print();
TERN(MULTI_VOLUME, lv_draw_media_select(), lv_draw_ready_print());
}
}
else {
Expand Down Expand Up @@ -248,6 +248,7 @@ static char test_public_buf_l[40];
void disp_gcode_icon(uint8_t file_num) {
uint8_t i;

// TODO: set current media title?!
scr = lv_screen_create(PRINT_FILE_UI, "");

// Create image buttons
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/extui/lib/mks_ui/draw_ready_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void event_handler(lv_obj_t *obj, lv_event_t event) {
case ID_INFO_EXT: uiCfg.curTempType = 0; lv_draw_preHeat(); break;
case ID_INFO_BED: uiCfg.curTempType = 1; lv_draw_preHeat(); break;
case ID_INFO_FAN: lv_draw_fan(); break;
case ID_PRINT: lv_draw_print_file(); break;
case ID_PRINT: TERN(MULTI_VOLUME, lv_draw_media_select(), lv_draw_print_file()); break;
}
}

Expand Down
Loading

0 comments on commit 138340e

Please sign in to comment.