void drawScreen(void) { char *msg = "ChibiOS/GFX"; font_t font1, font2; font1 = gdispOpenFont("UI2 Double"); font2 = gdispOpenFont("LargeNumbers"); gdispClear(White); gdispDrawString(gdispGetWidth()-gdispGetStringWidth(msg, font1)-3, 3, msg, font1, Black); /* colors */ gdispFillArea(0 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Black); /* Black */ gdispFillArea(1 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Red); /* Red */ gdispFillArea(2 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Yellow); /* Yellow */ gdispFillArea(3 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Green); /* Green */ gdispFillArea(4 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Blue); /* Blue */ gdispDrawBox (5 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Black); /* White */ /* pens */ gdispDrawString(OFFSET * 2, DRAW_PEN(1), "1", font2, Black); gdispDrawString(OFFSET * 2, DRAW_PEN(2), "2", font2, Black); gdispDrawString(OFFSET * 2, DRAW_PEN(3), "3", font2, Black); gdispDrawString(OFFSET * 2, DRAW_PEN(4), "4", font2, Black); gdispDrawString(OFFSET * 2, DRAW_PEN(5), "5", font2, Black); gdispCloseFont(font1); gdispCloseFont(font2); }
void drawScreen(void) { char *msg = "uGFX"; font_t font1, font2; font1 = gdispOpenFont("DejaVuSans24*"); font2 = gdispOpenFont("DejaVuSans12*"); gdispClear(White); gdispDrawString(gdispGetWidth()-gdispGetStringWidth(msg, font1)-3, 3, msg, font1, Black); /* colors */ gdispFillArea(0 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Black); /* Black */ gdispFillArea(1 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Red); /* Red */ gdispFillArea(2 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Yellow); /* Yellow */ gdispFillArea(3 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Green); /* Green */ gdispFillArea(4 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Blue); /* Blue */ gdispDrawBox (5 * COLOR_SIZE + 3, 3, COLOR_SIZE, COLOR_SIZE, Black); /* White */ /* pens */ gdispFillStringBox(OFFSET * 2, DRAW_PEN(1), PEN_SIZE, PEN_SIZE, "1", font2, White, Black, justifyCenter); gdispFillStringBox(OFFSET * 2, DRAW_PEN(2), PEN_SIZE, PEN_SIZE, "2", font2, White, Black, justifyCenter); gdispFillStringBox(OFFSET * 2, DRAW_PEN(3), PEN_SIZE, PEN_SIZE, "3", font2, White, Black, justifyCenter); gdispFillStringBox(OFFSET * 2, DRAW_PEN(4), PEN_SIZE, PEN_SIZE, "4", font2, White, Black, justifyCenter); gdispFillStringBox(OFFSET * 2, DRAW_PEN(5), PEN_SIZE, PEN_SIZE, "5", font2, White, Black, justifyCenter); gdispCloseFont(font1); gdispCloseFont(font2); }
int main(void) { uint8_t i; font_t font1, font2; /* initialize and clear the display */ gfxInit(); /* Set some fonts */ font1 = gdispOpenFont("UI2"); font2 = gdispOpenFont("DejaVu Sans 12"); gwinSetDefaultFont(font1); /* create the three console windows */ { GWindowInit wi; wi.show = TRUE; wi.x = 0; wi.y = 0; wi.width = gdispGetWidth(); wi.height = gdispGetHeight()/2; GW1 = gwinConsoleCreate(NULL, &wi); wi.y = gdispGetHeight()/2; wi.width = gdispGetWidth()/2; wi.height = gdispGetHeight(); GW2 = gwinConsoleCreate(NULL, &wi); wi.x = gdispGetWidth()/2; wi.height = gdispGetHeight(); GW3 = gwinConsoleCreate(NULL, &wi); } /* Use a special font for GW1 */ gwinSetFont(GW1, font2); /* Set the fore- and background colors for each console */ gwinSetColor(GW1, Green); gwinSetBgColor(GW1, Black); gwinSetColor(GW2, White); gwinSetBgColor(GW2, Blue); gwinSetColor(GW3, Black); gwinSetBgColor(GW3, Red); /* clear all console windows - to set background */ gwinClear(GW1); gwinClear(GW2); gwinClear(GW3); /* Output some data on the first console */ for(i = 0; i < 10; i++) { gwinPrintf(GW1, "Hello uGFX!\r\n"); } /* Output some data on the second console */ for(i = 0; i < 16; i++) { gwinPrintf(GW2, "Message Nr.: %d\r\n", i+1); } /* Output some data on the third console */ for(i = 0; i < 18; i++) { gwinPrintf(GW3, "Message Nr.: %d\r\n", i+1); } while(TRUE) { gfxSleepMilliseconds(500); } }
void menuScreen(){ //color_t colorrange[9] = {White, Yellow, Red, Blue, Magenta, SkyBlue, Orange,Lime,Black}; // 0 1 2 3 4 5 6 7 8 // Main menu Background gdispFillArea(433, 247, 20, 20, tetrisShapeColors[2]); gdispFillArea(112, 247, 20, 20, tetrisShapeColors[7]); gdispFillArea(455, 247, 20, 20, tetrisShapeColors[2]); gdispFillArea(0, 204, 20, 20, tetrisShapeColors[3]); gdispFillArea(23, 225, 20, 20, tetrisShapeColors[1]); gdispFillArea(455, 224, 20, 20, tetrisShapeColors[2]); gdispFillArea(91, 247, 20, 20, tetrisShapeColors[7]); gdispFillArea(83, 80, 20, 20, tetrisShapeColors[4]); gdispFillArea(432, 203, 20, 20, tetrisShapeColors[6]); gdispFillArea(178, 247, 20, 20, tetrisShapeColors[5]); gdispFillArea(134, 247, 20, 20, tetrisShapeColors[7]); gdispFillArea(455, 201, 20, 20, tetrisShapeColors[2]); gdispFillArea(22, 247, 20, 20, tetrisShapeColors[3]); gdispFillArea(46, 248, 20, 20, tetrisShapeColors[1]); gdispFillArea(46, 225, 20, 20, tetrisShapeColors[1]); gdispFillArea(82, 102, 21, 20, tetrisShapeColors[4]); gdispFillArea(70, 247, 20, 20, tetrisShapeColors[7]); gdispFillArea(60, 102, 20, 20, tetrisShapeColors[4]); gdispFillArea(410, 225, 20, 20, tetrisShapeColors[6]); gdispFillArea(156, 247, 20, 20, tetrisShapeColors[5]); gdispFillArea(134, 225, 20, 20, tetrisShapeColors[5]); gdispFillArea(410, 247, 20, 20, tetrisShapeColors[6]); gdispFillArea(0, 226, 20, 20, tetrisShapeColors[3]); gdispFillArea(47, 227, 20, 20, tetrisShapeColors[1]); gdispFillArea(60, 80, 20, 20, tetrisShapeColors[4]); gdispFillArea(69, 225, 20, 20, tetrisShapeColors[1]); gdispFillArea(0, 247, 20, 20, tetrisShapeColors[3]); gdispFillArea(432, 225, 20, 20, tetrisShapeColors[6]); gdispFillArea(157, 225, 20, 20, tetrisShapeColors[5]); //Button gdispFillArea(200,60,80,30,Grey); gdispFillArea(200+3,60+3,80-6,30-6,Silver); //text font16 = gdispOpenFont("DejaVuSans16"); gdispDrawString(225, 66, "Play", font16, tetrisShapeColors[8]); gdispCloseFont(font16); //Button gdispFillArea(200,110,96,30,Grey); gdispFillArea(200+3,110+3,96-6,30-6,Silver); //text font16 = gdispOpenFont("DejaVuSans16"); gdispDrawString(205, 116, "Dark Mode", font16, tetrisShapeColors[8]); gdispCloseFont(font16); //Button gdispFillArea(200,160,126,30,Grey); gdispFillArea(200+3,160+3,126-6,30-6,Silver); //text font16 = gdispOpenFont("DejaVuSans16"); gdispDrawString(205, 166, "Random Mode", font16, tetrisShapeColors[8]); gdispCloseFont(font16); }
int main(void) { uint8_t i; font_t font1, font2; halInit(); chSysInit(); /* initialize and clear the display */ gdispInit(); gdispClear(Black); font1 = gdispOpenFont("UI2 Double"); font2 = gdispOpenFont("Small"); /* create the three console windows and set a font for each */ GW1 = gwinCreateConsole(NULL, 0, 0, gdispGetWidth(), gdispGetHeight()/2, font1); GW2 = gwinCreateConsole(NULL, 0, gdispGetHeight()/2, gdispGetWidth()/2, gdispGetHeight(), font2); GW3 = gwinCreateConsole(NULL, gdispGetWidth()/2, gdispGetHeight()/2, gdispGetWidth(), gdispGetHeight(), font2); /* Set the fore- and background colors for each console */ gwinSetColor(GW1, Green); gwinSetBgColor(GW1, Black); gwinSetColor(GW2, White); gwinSetBgColor(GW2, Blue); gwinSetColor(GW3, Black); gwinSetBgColor(GW3, Red); /* clear all console windows - to set background */ gwinClear(GW1); gwinClear(GW2); gwinClear(GW3); /* receive the stream pointers of each console */ S1 = gwinGetConsoleStream(GW1); S2 = gwinGetConsoleStream(GW2); S3 = gwinGetConsoleStream(GW3); /* Output some data on the first console */ for(i = 0; i < 10; i++) { chprintf(S1, "Hello ChibiOS/GFX!\r\n"); } /* Output some data on the second console */ for(i = 0; i < 16; i++) { chprintf(S2, "Message Nr.: %d\r\n", i+1); } /* Output some data on the third console */ for(i = 0; i < 18; i++) { chprintf(S3, "Message Nr.: %d\r\n", i+1); } while(TRUE) { chThdSleepMilliseconds(500); } }
void benchmark(void) { uint32_t i, pixels, ms, pps; char pps_str[25]; coord_t height, width, rx, ry, rcx, rcy; color_t random_color; font_t font; gdispSetOrientation(GDISP_ROTATE_90); width = gdispGetWidth(); height = gdispGetHeight(); font = gdispOpenFont("UI2 Double"); gdispDrawStringBox(0, 0, width, 30, "ChibiOS/GFX - Benchmark", font, White, justifyCenter); font = gdispOpenFont("UI2"); gdispDrawStringBox(0, height/2, width, 30, "5000 random rectangles", font, White, justifyCenter); gfxSleepMilliseconds(3000); /* seed for the rand() */ srand(DWT_CYCCNT); pixels = 0; CPU_RESET_CYCLECOUNTER; for (i = 0; i < 5000; i++) { random_color = (rand() % 65535); rx = (rand() % (width-10)); ry = (rand() % (height-10)); rcx = (rand() % ((width-rx)-10))+10; rcy = (rand() % ((height-ry)-10))+10; gdispFillArea(rx, ry, rcx, rcy, random_color); pixels += (rcx+1)*(rcy+1); } ms = DWT_CYCCNT / 168000; pps = (float)pixels/((float)ms/1000.0f); memset (pps_str, 0, sizeof(pps_str)); uitoa(pps, pps_str, sizeof(pps_str)); strcat(pps_str, " Pixels/s"); font = gdispOpenFont("UI2 Double"); gdispClear(Black); gdispDrawStringBox(0, 0, width, 30, "ChibiOS/GFX - Benchmark", font, White, justifyCenter); gdispDrawStringBox(0, height/2, width, 30, pps_str, font, White, justifyCenter); //gdispDrawString(20, height/2, pps_str, font, White); }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); gfxInit(); // Set the widget defaults gwinSetDefaultFont(gdispOpenFont("UI2")); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(White); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { chThdSleepMilliseconds(500); } }
int main(void) { GEvent* pe; // Initialize the display gfxInit(); // Set the widget defaults gwinSetDefaultFont(gdispOpenFont("*")); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(White); // create the widget createWidgets(); // We want to listen for widget events geventListenerInit(&gl); gwinAttachListener(&gl); while(1) { // Get an Event pe = geventEventWait(&gl, TIME_INFINITE); } return 0; }
void printNextBrickWindow() { char* str; int i,j; font16 = gdispOpenFont("DejaVuSans16"); gdispDrawString(360, 100, "Next", font16, tetrisShapeColors[8]); gdispCloseFont(font16); //draw block //For every piece in the next block needs to be changed to nextPiece for (i = 0; i < nextPiece->size; i++) { for (j = 0; j < nextPiece->size; j++) { //If this isn't a blank char then print it if (nextPiece->graphic[i][j] != ' ') { // mvprintw(currentPiece->y + i, currentPiece->x + j, "%c", currentPiece->graphic[i][j]); //padding gdispFillArea(360+(j*BLOCK_SIZE),130+(i*(BLOCK_SIZE)),BLOCK_SIZE-2, BLOCK_SIZE-2, tetrisShapeColors[0]); //shape gdispFillArea(361+(j*BLOCK_SIZE),131+(i*(BLOCK_SIZE)),BLOCK_SIZE-2, BLOCK_SIZE-2, tetrisShapeColors[nextPiece->blockID]); } } } }
int main(void) { coord_t width, height; font_t font1, font2, font3, font4; const char *msg; halInit(); chSysInit(); /* Initialize and clear the display */ gdispInit(); gdispClear(Black); // Get the screen size width = gdispGetWidth(); height = gdispGetHeight(); // Get the fonts we want to use font1 = gdispOpenFont("UI2"); font2 = gdispOpenFont("UI2 Double"); font3 = gdispOpenFont("UI2 Narrow"); font4 = gdispOpenFont("LargeNumbers"); // Display large numbers on the right (measuring the string) msg = "123456"; gdispDrawString(width-gdispGetStringWidth(msg, font4)-3, 3, msg, font4, Green); // Display the font name under it. msg = gdispGetFontName(font4); gdispDrawString(width-gdispGetStringWidth(msg, font1)-3, 20, msg, font1, Green); // Demonstrate our other fonts gdispDrawString(10, 10, "Writing with Font 'UI2'", font1, Yellow); gdispFillString(10, 35, "Writing with Font 'UI2 Double'", font2, Red, White); gdispDrawStringBox(0, 50, width, 40, "Writing with Font 'UI2 Narrow'", font3, Red, justifyCenter); gdispFillStringBox(0, 90, width, 40, "Filled Centered", font3, Pink, Gray, justifyCenter); // Clean up the fonts gdispCloseFont(font1); gdispCloseFont(font2); gdispCloseFont(font3); gdispCloseFont(font4); // Wait forever while(TRUE) { chThdSleepMilliseconds(500); } }
int main(void) { // uint16_t i; // one wire initialization static OneWireDriver owDrv; static const OneWireConfig owCfg = { .dqPort = GPIOB, .dqPad = 10, .dqAlternate = 7, .uartd = &UARTD3 }; static OneWireRomAddress romAddr[8]; static bool_t searched = FALSE; static bool_t initialized = FALSE; // Initialize the uGFX and the underlying system gfxInit(); // Lock screen uint8_t secret_sequence[UNLOCKER_COLS * UNLOCKER_ROWS]; displayUnlockerSetup(secret_sequence); displayUnlocker(secret_sequence); // Set the widget defaults gwinSetDefaultFont(gdispOpenFont("UI2")); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(Black); // Create the widget createWidgets(); // Print to the console gwinPrintf(ghConsole, "Witamy w konsoli!\r\n\n"); gwinPrintf(ghConsole, "Odczyt temp z czujnikow DS18B20.\r\n"); //initialize ds18b20 if (initialized == FALSE) { oneWireInit(&owDrv, &owCfg); initialized = TRUE; } if (searched == FALSE) { oneWireSearchRom (&owDrv, FALSE, romAddr, ARRAY_LEN(romAddr)); searched = TRUE; } while (1) { oneWireSearchRom (&owDrv, FALSE, romAddr, ARRAY_LEN(romAddr)); for (uint8_t i=0; i< ARRAY_LEN(romAddr); i++) { if (romAddr[i].addr[0] == 0x28) { ds1820BInit (&owDrv, &(romAddr[i]), 10); const float temp =ds1820BGetTemp (&owDrv, &(romAddr[i])); gwinPrintf(ghConsole, "czujnik %d temp =%.2f\r\n", i, temp); } } gfxSleepMilliseconds(800); } return 0; }
void printScoreWindow() { char* str; font16 = gdispOpenFont("DejaVuSans16"); gdispDrawString(360, 20, "Score", font16, tetrisShapeColors[8]); // gdispDrawLine(365, 40, 380, 20, Purple); gdispFillArea(365, 35, 35, 1, Purple); //draw current score sprintf(str,"%d",Score); gdispDrawString(378, 42, str, font16, Purple); gdispCloseFont(font16); }
int main(void) { GEvent* pe; // Initialize the display gfxInit(); // Attach the mouse input gwinAttachMouse(0); // Set the widget defaults gwinSetDefaultFont(gdispOpenFont("*")); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(White); // create the widget _createWidgets(); // We want to listen for widget events geventListenerInit(&gl); gwinAttachListener(&gl); while(1) { // Get an Event pe = geventEventWait(&gl, TIME_INFINITE); switch(pe->type) { case GEVENT_GWIN_SLIDER: if (((GEventGWinSlider *)pe)->gwin == ghSliderR || \ ghSliderG || \ ghSliderB ) { _updateColor(); } break; case GEVENT_GWIN_BUTTON: if (((GEventGWinButton *)pe)->gwin == ghButton1) { gwinSliderSetPosition(ghSliderR, rand() % 256); } else if (((GEventGWinButton *)pe)->gwin == ghButton2) { gwinSliderSetPosition(ghSliderG, rand() % 256); } else if (((GEventGWinButton *)pe)->gwin == ghButton3) { gwinSliderSetPosition(ghSliderB, rand() % 256); } _updateColor(); default: break; } } return 0; }
int main(void) { GEventMouse ev; #if !JG10_SHOW_SPLASH font_t font; #endif gfxInit(); ginputGetMouse(0); jg10Init(); #if JG10_SHOW_SPLASH jg10ShowSplash(); #else font = gdispOpenFont("DejaVuSans16_aa"); gdispDrawString((gdispGetWidth()/2)-(gdispGetStringWidth("Touch to start!", font)/2), gdispGetHeight()/2, "Touch to start!", font, White); gdispCloseFont(font); #endif while (TRUE) { ginputGetMouseStatus(0, &ev); if (ev.buttons & GINPUT_MOUSE_BTN_LEFT) { while (ev.buttons & GINPUT_MOUSE_BTN_LEFT) { // Wait until release ginputGetMouseStatus(0, &ev); } #if !JG10_SHOW_SPLASH font = gdispOpenFont("DejaVuSans16"); gdispFillArea((gdispGetWidth()/2)-(gdispGetStringWidth("Touch to start!", font)/2), gdispGetHeight()/2, gdispGetWidth()/2, 17, Black); gdispCloseFont(font); #endif jg10Start(); } } }
int main(void) { font_t font1; // Initialize uGFX and the underlying system gfxInit(); // Get the fonts we want to use font1 = gdispOpenFont("Archangelsk Regular 12"); // Demonstrate our other fonts gdispDrawString(10, 10, "привет мир", font1, Yellow); // Wait forever while(TRUE) { gfxSleepMilliseconds(500); } }
/// \classmethod \constructor(skin_position) /// /// Construct an UGFX object. STATIC mp_obj_t pyb_ugfx_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check arguments mp_arg_check_num(n_args, n_kw, 0, 0, false); // get LCD position //const char *lcd_id = mp_obj_str_get_str(args[0]); // create lcd object pyb_ugfx_obj_t *ugfx = m_new_obj(pyb_ugfx_obj_t); ugfx->base.type = &pyb_ugfx_type; default_font = gdispOpenFont("ui2"); //TODO: allow to be changed //gdispCloseFont(font); gfxInit(); return ugfx; }
int main(void) { // Initialize the display gfxInit(); // Set the widget defaults gwinSetDefaultFont(gdispOpenFont("*")); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(White); // Create the widget createWidgets(); while(1) { gfxSleepMilliseconds(1000); } return 0; }
int main(void) { GEvent* pe; // Initialize the display gfxInit(); // Set the widget defaults gwinSetDefaultFont(gdispOpenFont("UI2")); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(White); // Attach the mouse input gwinAttachMouse(0); // create the widget createWidgets(); // We want to listen for widget events geventListenerInit(&gl); gwinAttachListener(&gl); while(1) { // Get an Event pe = geventEventWait(&gl, TIME_INFINITE); switch(pe->type) { case GEVENT_GWIN_CHECKBOX: if (((GEventGWinCheckbox*)pe)->checkbox == ghCheckbox1) { // The state of our checkbox has changed printf("Checkbox state: %d\r\n", ((GEventGWinCheckbox*)pe)->isChecked); } break; default: break; } } return 0; }
bool_t ginputCalibrateMouse(uint16_t instance) { #if !GINPUT_MOUSE_NEED_CALIBRATION (void) instance; return FALSE; #else const coord_t height = gdispGetHeight(); const coord_t width = gdispGetWidth(); const MousePoint cross[] = {{(width / 4), (height / 4)}, {(width - (width / 4)) , (height / 4)}, {(width - (width / 4)) , (height - (height / 4))}, {(width / 2), (height / 2)}}; /* Check point */ MousePoint points[GINPUT_MOUSE_CALIBRATION_POINTS]; const MousePoint *pc; MousePoint *pt; int32_t px, py; unsigned i, j; font_t font1, font2; #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 unsigned err; #endif if (instance || (MouseConfig.flags & FLG_IN_CAL)) return FALSE; font1 = gdispOpenFont(GINPUT_MOUSE_CALIBRATION_FONT); font2 = gdispOpenFont(GINPUT_MOUSE_CALIBRATION_FONT2); MouseConfig.flags |= FLG_IN_CAL; gtimerStop(&MouseTimer); MouseConfig.flags &= ~(FLG_CAL_OK|FLG_CAL_SAVED); #if GDISP_NEED_CONTROL gdispSetOrientation(GDISP_ROTATE_0); #endif #if GDISP_NEED_CLIP gdispSetClip(0, 0, width, height); #endif #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 while(1) { #endif gdispClear(Blue); gdispFillStringBox(0, 5, width, 30, GINPUT_MOUSE_CALIBRATION_TEXT, font1, White, Blue, justifyCenter); for(i = 0, pt = points, pc = cross; i < GINPUT_MOUSE_CALIBRATION_POINTS; i++, pt++, pc++) { _tsDrawCross(pc); do { /* Wait for the mouse to be pressed */ while(get_raw_reading(&MouseConfig.t), !(MouseConfig.t.buttons & GINPUT_MOUSE_BTN_LEFT)) chThdSleepMilliseconds(20); /* Average all the samples while the mouse is down */ for(px = py = 0, j = 0; chThdSleepMilliseconds(20), /* Settling time between readings */ get_raw_reading(&MouseConfig.t), (MouseConfig.t.buttons & GINPUT_MOUSE_BTN_LEFT); j++) { px += MouseConfig.t.x; py += MouseConfig.t.y; } } while(!j); pt->x = px / j; pt->y = py / j; _tsClearCross(pc); if (i >= 1 && pt->x == (pt-1)->x && pt->y == (pt-1)->y) { gdispFillStringBox(0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_SAME_TEXT, font2, Red, Yellow, justifyCenter); chThdSleepMilliseconds(5000); gdispFillArea(0, 35, width, 40, Blue); } } /* Apply 3 point calibration algorithm */ _tsDo3PointCalibration(cross, points, &MouseConfig.caldata); /* Verification of correctness of calibration (optional) : * See if the 4th point (Middle of the screen) coincides with the calibrated * result. If point is within +/- Squareroot(ERROR) pixel margin, then successful calibration * Else, start from the beginning. */ #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 /* Transform the co-ordinates */ MouseConfig.t.x = points[3].x; MouseConfig.t.y = points[3].y; _tsTransform(&MouseConfig.t, &MouseConfig.caldata); /* Calculate the delta */ err = (MouseConfig.t.x - cross[3].x) * (MouseConfig.t.x - cross[3].x) + (MouseConfig.t.y - cross[3].y) * (MouseConfig.t.y - cross[3].y); if (err <= GINPUT_MOUSE_MAX_CALIBRATION_ERROR * GINPUT_MOUSE_MAX_CALIBRATION_ERROR) break; gdispFillStringBox(0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_ERROR_TEXT, font2, Red, Yellow, justifyCenter); chThdSleepMilliseconds(5000); } #endif // Restart everything gdispCloseFont(font1); gdispCloseFont(font2); MouseConfig.flags |= FLG_CAL_OK; MouseConfig.last_buttons = 0; get_calibrated_reading(&MouseConfig.t); MouseConfig.flags &= ~FLG_IN_CAL; if ((MouseConfig.flags & FLG_INIT_DONE)) gtimerStart(&MouseTimer, MousePoll, 0, TRUE, GINPUT_MOUSE_POLL_PERIOD); // Save the calibration data (if possible) if (MouseConfig.fnsavecal) { MouseConfig.fnsavecal(instance, (const uint8_t *)&MouseConfig.caldata, sizeof(MouseConfig.caldata)); MouseConfig.flags |= FLG_CAL_SAVED; } return TRUE; #endif }
/** * Our main function. * There are two prototypes - one for systems with a command line and one for embedded systems without one. */ int main(proto_args) { uint16_t cmd[5]; unsigned cnt; // Initialize and clear the display gfxInit(); font = gdispOpenFont("UI2"); // Open the connection gdispDrawStringBox(0, 0, gdispGetWidth(), gdispGetHeight(), "Connecting to host...", font, White, justifyCenter); StartSockets(); netfd = doConnect(cmd_args); if (netfd == (SOCKET_TYPE)-1) gfxHalt("Could not connect to the specified server"); gdispClear(Black); // Get the initial packet from the host if (!getpkt(cmd, 2)) goto alldone; if (cmd[0] != GNETCODE_INIT || cmd[1] != GNETCODE_VERSION) gfxHalt("Oops - The protocol doesn't look like one we understand"); // Get the rest of the initial arguments if (!getpkt(cmd, 4)) goto alldone; // cmd[] = width, height, pixelformat, hasmouse // We will ignore size mismatches but the pixel format must match if (cmd[2] != GDISP_PIXELFORMAT) gfxHalt("Oops - The remote display is using a different pixel format to us.\nTry defining GDISP_PIXELFORMAT in your gfxconf.h file."); #if GFX_USE_GINPUT && GINPUT_NEED_MOUSE // Start the mouse thread if needed if (cmd[3]) gfxThreadClose(gfxThreadCreate(waNetThread, sizeof(waNetThread), HIGH_PRIORITY, NetThread, 0)); #endif // Process incoming instructions while(getpkt(cmd, 1)) { switch(cmd[0]) { case GNETCODE_FLUSH: gdispFlush(); break; case GNETCODE_PIXEL: if (!getpkt(cmd, 3)) goto alldone; // cmd[] = x, y, color gdispDrawPixel(cmd[0], cmd[1], cmd[2]); break; case GNETCODE_FILL: if (!getpkt(cmd, 5)) goto alldone; // cmd[] = x, y, cx, cy, color gdispFillArea(cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); break; case GNETCODE_BLIT: if (!getpkt(cmd, 4)) goto alldone; // cmd[] = x, y, cx, cy - Followed by cx * cy pixels gdispStreamStart(cmd[0],cmd[1],cmd[2],cmd[3]); for(cnt = (unsigned)cmd[2] * cmd[3]; cnt; cnt--) { if (!getpkt(cmd, 1)) goto alldone; gdispStreamColor(cmd[0]); } gdispStreamStop(); break; #if GDISP_NEED_PIXELREAD case GNETCODE_READ: if (!getpkt(cmd, 2)) goto alldone; // cmd[] = x, y - Response is GNETCODE_READ,color cmd[1] = gdispGetPixelColor(cmd[0], cmd[1]); cmd[0] = GNETCODE_READ; if (!sendpkt(cmd, 2)) goto alldone; break; #endif #if GDISP_NEED_SCROLL case GNETCODE_SCROLL: if (!getpkt(cmd, 5)) goto alldone; // cmd[] = x, y, cx, cy, lines gdispVerticalScroll(cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], Black); break; #endif case GNETCODE_CONTROL: if (!getpkt(cmd, 2)) goto alldone; // cmd[] = what,data - Response is GNETCODE_CONTROL, 0x0000 (fail) or GNETCODE_CONTROL, 0x0001 (success) gdispControl(cmd[0], (void *)(unsigned)cmd[1]); switch(cmd[0]) { case GDISP_CONTROL_ORIENTATION: cmd[1] = (uint16_t)gdispGetOrientation() == cmd[1] ? 1 : 0; break; case GDISP_CONTROL_POWER: cmd[1] = (uint16_t)gdispGetPowerMode() == cmd[1] ? 1 : 0; break; case GDISP_CONTROL_BACKLIGHT: cmd[1] = (uint16_t)gdispGetBacklight() == cmd[1] ? 1 : 0; break; default: cmd[1] = 0; break; } cmd[0] = GNETCODE_CONTROL; if (!sendpkt(cmd, 2)) goto alldone; break; default: gfxHalt("Oops - The host has sent invalid commands"); break; } } alldone: closesocket(netfd); gfxHalt("Connection closed"); return 0; }
int main(void) { GEventKeyboard *pk; unsigned i; /* initialize and clear the display */ gfxInit(); /* Set a font */ gwinSetDefaultFont(gdispOpenFont("*")); // We want to listen for keyboard events geventListenerInit(&gl); geventAttachSource(&gl, ginputGetKeyboard(0), GLISTEN_KEYTRANSITIONS|GLISTEN_KEYUP); /* create the console window */ { GWindowInit wi; gwinClearInit(&wi); wi.show = TRUE; wi.x = 0; wi.y = 0; wi.width = gdispGetWidth(); wi.height = gdispGetHeight(); GW = gwinConsoleCreate(0, &wi); /* Set the fore- and background colors for the console */ gwinSetColor(GW, Yellow); gwinSetBgColor(GW, Black); gwinClear(GW); } /* Say Hello */ gwinPrintf(GW, "Keyboard Monitor...\n"); while(1) { // Get an Event pk = (GEventKeyboard *)geventEventWait(&gl, TIME_INFINITE); if (pk->type != GEVENT_KEYBOARD) continue; gwinPrintf(GW, "KEYSTATE: 0x%04X [ %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s]", pk->keystate, (!pk->keystate ? "NONE " : ""), ((pk->keystate & GKEYSTATE_KEYUP) ? "KEYUP " : ""), ((pk->keystate & GKEYSTATE_REPEAT) ? "REPEAT " : ""), ((pk->keystate & GKEYSTATE_SPECIAL) ? "SPECIAL " : ""), ((pk->keystate & GKEYSTATE_RAW) ? "RAW " : ""), ((pk->keystate & GKEYSTATE_SHIFT_L) ? "LSHIFT " : ""), ((pk->keystate & GKEYSTATE_SHIFT_R) ? "RSHIFT " : ""), ((pk->keystate & GKEYSTATE_CTRL_L) ? "LCTRL " : ""), ((pk->keystate & GKEYSTATE_CTRL_R) ? "RCTRL " : ""), ((pk->keystate & GKEYSTATE_ALT_L) ? "LALT " : ""), ((pk->keystate & GKEYSTATE_ALT_R) ? "RALT " : ""), ((pk->keystate & GKEYSTATE_FN) ? "FN " : ""), ((pk->keystate & GKEYSTATE_COMPOSE) ? "COMPOSE " : ""), ((pk->keystate & GKEYSTATE_WINKEY) ? "WINKEY " : ""), ((pk->keystate & GKEYSTATE_CAPSLOCK) ? "CAPSLOCK " : ""), ((pk->keystate & GKEYSTATE_NUMLOCK) ? "NUMLOCK " : ""), ((pk->keystate & GKEYSTATE_SCROLLLOCK) ? "SCROLLLOCK " : "") ); if (pk->bytecount) { gwinPrintf(GW, " Keys:"); for (i = 0; i < pk->bytecount; i++) gwinPrintf(GW, " 0x%02X", (uint8_t)pk->c[i]); gwinPrintf(GW, " ["); for (i = 0; i < pk->bytecount; i++) gwinPrintf(GW, "%c", pk->c[i] >= ' ' && pk->c[i] <= '~' ? pk->c[i] : ' '); gwinPrintf(GW, "]"); } gwinPrintf(GW, "\n"); } }
static uint32_t CalibrateMouse(GMouse *m) { coord_t w, h; point cross[4]; // The locations of the test points on the display point points[4]; // The x, y readings obtained from the mouse for each test point uint32_t err; #if GDISP_NEED_TEXT font_t font1, font2; #endif #if GDISP_NEED_TEXT font1 = gdispOpenFont(CALIBRATION_FONT); if (!font1) font1 = gdispOpenFont("*"); font2 = gdispOpenFont(CALIBRATION_FONT2); if (!font2) font2 = gdispOpenFont("*"); #endif err = 0; w = gdispGGetWidth(m->display); h = gdispGGetHeight(m->display); #if GDISP_NEED_CLIP gdispGSetClip(m->display, 0, 0, w, h); #endif // Ensure we get minimaly processed readings for the calibration m->flags |= GMOUSE_FLG_IN_CAL; // Set up our calibration locations if ((gmvmt(m)->d.flags & GMOUSE_VFLG_CAL_EXTREMES)) { cross[0].x = 0; cross[0].y = 0; cross[1].x = w-1; cross[1].y = 0; cross[2].x = w-1; cross[2].y = h-1; cross[3].x = w/2; cross[3].y = h/2; } else { cross[0].x = w/4; cross[0].y = h/4; cross[1].x = w-w/4; cross[1].y = h/4; cross[2].x = w-w/4; cross[2].y = h-h/4; cross[3].x = w/2; cross[3].y = h/2; } // Set up the calibration display gdispGClear(m->display, Blue); #if GDISP_NEED_TEXT gdispGFillStringBox(m->display, 0, CALIBRATION_TITLE_Y, w, CALIBRATION_TITLE_HEIGHT, CALIBRATION_TITLE, font1, CALIBRATION_TITLE_COLOR, CALIBRATION_TITLE_BACKGROUND, justifyCenter); #endif // Calculate the calibration { unsigned i, maxpoints; maxpoints = (gmvmt(m)->d.flags & GMOUSE_VFLG_CAL_TEST) ? 4 : 3; // Loop through the calibration points for(i = 0; i < maxpoints; i++) { int32_t px, py; unsigned j; // Draw the current calibration point CalibrationCrossDraw(m, &cross[i]); // Get a valid "point pressed" average reading do { // Wait for the mouse to be pressed while(!(m->r.buttons & GINPUT_MOUSE_BTN_LEFT)) gfxSleepMilliseconds(CALIBRATION_POLL_PERIOD); // Sum samples taken every CALIBRATION_POLL_PERIOD milliseconds while the mouse is down px = py = j = 0; while((m->r.buttons & GINPUT_MOUSE_BTN_LEFT)) { // Limit sampling period to prevent overflow if (j < CALIBRATION_MAXPRESS_PERIOD/CALIBRATION_POLL_PERIOD) { px += m->r.x; py += m->r.y; j++; } gfxSleepMilliseconds(CALIBRATION_POLL_PERIOD); } // Ignore presses less than CALIBRATION_MAXPRESS_PERIOD milliseconds } while(j < CALIBRATION_MINPRESS_PERIOD/CALIBRATION_POLL_PERIOD); points[i].x = px / j; points[i].y = py / j; // Clear the current calibration point CalibrationCrossClear(m, &cross[i]); } } // Apply 3 point calibration algorithm CalibrationCalculate(m, cross, points); /* Verification of correctness of calibration (optional) : * See if the 4th point (Middle of the screen) coincides with the calibrated * result. If point is within +/- Squareroot(ERROR) pixel margin, then successful calibration * Else return the error. */ if ((gmvmt(m)->d.flags & GMOUSE_VFLG_CAL_TEST)) { const GMouseJitter *pj; // Are we in pen or finger mode pj = (m->flags & GMOUSE_FLG_FINGERMODE) ? &gmvmt(m)->finger_jitter : &gmvmt(m)->pen_jitter; // Transform the co-ordinates CalibrationTransform((GMouseReading *)&points[3], &m->caldata); // Do we need to rotate the reading to match the display #if GDISP_NEED_CONTROL if (!(gmvmt(m)->d.flags & GMOUSE_VFLG_SELFROTATION)) { coord_t t; switch(gdispGGetOrientation(m->display)) { case GDISP_ROTATE_0: break; case GDISP_ROTATE_90: t = points[3].x; points[3].x = w - 1 - points[3].y; points[3].y = t; break; case GDISP_ROTATE_180: points[3].x = w - 1 - points[3].x; points[3].y = h - 1 - points[3].y; break; case GDISP_ROTATE_270: t = points[3].y; points[3].y = h - 1 - points[3].x; points[3].x = t; break; default: break; } } #endif // Is this accurate enough? err = (points[3].x - cross[3].x) * (points[3].x - cross[3].x) + (points[3].y - cross[3].y) * (points[3].y - cross[3].y); if (err > (uint32_t)pj->calibrate * (uint32_t)pj->calibrate) { #if GDISP_NEED_TEXT // No - Display error and return gdispGFillStringBox(m->display, 0, CALIBRATION_ERROR_Y, w, CALIBRATION_ERROR_HEIGHT, CALIBRATION_ERROR_TEXT, font2, CALIBRATION_ERROR_COLOR, CALIBRATION_ERROR_BACKGROUND, justifyCenter); gfxSleepMilliseconds(CALIBRATION_ERROR_DELAY); #endif } else err = 0; } // We are done calibrating #if GDISP_NEED_TEXT gdispCloseFont(font1); gdispCloseFont(font2); #endif m->flags &= ~GMOUSE_FLG_IN_CAL; m->flags |= GMOUSE_FLG_CLIP; // Save the calibration data (if possible) if (!err) { m->flags |= GMOUSE_FLG_CALIBRATE; #if GINPUT_TOUCH_USER_CALIBRATION_SAVE SaveMouseCalibration(gdriverGetDriverInstanceNumber((GDriver *)m), &m->caldata, sizeof(GMouseCalibration)); #endif if (gmvmt(m)->calsave) gmvmt(m)->calsave(m, &m->caldata, sizeof(GMouseCalibration)); } // Force an initial reading m->r.buttons = 0; GetMouseReading(m); // Clear the screen using the GWIN default background color #if GFX_USE_GWIN gdispGClear(m->display, gwinGetDefaultBgColor()); #else gdispGClear(m->display, GDISP_STARTUP_COLOR); #endif return err; }
int main(void) { GEvent * pe; // Initialize the display gfxInit(); // Set the widget defaults font = gdispOpenFont("*"); // Get the first defined font. gwinSetDefaultFont(font); gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE); gdispClear(White); // Create the gwin windows/widgets createWidgets(); // Assign toggles and dials to specific buttons & sliders etc. #if GINPUT_NEED_TOGGLE gwinAttachToggle(ghButton1, 0, 0); gwinAttachToggle(ghButton2, 0, 1); #endif #if GINPUT_NEED_DIAL gwinAttachDial(ghSlider1, 0, 0); gwinAttachDial(ghSlider3, 0, 1); #endif // Make the console visible gwinShow(ghConsole); gwinClear(ghConsole); // We want to listen for widget events geventListenerInit(&gl); gwinAttachListener(&gl); gtimerInit(&FlashTimer); #if !GWIN_NEED_TABSET // Press the Tab we want visible gwinRadioPress(ghTabButtons); #endif while(1) { // Get an Event pe = geventEventWait(&gl, TIME_INFINITE); switch(pe->type) { case GEVENT_GWIN_BUTTON: gwinPrintf(ghConsole, "Button %s\n", gwinGetText(((GEventGWinButton *)pe)->gwin)); break; case GEVENT_GWIN_SLIDER: gwinPrintf(ghConsole, "Slider %s=%d\n", gwinGetText(((GEventGWinSlider *)pe)->gwin), ((GEventGWinSlider *)pe)->position); break; case GEVENT_GWIN_CHECKBOX: gwinPrintf(ghConsole, "Checkbox %s=%s\n", gwinGetText(((GEventGWinCheckbox *)pe)->gwin), ((GEventGWinCheckbox *)pe)->isChecked ? "Checked" : "UnChecked"); // If it is the Disable All checkbox then do that. if (((GEventGWinCheckbox *)pe)->gwin == ghCheckDisableAll) { gwinPrintf(ghConsole, "%s All\n", ((GEventGWinCheckbox *)pe)->isChecked ? "Disable" : "Enable"); setEnabled(!((GEventGWinCheckbox *)pe)->isChecked); // If it is the toggle button checkbox start the flash. } else if (((GEventGWinCheckbox *)pe)->gwin == ghCheckbox3) { gwinFlash(ghCheckbox3); gtimerStart(&FlashTimer, FlashOffFn, 0, FALSE, 3000); } break; case GEVENT_GWIN_LIST: gwinPrintf(ghConsole, "List %s Item %d %s\n", gwinGetText(((GEventGWinList *)pe)->gwin), ((GEventGWinList *)pe)->item, gwinListItemIsSelected(((GEventGWinList *)pe)->gwin, ((GEventGWinList *)pe)->item) ? "Selected" : "Unselected"); break; case GEVENT_GWIN_RADIO: gwinPrintf(ghConsole, "Radio Group %u=%s\n", ((GEventGWinRadio *)pe)->group, gwinGetText(((GEventGWinRadio *)pe)->gwin)); switch(((GEventGWinRadio *)pe)->group) { #if !GWIN_NEED_TABSET case GROUP_TABS: // Set control visibility depending on the tab selected setTab(((GEventGWinRadio *)pe)->gwin); // We show the state of some of the GUI elements here setProgressbar(((GEventGWinRadio *)pe)->gwin == ghTabProgressbar); if (((GEventGWinRadio *)pe)->gwin == ghTabLabels) setLabels(); break; #endif case GROUP_COLORS: { const GWidgetStyle *pstyle; gwinPrintf(ghConsole, "Change Color Scheme\n"); if (((GEventGWinRadio *)pe)->gwin == ghRadioYellow) pstyle = &YellowWidgetStyle; else if (((GEventGWinRadio *)pe)->gwin == ghRadioBlack) pstyle = &BlackWidgetStyle; else pstyle = &WhiteWidgetStyle; // Clear the screen to the new color gdispClear(pstyle->background); // Update the style on all controls gwinSetDefaultStyle(pstyle, TRUE); } break; } break; #if GWIN_NEED_TABSET case GEVENT_GWIN_TABSET: gwinPrintf(ghConsole, "TabPage %u (%s)\n", ((GEventGWinTabset *)pe)->nPage, gwinTabsetGetTitle(((GEventGWinTabset *)pe)->ghPage)); // We show the state of some of the GUI elements here setProgressbar(((GEventGWinTabset *)pe)->ghPage == ghPgProgressbars); if (((GEventGWinTabset *)pe)->ghPage == ghPgLabels) setLabels(); break; #endif default: gwinPrintf(ghConsole, "Unknown %d\n", pe->type); break; } } return 0; }
/*------------------------------------------------------------------------* * GINPUT Touch Driver Calibrator. * *------------------------------------------------------------------------*/ int main(void) { GSourceHandle gs, gsNext, gsPrev; GEvent *pe; GEventMouse *pem; GEventGWinButton *peb; coord_t swidth, sheight; GHandle ghc, ghNext, ghPrev; BaseSequentialStream *gp; GEventType deviceType; font_t font; halInit(); // Initialise the Hardware chSysInit(); // Initialize the OS gdispInit(); // Initialize the display // Get the display dimensions swidth = gdispGetWidth(); sheight = gdispGetHeight(); ghNext = ghPrev = 0; // Create our title font = gdispOpenFont("UI2"); gdispFillStringBox(0, 0, swidth, 20, "Touch Calibration", font, Red, White, justifyLeft); // Create our main display window ghc = gwinCreateConsole(&gc, 0, 20, swidth, sheight-20, font); gwinClear(ghc); gp = gwinGetConsoleStream(ghc); // Initialize the mouse in our special no calibration mode. geventListenerInit(&gl); gs = ginputGetMouse(9999); geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Device Type */ StepDeviceType: gwinClear(ghc); gwinSetColor(ghc, Yellow); chprintf(gp, "\n1. DEVICE TYPE\n\n"); pem = (GEventMouse *)&gl.event; ginputGetMouseStatus(0, pem); deviceType = pem->type; gwinSetColor(ghc, White); chprintf(gp, "This is detected as a %s device\n\n", deviceType == GEVENT_MOUSE ? "MOUSE" : (pem->type == GEVENT_TOUCH ? "TOUCH" : "UNKNOWN")); if (ghNext) chprintf(gp, "Press Next or Back to continue.\n"); else if (deviceType == GEVENT_MOUSE) chprintf(gp, "Click the mouse button to move on to the next test.\n"); else chprintf(gp, "Press and release your finger to move on to the next test.\n"); while(1) { pe = geventEventWait(&gl, TIME_INFINITE); if (pe->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pe; if (peb->button == ghPrev) goto StepClickJitter; if (peb->button == ghNext) break; } if (pe->type == GEVENT_MOUSE || pe->type == GEVENT_TOUCH) { pem = (GEventMouse *)pe; if (!ghNext && (pem->meta & GMETA_MOUSE_UP)) break; } } /* * Test: Mouse raw reading jitter */ StepRawJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); chprintf(gp, "\n2. GINPUT_MOUSE_READ_CYCLES\n\n"); gwinSetColor(ghc, White); if (deviceType == GEVENT_MOUSE) chprintf(gp, "Press and hold the mouse button.\n\n"); else chprintf(gp, "Press and hold on the surface.\n\n"); chprintf(gp, "Numbers will display in this window.\n" "Ensure that values don't jump around very much when your finger is stationary.\n\n" "Increasing GINPUT_MOUSE_READ_CYCLES helps reduce jitter but increases CPU usage.\n\n"); if (ghNext) chprintf(gp, "Press Next or Back to continue.\n"); else if (deviceType == GEVENT_MOUSE) chprintf(gp, "Release the mouse button to move on to the next test.\n"); else chprintf(gp, "Release your finger to move on to the next test.\n"); // For this test turn on ALL mouse movement events geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA|GLISTEN_MOUSENOFILTER); while(1) { pe = geventEventWait(&gl, TIME_INFINITE); if (pe->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pe; if (peb->button == ghPrev) goto StepDeviceType; if (peb->button == ghNext) break; } if (pe->type == GEVENT_MOUSE || pe->type == GEVENT_TOUCH) { pem = (GEventMouse *)pe; if ((pem->current_buttons & GINPUT_MOUSE_BTN_LEFT)) chprintf(gp, "%u:%u\n", pem->x, pem->y); if (!ghNext && (pem->meta & GMETA_MOUSE_UP)) break; } } // Reset to just changed movements. geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Calibration */ StepCalibrate: gwinClear(ghc); gwinSetColor(ghc, Yellow); chprintf(gp, "\n3. GINPUT_MOUSE_CALIBRATION_ERROR\n\n"); gwinSetColor(ghc, Gray); chprintf(gp, "Ensure GINPUT_MOUSE_NEED_CALIBRATION = TRUE and GINPUT_MOUSE_CALIBRATION_ERROR is >= 0\n\n"); gwinSetColor(ghc, White); chprintf(gp, "You will be presented with a number of points to touch.\nPress them in turn.\n\n" "If the calibration repeatedly fails, increase GINPUT_MOUSE_CALIBRATION_ERROR and try again.\n\n"); if (ghNext) chprintf(gp, "Press Next to start the calibration.\n"); else if (deviceType == GEVENT_MOUSE) chprintf(gp, "Click the mouse button to start the calibration.\n"); else chprintf(gp, "Press and release your finger to start the calibration.\n"); while(1) { pe = geventEventWait(&gl, TIME_INFINITE); if (pe->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pe; if (peb->button == ghPrev) goto StepRawJitter; if (peb->button == ghNext) break; } if (pe->type == GEVENT_MOUSE || pe->type == GEVENT_TOUCH) { pem = (GEventMouse *)pe; if (!ghNext && (pem->meta & GMETA_MOUSE_UP)) break; } } // Calibrate ginputCalibrateMouse(0); /* From now on we can use Next and Previous Buttons */ if (!ghNext) { ghNext = gwinCreateButton(&gNext, swidth-50, 0, 50, 20, font, GBTN_NORMAL); gwinSetButtonText(ghNext, "Next", FALSE); gsNext = gwinGetButtonSource(ghNext); geventAttachSource(&gl, gsNext, 0); gwinAttachButtonMouseSource(ghNext, gs); ghPrev = gwinCreateButton(&gPrev, swidth-100, 0, 50, 20, font, GBTN_NORMAL); gwinSetButtonText(ghPrev, "Back", FALSE); gsPrev = gwinGetButtonSource(ghPrev); geventAttachSource(&gl, gsPrev, 0); gwinAttachButtonMouseSource(ghPrev, gs); #if 0 { GSourceHandle gsButton1, gsButton2; // Attach a couple of hardware toggle buttons to our Next and Back buttons as well. // We can always use the mouse to trigger the buttons if you don't want to use hardware toggles. // This code depends on your hardware. Turn it on only if you have // defined a board definition for your toggle driver. Then change // the next two lines to be correct for your hardware. The values // below are correct for the Win32 toggle driver. gsButton1 = ginputGetToggle(GINPUT_TOGGLE_MOMENTARY1); gsButton2 = ginputGetToggle(GINPUT_TOGGLE_MOMENTARY2); gwinAttachButtonToggleSource(ghNext, gsButton2); gwinAttachButtonToggleSource(ghPrev, gsButton1); } #endif } // Calibration used the whole screen - re-establish our title gdispFillStringBox(0, 0, swidth, 20, "Touch Calibration", font, Green, White, justifyLeft); gwinButtonDraw(ghNext); gwinButtonDraw(ghPrev); /* * Test: Mouse movement jitter */ StepJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); chprintf(gp, "\n4. GINPUT_MOUSE_MOVE_JITTER\n\n"); gwinSetColor(ghc, White); if (deviceType == GEVENT_MOUSE) chprintf(gp, "Press and hold the mouse button and move around as if to draw.\n\n"); else chprintf(gp, "Press firmly on the surface and move around as if to draw.\n\n"); chprintf(gp, "Dots will display in this window. Ensure that when you stop moving your finger that " "new dots stop displaying.\nNew dots should only display when your finger is moving.\n\n" "Adjust GINPUT_MOUSE_MOVE_JITTER to the smallest value that this reliably works for.\n\n"); chprintf(gp, "Press Next or Back to continue.\n\n"); while(1) { pe = geventEventWait(&gl, TIME_INFINITE); if (pe->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pe; if (peb->button == ghPrev) goto StepCalibrate; if (peb->button == ghNext) break; } if (pe->type == GEVENT_MOUSE || pe->type == GEVENT_TOUCH) { pem = (GEventMouse *)pe; if ((pem->current_buttons & GINPUT_MOUSE_BTN_LEFT)) chprintf(gp, "."); } } /* * Test: Polling frequency */ StepPolling: gwinClear(ghc); gwinSetColor(ghc, Yellow); chprintf(gp, "\n5. GINPUT_MOUSE_POLL_PERIOD\n\n"); gwinSetColor(ghc, White); chprintf(gp, "Press firmly on the surface (or press and hold the mouse button) and move around as if to draw.\n\n"); chprintf(gp, "A green line will follow your finger.\n" "Adjust GINPUT_MOUSE_POLL_PERIOD to the highest value that provides a line without " "gaps that are too big.\nDecreasing the value increases CPU usage.\n" "About 25 (millisecs) normally produces good results." "This test can be ignored for interrupt driven drivers.\n\n"); chprintf(gp, "Press Next or Back to continue.\n\n"); while(1) { pe = geventEventWait(&gl, TIME_INFINITE); if (pe->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pe; if (peb->button == ghPrev) goto StepJitter; if (peb->button == ghNext) break; } if (pe->type == GEVENT_MOUSE || pe->type == GEVENT_TOUCH) { pem = (GEventMouse *)pe; if ((pem->current_buttons & GINPUT_MOUSE_BTN_LEFT)) gdispDrawPixel(pem->x, pem->y, Green); } } /* * Test: Click Jitter */ StepClickJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); chprintf(gp, "\n6. GINPUT_MOUSE_MAX_CLICK_JITTER\n\n"); gwinSetColor(ghc, White); chprintf(gp, "Press and release the touch surface to \"click\".\nTry both short and long presses.\n"); chprintf(gp, "For a mouse click with the left and right buttons.\n\n"); chprintf(gp, "Dots will display in this window. A yellow dash is a left (or short) click. " "A red x is a right (or long) click.\n\n" "Adjust GINPUT_MOUSE_CLICK_JITTER to the smallest value that this reliably works for.\n" "Adjust GINPUT_MOUSE_CLICK_TIME to adjust distinguishing short vs long presses.\n" "TIME_INFINITE means there are no long presses (although a right mouse button will still work).\n\n" "Note: moving your finger (mouse) during a click cancels it.\n\n"); chprintf(gp, "This is the last test but you can press Next or Back to continue.\n\n"); while(1) { pe = geventEventWait(&gl, TIME_INFINITE); if (pe->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pe; if (peb->button == ghPrev) goto StepPolling; if (peb->button == ghNext) break; } if (pe->type == GEVENT_MOUSE || pe->type == GEVENT_TOUCH) { pem = (GEventMouse *)pe; if ((pem->meta & GMETA_MOUSE_CLICK)) { gwinSetColor(ghc, Yellow); chprintf(gp, "-"); } if ((pem->meta & GMETA_MOUSE_CXTCLICK)) { gwinSetColor(ghc, Red); chprintf(gp, "x"); } } } // Can't let this really exit goto StepDeviceType; }
/*------------------------------------------------------------------------* * GINPUT Touch Driver Calibrator. * *------------------------------------------------------------------------*/ int main(void) { GSourceHandle gs; GEventMouse *pem; bool_t isFirstTime; bool_t isCalibrated; bool_t isTouch; bool_t isFinger; const char * isFingerText; const char * deviceText; GMouse * m; GMouseVMT * vmt; GMouseJitter * pjit; uint32_t calerr; gfxInit(); // Initialize the display // Get the display dimensions swidth = gdispGetWidth(); sheight = gdispGetHeight(); // Create our title font = gdispOpenFont("UI2"); gwinSetDefaultFont(font); bWidth = gdispGetStringWidth("Next", font); bHeight = gdispGetStringWidth("Prev", font); if (bHeight > bWidth) bWidth = bHeight; bWidth += 4; bWidth2 = gdispGetStringWidth("+", font)*2; bHeight = gdispGetStringWidth("-", font)*2; if (bHeight > bWidth2) bWidth2 = bHeight; bWidth2 += 4; bHeight = gdispGetFontMetric(font, fontHeight)*2+2; // Create our main display window { GWindowInit wi; gwinClearInit(&wi); wi.show = TRUE; wi.x = 0; wi.y = bHeight; wi.width = swidth; wi.height = sheight-bHeight; ghc = gwinConsoleCreate(&gc, &wi); } // Initialize the listener geventListenerInit(&gl); // Copy the current mouse's VMT so we can play with it. m = (GMouse *)gdriverGetInstance(GDRIVER_TYPE_MOUSE, 0); if (!m) gfxHalt("No mouse instance 0"); vmt = gfxAlloc(sizeof(GMouseVMT)); if (!vmt) gfxHalt("Could not allocate memory for mouse VMT"); memcpy(vmt, m->d.vmt, sizeof(GMouseVMT)); // Swap VMT's on the current mouse to our RAM copy m->d.vmt = (const GDriverVMT *)vmt; // Listen for events gs = ginputGetMouse(0); geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); // Get initial display settings for buttons isFirstTime = TRUE; isCalibrated = (vmt->d.flags & GMOUSE_VFLG_CALIBRATE) ? FALSE : TRUE; calerr = 0; /* * Test: Device Type */ StepDeviceType: DrawHeader("1. Device Type", isCalibrated, isCalibrated && !isFirstTime, isCalibrated); // Get the type of device and the current mode isTouch = (vmt->d.flags & GMOUSE_VFLG_TOUCH) ? TRUE : FALSE; isFinger = (m->flags & GMOUSE_FLG_FINGERMODE) ? TRUE : FALSE; pjit = isFinger ? &vmt->finger_jitter : &vmt->pen_jitter; isFingerText = isFinger ? "finger" : "pen"; deviceText = isTouch ? isFingerText : "mouse"; gwinPrintf(ghc, "This is detected as a %s device\n\n", isTouch ? "TOUCH" : "MOUSE"); gwinPrintf(ghc, "It is currently in %s mode\n\n", isFinger ? "FINGER" : "PEN"); if (!isCalibrated) gwinPrintf(ghc, "Press and release your %s to move on to the next test.\n", deviceText); else { gwinPrintf(ghc, "Press + for pen or - for finger.\n"); if (isFirstTime) gwinPrintf(ghc, "Press Next to continue.\n"); else gwinPrintf(ghc, "Press Next or Back to continue.\n"); } while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (isCalibrated) { switch (CheckButtons(pem)) { case BTN_NEXT: break; case BTN_PREV: if (!isFirstTime) goto StepDrawing; continue; case BTN_PLUS: m->flags &= ~GMOUSE_FLG_FINGERMODE; goto StepDeviceType; case BTN_MINUS: m->flags |= GMOUSE_FLG_FINGERMODE; goto StepDeviceType; default: continue; } break; } if ((pem->buttons & GMETA_MOUSE_UP)) break; } /* * Test: Mouse raw reading */ StepRawReading: DrawHeader("2. Raw Mouse Output", FALSE, FALSE, FALSE); if (isTouch) gwinPrintf(ghc, "Press and hold on the surface.\n\n"); else gwinPrintf(ghc, "Press and hold the mouse button.\n\n"); gwinPrintf(ghc, "The raw values coming from your mouse driver will display.\n\n"); gwinPrintf(ghc, "Make sure the x and y values change as you move.\n\n"); gwinPrintf(ghc, "Release your %s to move on to the next test.\n", deviceText); // Make sure we are in uncalibrated mode m->flags &= ~(GMOUSE_FLG_CALIBRATE|GMOUSE_FLG_CLIP); // For this test turn on ALL mouse movement events geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEUPMOVES|GLISTEN_MOUSEMETA|GLISTEN_MOUSENOFILTER); while(1) { // Always sleep a bit first to enable other events. We actually don't // mind missing events for this test. gfxSleepMilliseconds(100); pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); gwinPrintf(ghc, "%u, %u z=%u b=0x%04x\n", pem->x, pem->y, pem->z, pem->buttons & ~GINPUT_MISSED_MOUSE_EVENT); if ((pem->buttons & GMETA_MOUSE_UP)) break; } // Reset to calibrated condition if (isCalibrated) { m->flags |= GMOUSE_FLG_CLIP; if ((vmt->d.flags & GMOUSE_VFLG_CALIBRATE)) m->flags |= GMOUSE_FLG_CALIBRATE; } // Reset to just changed movements. geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Calibration */ StepCalibrate: DrawHeader("3. Calibration Jitter", isCalibrated, isCalibrated, isCalibrated); if ((vmt->d.flags & GMOUSE_VFLG_CALIBRATE)) { gwinPrintf(ghc, "You will be presented with a number of points to touch.\nPress them in turn.\n\n" "If the calibration repeatedly fails, increase the jitter for %s calibration and try again.\n\n", isFingerText); gwinPrintf(ghc, "Pressing the surface for longer gives more accurate results.\n\n"); if (calerr) gwinPrintf(ghc, "Last calibration error ^ 2 = %u\n", calerr); gwinPrintf(ghc, "Calibration jitter (%s) = %u\n", isFingerText, pjit->calibrate); if (isCalibrated) gwinPrintf(ghc, "Press + or - to adjust.\n"); } else { gwinPrintf(ghc, "This device does not need calibration.\n\n"); } if (isCalibrated) gwinPrintf(ghc, "Press Next or Back to continue.\n"); else gwinPrintf(ghc, "Press and release your %s to move on to start calibration.\n", deviceText); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (isCalibrated) { switch (CheckButtons(pem)) { case BTN_NEXT: break; case BTN_PREV: goto StepRawReading; case BTN_PLUS: gwinPrintf(ghc, "Calibration jitter (%s) = %u", isFingerText, ++pjit->calibrate); continue; case BTN_MINUS: gwinPrintf(ghc, "Calibration jitter (%s) = %u", isFingerText, --pjit->calibrate); continue; default: continue; } break; } if ((pem->buttons & GMETA_MOUSE_UP)) break; } // Calibrate if ((vmt->d.flags & GMOUSE_VFLG_CALIBRATE)) { calerr = ginputCalibrateMouse(0); if (calerr) goto StepCalibrate; isCalibrated = TRUE; } /* * Test: Mouse coords */ StepMouseCoords: DrawHeader("4. Show Mouse Coordinates", TRUE, TRUE, TRUE); if (isTouch) gwinPrintf(ghc, "Press and hold on the surface.\n\n"); else gwinPrintf(ghc, "Press and hold the mouse button.\n\n"); gwinPrintf(ghc, "Check the coordinates against where it should be on the screen.\n\n"); gwinPrintf(ghc, "X should be 0 to %u\nY should be 0 to %u\n\n", swidth-1, sheight-1); gwinPrintf(ghc, "Press + to retry using extremes or - for normal calibration.\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n"); // For this test normal mouse movement events geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); while(1) { // Always sleep a bit first to enable other events. We actually don't // mind missing events for this test. gfxSleepMilliseconds(100); pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); switch (CheckButtons(pem)) { case BTN_NEXT: break; case BTN_PREV: goto StepCalibrate; case BTN_PLUS: vmt->d.flags |= GMOUSE_VFLG_CAL_EXTREMES; goto StepCalibrate; case BTN_MINUS: vmt->d.flags &= ~GMOUSE_VFLG_CAL_EXTREMES; goto StepCalibrate; default: gwinPrintf(ghc, "%u, %u\n", pem->x, pem->y); continue; } break; } // Reset to just changed movements. geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Mouse movement jitter */ StepMovementJitter: DrawHeader("5. Movement Jitter", TRUE, TRUE, TRUE); if (isTouch) gwinPrintf(ghc, "Press firmly on the surface and move around as if to draw.\n\n"); else gwinPrintf(ghc, "Press and hold the mouse button and move around as if to draw.\n\n"); gwinPrintf(ghc, "Dots will display in this window. Ensure that when you stop moving your %s that " "new dots stop displaying.\nNew dots should only display when your %s is moving.\n\n" "Adjust %s movement jitter to the smallest value that this reliably works for.\n\n", deviceText, deviceText, isFingerText); gwinPrintf(ghc, "Movement jitter (%s) = %u\n", isFingerText, pjit->move); gwinPrintf(ghc, "Press + or - to adjust.\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); switch (CheckButtons(pem)) { case BTN_NEXT: break; case BTN_PREV: goto StepMouseCoords; case BTN_PLUS: gwinPrintf(ghc, "Movement jitter (%s) = %u", isFingerText, ++pjit->move); continue; case BTN_MINUS: gwinPrintf(ghc, "Movement jitter (%s) = %u", isFingerText, --pjit->move); continue; default: if ((pem->buttons & GINPUT_MOUSE_BTN_LEFT)) gwinPrintf(ghc, "."); continue; } break; } /* * Test: Click Jitter */ StepClickJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n6. Click Jitter\n\n"); gwinSetColor(ghc, White); if (isTouch) gwinPrintf(ghc, "Press and release the touch surface to \"click\".\nTry both short and long presses.\n"); else gwinPrintf(ghc, "Click the mouse with the left and right buttons.\n\n"); gwinPrintf(ghc, "A yellow dash is a left (or short) click.\n" "A red x is a right (or long) click.\n\n" "Adjust %s click jitter to the smallest value that this reliably works for.\n" "Note: moving your %s during a click cancels it.\n\n", isFingerText, deviceText); gwinPrintf(ghc, "Click jitter (%s) = %u\n", isFingerText, pjit->click); gwinPrintf(ghc, "Press + or - to adjust.\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); switch (CheckButtons(pem)) { case BTN_NEXT: break; case BTN_PREV: goto StepMovementJitter; case BTN_PLUS: gwinPrintf(ghc, "Click jitter (%s) = %u", isFingerText, ++pjit->click); continue; case BTN_MINUS: gwinPrintf(ghc, "Click jitter (%s) = %u", isFingerText, --pjit->click); continue; default: if ((pem->buttons & GMETA_MOUSE_CLICK)) { gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "-"); } if ((pem->buttons & GMETA_MOUSE_CXTCLICK)) { gwinSetColor(ghc, uRed); gwinPrintf(ghc, "x"); } continue; } break; } /* * Test: Polling frequency */ StepDrawing: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n7. Drawing\n\n"); gwinSetColor(ghc, White); gwinPrintf(ghc, "Press firmly on the surface (or press and hold the mouse button) and move around as if to draw.\n\n"); gwinPrintf(ghc, "A green line will follow your %s.\n\n", deviceText); gwinPrintf(ghc, "Pressing Next will start the tests again but with the option of changing pen/finger mode.\n\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->buttons & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepClickJitter; } } gdispDrawPixel(pem->x, pem->y, uGreen); } // Can't let this really exit isFirstTime = FALSE; goto StepDeviceType; }
/*------------------------------------------------------------------------* * GINPUT Touch Driver Calibrator. * *------------------------------------------------------------------------*/ int main(void) { GSourceHandle gs; GEventMouse *pem; GMouse * m; GMouseVMT * vmt; gfxInit(); // Initialize the display // Get the display dimensions swidth = gdispGetWidth(); sheight = gdispGetHeight(); // Create our title font = gdispOpenFont("UI2"); gwinSetDefaultFont(font); bHeight = gdispGetFontMetric(font, fontHeight)+4; gdispFillStringBox(0, 0, swidth, bHeight, "Raw Touch Readings", font, Red, White, justifyCenter); // Create our main display writing window { GWindowInit wi; gwinClearInit(&wi); wi.show = TRUE; wi.x = 0; wi.y = bHeight; wi.width = swidth; wi.height = sheight-bHeight; ghc = gwinConsoleCreate(&gc, &wi); } // Initialize the listener geventListenerInit(&gl); // Copy the current mouse's VMT so we can play with it. m = (GMouse *)gdriverGetInstance(GDRIVER_TYPE_MOUSE, 0); if (!m) gfxHalt("No mouse instance 0"); vmt = gfxAlloc(sizeof(GMouseVMT)); if (!vmt) gfxHalt("Could not allocate memory for mouse VMT"); memcpy(vmt, m->d.vmt, sizeof(GMouseVMT)); // Swap VMT's on the current mouse to our RAM copy m->d.vmt = (const GDriverVMT *)vmt; // Listen for events gs = ginputGetMouse(0); geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); // Make sure we are in uncalibrated pen mode m->flags &= ~(GMOUSE_FLG_CALIBRATE|GMOUSE_FLG_CLIP|GMOUSE_FLG_FINGERMODE); // Pretend we are a mouse, turn off all touch processing, turn off move and click filtering vmt->d.flags &= ~(GMOUSE_VFLG_TOUCH | GMOUSE_VFLG_ONLY_DOWN | GMOUSE_VFLG_POORUPDOWN); vmt->pen_jitter.move = 0; vmt->pen_jitter.click = 0; // For this test turn on ALL mouse movement events geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEUPMOVES|GLISTEN_MOUSEMETA|GLISTEN_MOUSENOFILTER); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); gwinPrintf(ghc, "%u, %u z=%u b=0x%04x\n", pem->x, pem->y, pem->z, pem->buttons & ~GINPUT_MISSED_MOUSE_EVENT); // Always sleep a bit first to enable other events. We actually don't mind missing events. gfxSleepMilliseconds(100); } }
static msg_t notepadThread(void *param) { GEventMouse *pem; GEventGWinButton *peb; GHandle ghc; (void)param; /* Get the display dimensions */ swidth = gdispGetWidth(); sheight = gdispGetHeight(); font = gdispOpenFont("UI2"); /* Initialize the mouse */ geventListenerInit(&gl); ginputGetMouse(0); initButtons(); /* Configure the GIF decoder with the toolbar Icon images */ gdispImageSetMemoryReader(&toolbarImageFilmstrip, toolbarIcons); gdispImageOpen(&toolbarImageFilmstrip); /* Set clip to the entire screen */ gdispSetClip(0, 0, swidth, sheight); /* Clear the screen with the window background * Also, draw the title bars */ gdispClear(nCurColorScheme.winBgColor); gdispDrawBox(0, 0, swidth, sheight, nCurColorScheme.titleBarColor); gdispFillArea(0, 0, swidth, NPAD_TITLEBAR_HEIGHT, nCurColorScheme.titleBarColor); gdispDrawStringBox(NPAD_TITLETEXT_START_X, NPAD_TITLETEXT_START_Y, swidth, NPAD_TITLEBAR_HEIGHT, NPAD_TITLETEXT_STR, font, nCurColorScheme.titleTextColor, justifyLeft); /* Create the drawing window, draw its border */ gdispDrawBox(NPAD_DRAWING_AREA_START_X - 1, NPAD_DRAWING_AREA_START_Y - 1, NPAD_DRAWING_AREA_WIDTH + 2, NPAD_DRAWING_AREA_HEIGHT + 2, nCurColorScheme.drawingWinBorder); nDrawingArea = gwinCreateWindow(NULL, NPAD_DRAWING_AREA_START_X, NPAD_DRAWING_AREA_START_Y, NPAD_DRAWING_AREA_WIDTH, NPAD_DRAWING_AREA_HEIGHT); /* Create the bottom status bar console */ ghc = gwinCreateConsole(NULL, NPAD_STATUSBAR_START_X, NPAD_STATUSBAR_START_Y, NPAD_STATUSBAR_WIDTH, NPAD_STATUSBAR_HEIGHT, font); gdispImageDraw(&toolbarImageFilmstrip, NPAD_STATUSBAR_ICON_START_X, NPAD_STATUSBAR_ICON_START_Y, NPAD_ICON_WIDTH, NPAD_ICON_HEIGHT, NPAD_ICON_START(12), 0); gwinSetBgColor(ghc, nCurColorScheme.winBgColor); gwinSetColor(ghc, Black); gstatusConsole = gwinGetConsoleStream(ghc); /* draw the buttons */ gwinSetColor(nDrawingArea, Black); gwinSetBgColor(nDrawingArea, White); gwinClear(nDrawingArea); gwinClear(ghc); drawButtons(); drawVButtons(); chprintf(gstatusConsole, "Welcome to ChibiOS/GFX Notepad demo."); ncoreSpawnDrawThread(nDrawingArea, gstatusConsole); while(TRUE) { pem = (GEventMouse *) geventEventWait(&gl, TIME_INFINITE); /* button pressed... */ if (pem->type == GEVENT_GWIN_BUTTON) { peb = (GEventGWinButton *)pem; if (peb->button == H(btnNew)) { // Reset all the settings selColorIndex = 0; selPenWidth = 0; ncoreSetMode(NCORE_MODE_DRAW); gwinSetColor(nDrawingArea, Black); gwinSetBgColor(nDrawingArea, White); // Refresh the buttons drawButtons(); drawVButtons(); gwinClear(nDrawingArea); chprintf(gstatusConsole, "\nScreen Cleared."); } else if (peb->button == H(btnOpen)) { chprintf(gstatusConsole, "\nFile Open not implemented."); } else if (peb->button == H(btnSave)) { chprintf(gstatusConsole, "\nFile Save not implemented."); } else if (peb->button == H(btnPencil)) { ncoreSetMode(NCORE_MODE_DRAW); drawVButtons(); chprintf(gstatusConsole, "\nPencil Tool Selected."); } else if (peb->button == H(btnEraser)) { ncoreSetMode(NCORE_MODE_ERASE); drawVButtons(); chprintf(gstatusConsole, "\nEraser Tool Selected."); } else if (peb->button == H(btnFill)) { ncoreSetMode(NCORE_MODE_FILL); drawVButtons(); chprintf(gstatusConsole, "\nFill Tool Selected."); } else if (peb->button == H(btnClose)) { break; } } } gwinDestroyWindow(ghc); // No need to destroy the buttons as they are statically allocated gdispCloseFont(font); ncoreTerminateDrawThread(); gdispImageClose(&toolbarImageFilmstrip); return 0; }
/*------------------------------------------------------------------------* * GINPUT Touch Driver Calibrator. * *------------------------------------------------------------------------*/ int main(void) { GSourceHandle gs; GEventMouse *pem; coord_t swidth, sheight; GHandle ghc; GEventType deviceType; bool_t calibrated; coord_t bWidth, bHeight; gfxInit(); // Initialize the display // Get the display dimensions swidth = gdispGetWidth(); sheight = gdispGetHeight(); calibrated = FALSE; // Create our title font = gdispOpenFont("UI2"); gwinSetDefaultFont(font); bWidth = gdispGetStringWidth("Next", font); bHeight = gdispGetStringWidth("Prev", font); if (bHeight > bWidth) bWidth = bHeight; bWidth += 4; bHeight = gdispGetFontMetric(font, fontHeight)+2; gdispFillStringBox(0, 0, swidth, bHeight, "Touch Calibration", font, Red, White, justifyLeft); // Create our main display window { GWindowInit wi; gwinClearInit(&wi); wi.show = TRUE; wi.x = 0; wi.y = bHeight; wi.width = swidth; wi.height = sheight-bHeight; ghc = gwinConsoleCreate(&gc, &wi); } gwinClear(ghc); // Initialize the mouse in our special no calibration mode. geventListenerInit(&gl); gs = ginputGetMouse(9999); geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Device Type */ StepDeviceType: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n1. DEVICE TYPE\n\n"); pem = (GEventMouse *)&gl.event; ginputGetMouseStatus(0, pem); deviceType = pem->type; gwinSetColor(ghc, White); gwinPrintf(ghc, "This is detected as a %s device\n\n", deviceType == GEVENT_MOUSE ? "MOUSE" : (pem->type == GEVENT_TOUCH ? "TOUCH" : "UNKNOWN")); if (calibrated) gwinPrintf(ghc, "Press Next or Back to continue.\n"); else if (deviceType == GEVENT_MOUSE) gwinPrintf(ghc, "Click the mouse button to move on to the next test.\n"); else gwinPrintf(ghc, "Press and release your finger to move on to the next test.\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (calibrated) { if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepClickJitter; } } } else if ((pem->meta & GMETA_MOUSE_UP)) break; } /* * Test: Mouse raw reading jitter */ StepRawJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n2. GINPUT_MOUSE_READ_CYCLES\n\n"); gwinSetColor(ghc, White); if (deviceType == GEVENT_MOUSE) gwinPrintf(ghc, "Press and hold the mouse button.\n\n"); else gwinPrintf(ghc, "Press and hold on the surface.\n\n"); gwinPrintf(ghc, "Numbers will display in this window.\n" "Ensure that values don't jump around very much when your finger is stationary.\n\n" "Increasing GINPUT_MOUSE_READ_CYCLES helps reduce jitter but increases CPU usage.\n\n"); if (calibrated) gwinPrintf(ghc, "Press Next or Back to continue.\n"); else if (deviceType == GEVENT_MOUSE) gwinPrintf(ghc, "Release the mouse button to move on to the next test.\n"); else gwinPrintf(ghc, "Release your finger to move on to the next test.\n"); // For this test turn on ALL mouse movement events geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEUPMOVES|GLISTEN_MOUSEMETA|GLISTEN_MOUSENOFILTER); while(1) { // Always sleep a bit first to enable other events. We actually don't // mind missing events for this test. gfxSleepMilliseconds(100); pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (calibrated) { if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepDeviceType; } } } else if ((pem->meta & GMETA_MOUSE_UP)) break; gwinPrintf(ghc, "%u:%u z=%u b=0x%04x m=%04x\n", pem->x, pem->y, pem->z, pem->current_buttons, pem->meta); } // Reset to just changed movements. geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Calibration */ StepCalibrate: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n3. GINPUT_MOUSE_CALIBRATION_ERROR\n\n"); gwinSetColor(ghc, Gray); gwinPrintf(ghc, "Ensure GINPUT_MOUSE_NEED_CALIBRATION = TRUE and GINPUT_MOUSE_CALIBRATION_ERROR is >= 0\n\n"); gwinSetColor(ghc, White); gwinPrintf(ghc, "You will be presented with a number of points to touch.\nPress them in turn.\n\n" "If the calibration repeatedly fails, increase GINPUT_MOUSE_CALIBRATION_ERROR and try again.\n\n"); if (calibrated) gwinPrintf(ghc, "Press Next to start the calibration.\n"); else if (deviceType == GEVENT_MOUSE) gwinPrintf(ghc, "Click the mouse button to start the calibration.\n"); else gwinPrintf(ghc, "Press and release your finger to start the calibration.\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (calibrated) { if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepRawJitter; } } } else if ((pem->meta & GMETA_MOUSE_UP)) break; } // Calibrate ginputCalibrateMouse(0); calibrated = TRUE; // Calibration used the whole screen - re-establish our title and Next and Previous Buttons gdispFillStringBox(0, 0, swidth, bHeight, "Touch Calibration", font, Green, White, justifyLeft); gdispFillStringBox(swidth-2*bWidth, 0, bWidth-1, bHeight, "Prev", font, Black, Gray, justifyCenter); gdispFillStringBox(swidth-1*bWidth, 0, bWidth , bHeight, "Next", font, Black, Gray, justifyCenter); /* * Test: Mouse coords */ StepMouseCoords: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n4. Show Mouse Coordinates\n\n"); gwinSetColor(ghc, White); if (deviceType == GEVENT_MOUSE) gwinPrintf(ghc, "Press and hold the mouse button.\n\n"); else gwinPrintf(ghc, "Press and hold on the surface.\n\n"); gwinPrintf(ghc, "Numbers will display in this window.\n" "Check the coordinates against where it should be on the screen.\n\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n"); // For this test normal mouse movement events geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); while(1) { // Always sleep a bit first to enable other events. We actually don't // mind missing events for this test. gfxSleepMilliseconds(100); pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepCalibrate; } } if ((pem->current_buttons & GINPUT_MOUSE_BTN_LEFT)) gwinPrintf(ghc, "%u:%u z=%u\n", pem->x, pem->y, pem->z); } // Reset to just changed movements. geventAttachSource(&gl, gs, GLISTEN_MOUSEDOWNMOVES|GLISTEN_MOUSEMETA); /* * Test: Mouse movement jitter */ StepJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n4. GINPUT_MOUSE_MOVE_JITTER\n\n"); gwinSetColor(ghc, White); if (deviceType == GEVENT_MOUSE) gwinPrintf(ghc, "Press and hold the mouse button and move around as if to draw.\n\n"); else gwinPrintf(ghc, "Press firmly on the surface and move around as if to draw.\n\n"); gwinPrintf(ghc, "Dots will display in this window. Ensure that when you stop moving your finger that " "new dots stop displaying.\nNew dots should only display when your finger is moving.\n\n" "Adjust GINPUT_MOUSE_MOVE_JITTER to the smallest value that this reliably works for.\n\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepMouseCoords; } } if ((pem->current_buttons & GINPUT_MOUSE_BTN_LEFT)) gwinPrintf(ghc, "."); } /* * Test: Polling frequency */ StepPolling: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n5. GINPUT_MOUSE_POLL_PERIOD\n\n"); gwinSetColor(ghc, White); gwinPrintf(ghc, "Press firmly on the surface (or press and hold the mouse button) and move around as if to draw.\n\n"); gwinPrintf(ghc, "A green line will follow your finger.\n" "Adjust GINPUT_MOUSE_POLL_PERIOD to the highest value that provides a line without " "gaps that are too big.\nDecreasing the value increases CPU usage.\n" "About 25 (millisecs) normally produces good results." "This test can be ignored for interrupt driven drivers.\n\n"); gwinPrintf(ghc, "Press Next or Back to continue.\n\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepJitter; } } if ((pem->current_buttons & GINPUT_MOUSE_BTN_LEFT)) gdispDrawPixel(pem->x, pem->y, Green); } /* * Test: Click Jitter */ StepClickJitter: gwinClear(ghc); gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "\n6. GINPUT_MOUSE_MAX_CLICK_JITTER\n\n"); gwinSetColor(ghc, White); gwinPrintf(ghc, "Press and release the touch surface to \"click\".\nTry both short and long presses.\n"); gwinPrintf(ghc, "For a mouse click with the left and right buttons.\n\n"); gwinPrintf(ghc, "Dots will display in this window. A yellow dash is a left (or short) click. " "A red x is a right (or long) click.\n\n" "Adjust GINPUT_MOUSE_CLICK_JITTER to the smallest value that this reliably works for.\n" "Adjust GINPUT_MOUSE_CLICK_TIME to adjust distinguishing short vs long presses.\n" "TIME_INFINITE means there are no long presses (although a right mouse button will still work).\n\n" "Note: moving your finger (mouse) during a click cancels it.\n\n"); gwinPrintf(ghc, "This is the last test but you can press Next or Back to continue.\n\n"); while(1) { pem = (GEventMouse *)geventEventWait(&gl, TIME_INFINITE); if (pem->y < bHeight && pem->x >= swidth-2*bWidth) { if ((pem->meta & GMETA_MOUSE_UP)) { if (pem->x >= swidth-bWidth) break; goto StepPolling; } } if ((pem->meta & GMETA_MOUSE_CLICK)) { gwinSetColor(ghc, Yellow); gwinPrintf(ghc, "-"); } if ((pem->meta & GMETA_MOUSE_CXTCLICK)) { gwinSetColor(ghc, Red); gwinPrintf(ghc, "x"); } } // Can't let this really exit goto StepDeviceType; }
int main(void) { uint8_t i; font_t font1, font2; /* initialize and clear the display */ gfxInit(); /* Set some fonts */ font1 = gdispOpenFont("UI2"); font2 = gdispOpenFont("DejaVu Sans 12"); gwinSetDefaultFont(font1); /* create the three console windows */ { GWindowInit wi; gwinClearInit(&wi); wi.show = TRUE; wi.x = 0; wi.y = 0; wi.width = gdispGetWidth(); wi.height = gdispGetHeight()/2; GW1 = gwinConsoleCreate(0, &wi); wi.y = gdispGetHeight()/2; wi.width = gdispGetWidth()/2; wi.height = gdispGetHeight(); GW2 = gwinConsoleCreate(0, &wi); wi.x = gdispGetWidth()/2; wi.height = gdispGetHeight(); GW3 = gwinConsoleCreate(0, &wi); } /* Use a special font for GW1 */ gwinSetFont(GW1, font2); /* Set the fore- and background colors for each console */ gwinSetColor(GW1, Green); gwinSetBgColor(GW1, Black); gwinSetColor(GW2, White); gwinSetBgColor(GW2, Blue); gwinSetColor(GW3, Black); gwinSetBgColor(GW3, Red); /* clear all console windows - to set background */ gwinClear(GW1); gwinClear(GW2); gwinClear(GW3); /* Output some data on the first console */ for(i = 0; i < 10; i++) { gwinPrintf(GW1, "Hello \033buGFX\033B!\n"); } /* Output some data on the second console - Fast */ for(i = 0; i < 32; i++) { gwinPrintf(GW2, "Message Nr.: \0331\033b%d\033B\033C\n", i+1); } /* Output some data on the third console - Slowly */ for(i = 0; i < 32; i++) { gwinPrintf(GW3, "Message Nr.: \033u%d\033U\n", i+1); gfxSleepMilliseconds(500); } /* Make console 3 invisible and then visible again to demonstrate the history buffer */ gwinPrintf(GW2, "Making red window \033uinvisible\033U\n"); gwinSetVisible(GW3, FALSE); gfxSleepMilliseconds(1000); gwinPrintf(GW2, "Making red window \033uvisible\033U\n"); gwinSetVisible(GW3, TRUE); gwinPrintf(GW3, "\033bI'm back!!!\033B\n", i+1); while(TRUE) { gfxSleepMilliseconds(500); } }
static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { (void)arg; GListener event_listener; geventListenerInit(&event_listener); geventAttachSource(&event_listener, (GSourceHandle)¤t_status, 0); visualizer_keyboard_status_t initial_status = { .default_layer = 0xFFFFFFFF, .layer = 0xFFFFFFFF, .mods = 0xFF, .leds = 0xFFFFFFFF, .suspended = false, }; visualizer_state_t state = { .status = initial_status, .current_lcd_color = 0, #ifdef LCD_ENABLE .font_fixed5x8 = gdispOpenFont("fixed_5x8"), .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12") #endif }; initialize_user_visualizer(&state); state.prev_lcd_color = state.current_lcd_color; #ifdef LCD_BACKLIGHT_ENABLE lcd_backlight_color( LCD_HUE(state.current_lcd_color), LCD_SAT(state.current_lcd_color), LCD_INT(state.current_lcd_color)); #endif systemticks_t sleep_time = TIME_INFINITE; systemticks_t current_time = gfxSystemTicks(); while(true) { systemticks_t new_time = gfxSystemTicks(); systemticks_t delta = new_time - current_time; current_time = new_time; bool enabled = visualizer_enabled; if (!same_status(&state.status, ¤t_status)) { if (visualizer_enabled) { if (current_status.suspended) { stop_all_keyframe_animations(); visualizer_enabled = false; state.status = current_status; user_visualizer_suspend(&state); } else { state.status = current_status; update_user_visualizer_state(&state); } state.prev_lcd_color = state.current_lcd_color; } } if (!enabled && state.status.suspended && current_status.suspended == false) { // Setting the status to the initial status will force an update // when the visualizer is enabled again state.status = initial_status; state.status.suspended = false; stop_all_keyframe_animations(); user_visualizer_resume(&state); state.prev_lcd_color = state.current_lcd_color; } sleep_time = TIME_INFINITE; for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) { if (animations[i]) { update_keyframe_animation(animations[i], &state, delta, &sleep_time); } } #ifdef LED_ENABLE gdispGFlush(LED_DISPLAY); #endif #ifdef EMULATOR draw_emulator(); #endif // The animation can enable the visualizer // And we might need to update the state when that happens // so don't sleep if (enabled != visualizer_enabled) { sleep_time = 0; } systemticks_t after_update = gfxSystemTicks(); unsigned update_delta = after_update - current_time; if (sleep_time != TIME_INFINITE) { if (sleep_time > update_delta) { sleep_time -= update_delta; } else { sleep_time = 0; } } dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time); #ifdef PROTOCOL_CHIBIOS // The gEventWait function really takes milliseconds, even if the documentation says ticks. // Unfortunately there's no generic ugfx conversion from system time to milliseconds, // so let's do it in a platform dependent way. // On windows the system ticks is the same as milliseconds anyway if (sleep_time != TIME_INFINITE) { sleep_time = ST2MS(sleep_time); } #endif geventEventWait(&event_listener, sleep_time); } #ifdef LCD_ENABLE gdispCloseFont(state.font_fixed5x8); gdispCloseFont(state.font_dejavusansbold12); #endif return 0; } void visualizer_init(void) { gfxInit(); #ifdef LCD_BACKLIGHT_ENABLE lcd_backlight_init(); #endif #ifdef SERIAL_LINK_ENABLE add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); #endif #ifdef LCD_ENABLE LCD_DISPLAY = get_lcd_display(); #endif #ifdef LED_ENABLE LED_DISPLAY = get_led_display(); #endif // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack), VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL); } void update_status(bool changed) { if (changed) { GSourceListener* listener = geventGetSourceListener((GSourceHandle)¤t_status, NULL); if (listener) { geventSendEvent(listener); } } #ifdef SERIAL_LINK_ENABLE static systime_t last_update = 0; systime_t current_update = chVTGetSystemTimeX(); systime_t delta = current_update - last_update; if (changed || delta > MS2ST(10)) { last_update = current_update; visualizer_keyboard_status_t* r = begin_write_current_status(); *r = current_status; end_write_current_status(); } #endif } uint8_t visualizer_get_mods() { uint8_t mods = get_mods(); #ifndef NO_ACTION_ONESHOT if (!has_oneshot_mods_timed_out()) { mods |= get_oneshot_mods(); } #endif return mods; } void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) { // Note that there's a small race condition here, the thread could read // a state where one of these are set but not the other. But this should // not really matter as it will be fixed during the next loop step. // Alternatively a mutex could be used instead of the volatile variables bool changed = false; #ifdef SERIAL_LINK_ENABLE if (is_serial_link_connected ()) { visualizer_keyboard_status_t* new_status = read_current_status(); if (new_status) { if (!same_status(¤t_status, new_status)) { changed = true; current_status = *new_status; } } } else { #else { #endif visualizer_keyboard_status_t new_status = { .layer = state, .default_layer = default_state, .mods = mods, .leds = leds, .suspended = current_status.suspended, }; if (!same_status(¤t_status, &new_status)) { changed = true; current_status = new_status; } } update_status(changed); } void visualizer_suspend(void) { current_status.suspended = true; update_status(true); } void visualizer_resume(void) { current_status.suspended = false; update_status(true); }