element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experimenting with Gesture Sensors
  • Challenges & Projects
  • Design Challenges
  • Experimenting with Gesture Sensors
  • More
  • Cancel
Experimenting with Gesture Sensors
Challenge Blog Blog #6: Connecting to MAX25405EVKIT using Web Browser and TypeScript
  • Challenge Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experimenting with Gesture Sensors to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: misaz
  • Date Created: 29 Oct 2022 10:44 AM Date Created
  • Views 2282 views
  • Likes 10 likes
  • Comments 16 comments
  • MAX25405
  • gesture sensors
  • web
  • maxim integrated
  • TypeScript
  • experimenting with gesture sensors
  • javascript
  • uart
  • Web Serial
  • MAX25405EVKIT
  • maxim
Related
Recommended

Blog #6: Connecting to MAX25405EVKIT using Web Browser and TypeScript

misaz
misaz
29 Oct 2022
Blog #6: Connecting to MAX25405EVKIT using Web Browser and TypeScript

Hello everyone. I welcome you to my 6th blog as part of Experimenting with Gesture Sensors Design Challenge. In this blog post I will make one step further from theory and evaluation kit description and I start implementing simple code which will act as a base for my first gesture-controlled game. In this blog I will describe my first experiments with Web Serial API which is JavaScript API used for interfacing Serial (UART) devices from web browser. At the end I will show my first web application which prints firmware version and also show heat map similarly like Maxim’s PC program does.

Web Serial

Web Serial is term for JavaScript API used for communicating with UART devices connected to the computer. Historically web browsers did not allow you to access any HW using web page but in last years this started changing and JavaScript was extended by set of APIs like Web USB, Web Bluetooth or Web Serial which I will show today.

Experiment

For my first experiment with this technology, I decided to make simple page which

  • Connects to the MAX25405EVKIT Serial port
  • Executes “ver” command and prints output
  • Starts timer which will periodically read RAW ADC accumulators and draw heat map similarly to Maxim’s GUI program.

TypeScript

Instead of writing JavaScript I decided to use TypeScript. I did this because TypeScript adds type information and some syntax sugar to the JavaScript, so IDE (Visual Studio) can determine more information needed for functional Intellisense (Intellisense is name for code autocomplete in Visual Studio). At the end it is transpiled to JavaScript. Because it is new API there are no type definition for this API in standard TypeScript definitions, but you can download required definition from DefinelyTyped Github and include it in the project.

In this blog post I do not describe all details like HTML code and basic JavaScript/TypeScript constructions because this blog is not designed as HTML/JavaScript tutorial and this information have no relation to MAX25405. Source codes are available on Github here, so you can simply open them if you are interested in these details which I omit. Also note that in code examples I omitted almost all error checks which are present in source code. I did this for simplifying code examples. Error checks usually add many additional lines.

Requesting access to Serial devices

Before we can open serial port, we need to request permission from user. We can do this using requestPort function. All function related to Web Serial API are available in navigator.serial object. We can call this function as is, but we can specify optional parameter with some requirements. For example, we can in case of serial ports connected to computer over USB limit devices offered to user by USB Vendor Id (VID) and Product Id (PID). Because MAX25405 is this kind of device with emulated UART as USB CDC, I decided to do so. Advantage is that user can’t select wrong device because browser will not offer other COM ports to you. Function (and most other Web Serial functions) returns Promise because this function does not complete immediately. Instead, it completes sometime in the future (after user confirm his selection or reject the request). You can use standard .then() syntax but because I use TypeScript I used await keyword. There are two restrictions about calling this function. It must be called from handler which was triggered by user action (for example button click). Calling it in non-user triggered context (for example in onload handler, handler of setInterval, …) will result to fail. Other similar limitation is that webpage must run over encrypted HTTPS or run from localhost. Otherwise, you get error.

await navigator.serial.requestPort({
       filters: [
             {
                    usbVendorId: 0x0B6A, // Maxim Integrated
                    usbProductId: 0x4360 // MAX32620 MCU with MAX25405EVKIT firmware
             }
       ]
});

Following popup appears after executing:

image

Search and open port

After gaining permissions you can enumerate serial ports and open it. Open method accepts (optional) parameters. Parameters are passed using object and you can for example specify baudrate, parity, number of stop bits, …. For writing and reading port we need to get instances of streams. We can get them using getWriter() and getReader methods from writable and readable objects on serial port object.

var serialPorts = await navigator.serial.getPorts();
for (var i = 0; i < serialPorts.length; i++) {
    await serialPorts[i].open({ baudRate: 115200 });
    var writer = serialPorts[i].writable.getWriter();
    var reader = serialPorts[i].readable.getReader();
}

Writing to serial port

For writing we can use write method of writer which we get in previous step. It has write method. This method does not expect string but rather expects array of 8-bit unsigned numbers. For converting string to array of numbers we can use TextEncoder class.

await writer.write(new TextEncoder().encode("ver\n"));

Reading from serial port

Reading we can do similarly but it has one issue. It reads very small fragments, frequently only single character. For this reason, we need to implement loop which will read whole line. It is slightly more complicated because we need to check for errors. Reader class do not return char directly, but it returns object which has something like status field which in case of occasional closing port while receiving data contains done property set to true (I guess that this can happen for example when disconnecting device from PC at the time of waiting for data). After receiving we can use TextDecoder to convert received array of bytes to natural string.

var message = "";
var completed = false;

while (!completed && message.indexOf("\n") == -1) {
       var output = await reader.read();
       if (output.done) {
             completed = true;
       } else {
             var received = new TextDecoder().decode(output.value);
             message = message + received;
       }
}

After receiving message, I printed output on page.

 image

After this I started implement heat map. Sending and receiving data is very similar.

Parsing big-endian numbers

As I stated in previous blog post registers containing ADC accumulator values are in big-endian order. For this reason processing output from “read reg 0x10 120” requires additional processing stuff. I need to combine two values by shifting and ORing them and I also need to handle sign because sensor can also return negative values (reason for negative values I described in third blog). Because values are encoded in two’s-complement I used construction with inverting all bits and adding hot one for converting negative unsigned number to signed. After processing pixel value, I implement threshold for values slower then 0 and higher than 4000. Note this algorithm is not the same as algorithm for computing pixel colors of heat map in Maxim’s GUI. Maxim implemented more advanced algorithm which handles underrange and overrange conditions by adjusting boundaries for color calculation. I implemented it simpler. At the end, pixel intensity is formed to RGB color and set to background color of the related table cell.

await writer.write(new TextEncoder().encode("reg read 0x10 120\n"));
var output = await serialReadLine(reader);
var bytes = output.split(" ").map((x) => Number.parseInt(x, 16));

for (var y = 0; y < 6; y++) {
	for (var x = 0; x < 10; x++) {
		// values are encoded as signed 16-bit integer encoded using BIG endian.
		var byte1 = bytes[2 * (y * 10 + x)];
		var byte2 = bytes[2 * (y * 10 + x) + 1];
		var valueRaw = (byte1 << 8) | byte2;
		var value = valueRaw;
		if (byte1 & 0x80) {
			value = ~valueRaw + 1;
		}

		if (value < 0) {
			value = 0;
		}

		if (value > 4000) {
			value = 4000;
		}

		var pixelValue = 255 * value / 4000;

		var cell = table.children[0].children[y].children[x] as HTMLTableCellElement;
		cell.style.backgroundColor = "rgb(0, " + Math.round(pixelValue) + ", 0)";
	}
}

See it in the action

You can see it in the action on the following video. After connecting I placed hand before sensor and did several gestures including shaking hand.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Try it yourself

Because it is website you do not need to download anything. I published this application to my website, so you can try it yourself very easily. Just connect your MAX25405 EVKIT to the computer and open following link. Note that currently only Google Chrome and Microsoft Edge support this feature.

https://misaz.cz/Public/MAX25405_WebSerialDemo/

image

Source codes

As I mentioned above all source codes are on Github. Note that according to best practices, Github repository do not contains compiled outputs (they are gitignored). If you want to see compiled outputs without transpiling them manually, you can see them directly on my website. I transpiled them without any optimizations, so they are pretty readable.

https://github.com/misaz/MAX25405EVKIT-WebSerialDemo

https://misaz.cz/Public/MAX25405_WebSerialDemo/MAX25405_ConnectionTest.js

What’s next?

Currently I am working on my first game using approach described in this blog. So most probably my next blog will be about my first gesture-controlled game. I already ordered and paid domain for it (http://gesture-tetris.fun/), but I did not configure web server and SSL certificate yet. In the meantime, I try to help other challengers on forum (and private messages) with compiling, flashing and debugging firmware provided by Maxim. Forum thread has over 70 replies and we resolved most issues, but some still remains. Most probably after resolving I will write some blog post or tutorial for saving time other challengers.

Next blog: Blog #7: Debugging Maxim’s Firmware Framework Crash

  • Sign in to reply
  • misaz
    misaz over 2 years ago in reply to asokfair

    Hi.

    Sorry for late reply.

    Yes, they implemented processFrame. It does two things, process data by calling runGesture function and sent data over UART if enabled. If you go to runGesture, then you can see that they calls runDynamicGesture or runTracking depending on selected mode. In runGesture you can see some data processing but as you can see they fill only few output fields and the most important field (gesture type) is not filled because there is no algorithm for doing this. They preprocess data somehow but do not complete gesture detection. They generaly preprocess data but do not implement any state dependent gesture detection. Some challengers decided to implement their own algorithm (I am working on basic algorithm right now) but for basic usage I recommend restoring stock firmware and going differently. Methods of using kit without need for programming own gesture detection algorithm are at the end on this comment. Now I will show you how to restore stock firmware. You need a backup of the firmware. If you do not have backup, please contact me via private message and I will send it for you but because it is Maxim proprietary I will not share it totally publicly.

    Steps for flashing firmware to the MAX32620 MCU by J-Link are following:

    • Disconnect USB cable between MAX32620FTHR and PC
    • Disconnect power from EVKIT
    • Remove EVKIT Shield Board (green) from MAX32620FHTR and for now let the evkit aside
    • Connect only MAX32620FTHR to the PC using USB cable
    • Connect J-Link to the MAX32620FTHR
    • Start J-Flash utility, select Create New project and start it:
      image
    • Click three dots button:
      image
    • Type MAX32620 to the device field, select the only MCU found and click Ok:
      image
    • Connect to the target:
      image
    • Drag and drop stock firmware file to the window
    • If you are using full flash memory dump, then let start address 0 and just confirm:
      image
    • Erase the entire chip:
      image
    • It should success:
    • Then Program and verify stock firmware to the MCU:
      image
    • Check results:
      image
    • If you receive error, try erase chip again and try program it again. In my case it failed for a first time but at second attempt I succeeded.
    • Disconnect J-Link from MCU
    • Disconnect USB connecting MCU and PC
    • Assemble EVKIT again
    • Check that EVKIT software work:

    Now you can again use stock UART interface with functional gesture detection algorithm. This UART interface is virtual (emulated over USB). You can use it in several ways:

    • On Raspberry Pi you can use it as a standard UART /dev/ttyACM0 (or 1 if other UART is present, 2 if two other UARTs was present in the system and so on). You can write your app in whatever language you want. There are plenty of tutorials for Python on the internet.
      image
    • Use WebSerial and write online application interfacing with the sensor like I did. See my Blog #6: Connecting to MAX25405EVKIT using Web Browser and TypeScript and Blog #9: Gesture Controlled Tetris.
    • Use special Arduino shield with chip capable of acting as USB Host. BigG shown it in his blog Gesture Mouse Experiment #3: Polling the Gesture Sensor EV Kit using the default Serial API.
    • Use microcontroller with USB host and implement it yourself.

    Of course, you are not limited to 4 methods mentioned above and you can freely implement your firmware form the scratch without need of using any UART but it is lot of work. I am currently working on it. You can use my library which works with any MCU (you need to provide implementation for SPI methods) but it only provides you raw values from the sensor. Gesture detection algorithm you need currently implement yourself. You can also use library from BigG which is designed for use in Mbed environment (like Maxim Firmware framework which you already tried). Because time is running low I recommend using Raspberry Pi or WebSerial API which I mentioned above. They are the easiest way of using MAX25405 Evaluation Kit.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • asokfair
    asokfair over 2 years ago in reply to asokfair

    But i see that, they have implmented

            getSensorPixels(pixels, getGestureConfigPtr()->flip_sensor_pixels);
            processFrame(pixels);
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • asokfair
    asokfair over 2 years ago in reply to misaz

    i downloaded from maxim's website firmware_framework.zip, I have Jlink , im programing over bootloader method drag and drop

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • misaz
    misaz over 2 years ago in reply to asokfair

    You are using Maxim's Firmware framework, don't you? Firmware framework as we have later found has not implemented any gesture recognition algorithm and it is very incomplete project. You need to revert stock firmware if you want to continue with (functional) UART interface. Do you have MAX32625PICO or any other SWD/JTAG probe?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • asokfair
    asokfair over 2 years ago in reply to misaz

    gesture firmware framework version 1.0

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube