/**************************************************************************//** * @brief Initialize/retarget a TEXTDISPLAY device to receivie stdout(put). * * @return EMSTATUS code of the operation. *****************************************************************************/ EMSTATUS RETARGET_TextDisplayInit(void) { EMSTATUS status; DISPLAY_Device_t displayDevice; TEXTDISPLAY_Config_t textDisplayConfig; /* Query that the specified DISPLAY device is available. */ status = DISPLAY_DeviceGet(RETARGETTEXTDISPLAY_DISPLAY_NO, &displayDevice); if (DISPLAY_EMSTATUS_OK == status) { textDisplayConfig.displayDeviceNo = RETARGETTEXTDISPLAY_DISPLAY_NO; textDisplayConfig.scrollEnable = RETARGETTEXTDISPLAY_SCROLL_MODE; textDisplayConfig.lfToCrLf = RETARGETTEXTDISPLAY_LINE_FEED_MODE; status = TEXTDISPLAY_New(&textDisplayConfig, &textDisplayHandle); #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__) if (TEXTDISPLAY_EMSTATUS_OK == status) { /* Set unbuffered mode for stdout (newlib) */ setvbuf(stdout, NULL, _IONBF, 0); } #endif } return status; }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main( void ) { /* Chip errata */ CHIP_Init(); /* Setup GPIO for pushbuttons. */ GpioSetup(); /* Initialize the display module. */ displayEnabled = true; DISPLAY_Init(); /* Retrieve the properties of the display. */ if ( DISPLAY_DeviceGet( 0, &displayDevice ) != DISPLAY_EMSTATUS_OK ) { /* Unable to get display handle. */ while( 1 ); } /* Retarget stdio to the display. */ if ( TEXTDISPLAY_EMSTATUS_OK != RETARGET_TextDisplayInit() ) { /* Text display initialization failed. */ while( 1 ); } /* Set PCNT to generate an interrupt every second. */ PcntInit(); printf( "\n\n Cycling through" "\n energy modes" "\n EM0-EM3" "\n\n Push PB0 to" "\n enter EM4\n\n\n\n" ); /* Turn on LFXO to be able to see the difference between EM2 and EM3. */ CMU_OscillatorEnable(cmuOsc_LFXO, true, false ); for (;;) { printf( "\r EM0" ); EnterEMode( 0, SLEEP_TIME ); CheckEM4Entry(); printf( "\r EM1" ); EnterEMode( 1, SLEEP_TIME ); CheckEM4Entry(); printf( "\r EM2" ); EnterEMode( 2, SLEEP_TIME ); CheckEM4Entry(); printf( "\r EM3" ); EnterEMode( 3, SLEEP_TIME ); CheckEM4Entry(); } }
/**************************************************************************//** * @brief main - the entrypoint after reset. *****************************************************************************/ int main(void) { /* Initialize LEUSB state variables */ leusbTogglePushed = false; leusbEnabled = false; refreshDisplay = false; HIDKBD_Init_t hidInitStruct; /* Chip errata */ CHIP_Init(); /* Go slow to reduce current consumption. */ CMU_HFRCOBandSet( cmuHFRCOBand_7MHz ); CMU_ClockEnable( cmuClock_GPIO, true ); GPIO_PinModeSet( BUTTON0_PORT, BUTTON0_PIN, gpioModeInputPull, 1 ); GPIO_PinModeSet( BUTTON1_PORT, BUTTON1_PIN, gpioModeInputPull, 1 ); /* Initialize the display module. */ DISPLAY_Init(); /* Retrieve the properties of the display. */ if ( DISPLAY_DeviceGet( 0, &displayDevice ) != DISPLAY_EMSTATUS_OK ) { /* Unable to get display handle. */ while( 1 ); } memset( (void*)blank_image, 0xFF, 128*16 ); displayDevice.pPixelMatrixDraw( &displayDevice, (void*)blank_image, /* start column, width */ 0, displayDevice.geometry.width, /* start row, height */ 0, displayDevice.geometry.height); scrollLcd( &displayDevice, scrollLeft, blank_image, gecko_image ); /* Initialize HID keyboard driver. */ hidInitStruct.hidDescriptor = (void*)USBDESC_HidDescriptor; hidInitStruct.setReportFunc = NULL; HIDKBD_Init( &hidInitStruct ); /* Initialize and start USB device stack. */ USBD_Init( &usbInitStruct ); /* Turn off the Low Energy Mode (LEM) features that were enabled in USBD_Init() */ USB->CTRL &= ~USB_CTRL_LEMIDLEEN; // LEUSB off to begin demo /* * When using a debugger it is practical to uncomment the following three * lines to force host to re-enumerate the device. */ /* USBD_Disconnect(); */ /* USBTIMER_DelayMs(1000); */ /* USBD_Connect(); */ for (;;) { if (refreshDisplay) { refreshDisplay = false; /* Clear the "refresh display" flag */ if (leusbEnabled) { /* Update the LCD image to reflect USB Low Energy Mode enabled */ displayDevice.pPixelMatrixDraw( &displayDevice, (void*)leusb_image, /* start column, width */ 0, displayDevice.geometry.width, /* start row, height */ 0, displayDevice.geometry.height); } else { /* Update the LCD image to reflect normal USB HID keyboard demo status */ displayDevice.pPixelMatrixDraw( &displayDevice, (void*)usb_image, /* start column, width */ 0, displayDevice.geometry.width, /* start row, height */ 0, displayDevice.geometry.height); } } /* Conserve energy ! */ EMU_EnterEM1(); } }
/**************************************************************************//** * @brief Create a new text display device. * * @param[in] config Configuration data structure for the text display device * to create. * @param[out] handle Pointer to text display handle which will be returned * if the function is successful. * * @return EMSTATUS code of the operation. *****************************************************************************/ EMSTATUS TEXTDISPLAY_New(TEXTDISPLAY_Config_t *config, TEXTDISPLAY_Handle_t *handle) { TEXTDISPLAY_Device_t* textdisplay; int deviceNo; unsigned int i; EMSTATUS status; /* Find unused text display device structure. */ for (textdisplay=NULL, deviceNo=0; deviceNo<TEXTDISPLAY_DEVICES_MAX; deviceNo++) { if (false == textdisplayTbl[deviceNo].initialized) { textdisplay = &textdisplayTbl[deviceNo]; break; } } if (NULL == textdisplay) { return TEXTDISPLAY_EMSTATUS_NOT_ENOUGH_MEMORY; } /* Retrieve the properties of the DISPLAY. */ status = DISPLAY_DeviceGet(config->displayDeviceNo, &textdisplay->displayDevice); if (DISPLAY_EMSTATUS_OK != status) return status; /* If the display address mode is DISPLAY_ADDRESSING_BY_ROWS_ONLY we need a buffer to draw the current line with new characters as they arrive. */ if (DISPLAY_ADDRESSING_BY_ROWS_ONLY == textdisplay->displayDevice.addressMode) { status = textdisplay->displayDevice. pPixelMatrixAllocate (&textdisplay->displayDevice, textdisplay->displayDevice.geometry.width, #ifdef EMWIN_WORKAROUND textdisplay->displayDevice.geometry.width, #endif FONT_HEIGHT, &textdisplay->lineBuffer); if (DISPLAY_EMSTATUS_OK != status) return status; status = textdisplay->displayDevice. pPixelMatrixClear (&textdisplay->displayDevice, textdisplay->lineBuffer, textdisplay->displayDevice.geometry.width, FONT_HEIGHT); if (DISPLAY_EMSTATUS_OK != status) return status; } else { textdisplay->lineBuffer = NULL; } /* Check that the requested number of text rows and columns fits inside the geometry of the display device. */ if (charBufferTbl[deviceNo].lines > textdisplay->displayDevice.geometry.height / FONT_HEIGHT) return TEXTDISPLAY_EMSTATUS_OUT_OF_RANGE; if (charBufferTbl[deviceNo].columns > textdisplay->displayDevice.geometry.width / FONT_WIDTH) return TEXTDISPLAY_EMSTATUS_OUT_OF_RANGE; /* Remember the character buffer parameters. */ textdisplay->lines = charBufferTbl[deviceNo].lines; textdisplay->columns = charBufferTbl[deviceNo].columns; textdisplay->charBuffer = charBufferTbl[deviceNo].charBuffer; textdisplay->charArray = charBufferTbl[deviceNo].charArray; /* Store user configuration options */ textdisplay->displayDeviceNo = config->displayDeviceNo; textdisplay->scrollEnable = config->scrollEnable; textdisplay->lfToCrLf = config->lfToCrLf; /* Setup char pointer array */ for (i=0; i<textdisplay->lines; i++) textdisplay->charArray[i] = textdisplay->charBuffer + textdisplay->columns * i; /* Initialize color for text */ /* Use \b for red text (bell/warning) */ textdisplay->rgbColor[0] = 0xff; textdisplay->rgbColor[1] = 0xff; textdisplay->rgbColor[2] = 0xff; /* Clear character buffer */ TextdisplayClear(textdisplay); textdisplay->initialized = true; *handle = (TEXTDISPLAY_Handle_t*) textdisplay; return TEXTDISPLAY_EMSTATUS_OK; }