FabGL
ESP32 Display Controller and Graphics Library
VGA/MouseOnScreen/MouseOnScreen.ino

PS/2 mouse and mouse pointer on screen

/*
Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
Copyright (c) 2019-2022 Fabrizio Di Vittorio.
All rights reserved.
* Please contact fdivitto2013@gmail.com if you need a commercial license.
* This library and related software is available under GPL v3.
FabGL 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.
FabGL 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 FabGL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fabgl.h"
fabgl::VGAController DisplayController;
fabgl::Canvas canvas(&DisplayController);
fabgl::PS2Controller PS2Controller;
int indicatorX = 300;
int indicatorY = 170;
int cursor = 0;
void showCursorPos(MouseStatus const & status)
{
canvas.setPenColor(Color::Blue);
canvas.setBrushColor(Color::Yellow);
canvas.drawTextFmt(indicatorX, indicatorY, " %d %d ", status.X, status.Y);
}
void setup()
{
DisplayController.begin();
DisplayController.setResolution(VGA_640x350_70HzAlt1);
//DisplayController.setResolution(VGA_640x240_60Hz); // select to have more free memory
// Setup mouse
PS2Controller.begin();
PS2Controller.mouse()->setupAbsolutePositioner(canvas.getWidth(), canvas.getHeight(), true, &DisplayController);
DisplayController.setMouseCursor((CursorName)cursor);
canvas.setBrushColor(Color::Blue);
canvas.clear();
canvas.selectFont(&fabgl::FONT_8x8);
canvas.setGlyphOptions(GlyphOptions().FillBackground(true));
showCursorPos(PS2Controller.mouse()->status());
}
void loop()
{
MouseStatus status = PS2Controller.mouse()->getNextStatus(-1); // -1 = blocking operation
// left button writes pixels
if (status.buttons.left) {
canvas.setPenColor(Color::BrightRed);
canvas.setPixel(status.X, status.Y);
canvas.moveTo(status.X, status.Y);
}
// right button change mouse shape
if (status.buttons.right) {
cursor = ((CursorName)cursor == CursorName::CursorTextInput ? 0 : cursor + 1);
DisplayController.setMouseCursor((CursorName)cursor);
}
// middle button clear screen
if (status.buttons.middle) {
canvas.setBrushColor(Color::Blue);
canvas.clear();
}
// mouse wheel moves cursor position indicator
if (status.wheelDelta != 0) {
indicatorY = fabgl::tclamp(indicatorY + status.wheelDelta, 0, canvas.getHeight() - 16);
PS2Controller.mouse()->status().wheelDelta = 0;
canvas.setBrushColor(Color::Blue);
canvas.clear();
}
showCursorPos(status);
}
void setMouseCursor(Cursor *cursor)
Sets mouse cursor and make it visible.
A class with a set of drawing methods.
Definition: canvas.h:70
MouseStatus & status()
Gets or sets current mouse status.
Definition: mouse.h:313
void setupAbsolutePositioner(int width, int height, bool createAbsolutePositionsQueue, BitmappedDisplayController *updateDisplayController=nullptr, uiApp *app=nullptr)
Initializes absolute position handler.
Definition: mouse.cpp:194
MouseStatus getNextStatus(int timeOutMS=-1)
Gets the next status from the status queue.
Definition: mouse.cpp:377
static Mouse * mouse()
Returns the instance of Mouse object automatically created by PS2Controller.
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes PS2 device controller.
The PS2 device controller class.
Definition: ps2controller.h:82
Represents the VGA bitmapped controller.
Definition: vgacontroller.h:89
GlyphOptions & FillBackground(bool value)
Helper method to set or reset fillBackground.
This file is the all in one include file. Application can just include this file to use FabGL library...
#define VGA_640x350_70HzAlt1
Definition: fabglconf.h:228
CursorName
This enum defines a set of predefined mouse cursors.