Video details

I can read your mind using JavaScript - Anca Spatariu | JSHeroes 2022

JavaScript
08.15.2022
English

Transcript

Thank you for having me. Hopefully my presentation will show up. There we go. So I can read your mind using JavaScript. This sounds a bit crazy, and if I tell you that this is a lightning and talk and I can do it in less than ten minutes, well, that sounds even crazier. I wanted to say it's simple, but I learned today that it's not. Okay. So it takes about 40 lines of code. So thanks for that. It was actually a good point. Few words about me. My name is Anka. As Alex mentioned, I come from Timishara. I'm a software developer at Wi Video and I also like organizing events like this one. Okay. So today I'll be using this device together with web bluetooth to attempt and deliver on the promise I made in the presentation. And if all goes well, all these sensors that I have on this headband will read my mind. And if you want to yours as well. During the break time, if you want to give it a try, please find me and we can do it. So this is a headband fitted with EEG sensors that are reading basically the electrical activity of the brain. But let's see how it actually works. So for this, I have prepared a few libraries in my setup. We're going to be using two of them and we're going to start from scratch. So first, what do we need for this? Well, we need the Bluetooth because this is how this headband communicates. So for this I'm going to declare a variable and then require webb. Okay, we have that in place. Another thing that we need is a library that's called Musejs. It's library created specifically for this device to make the communication easier. So for that, I'm going to start by adding the variable constmus requires. All right, this is all we need in terms of libraries. And I'm going to add a placeholder for a function that we'll use to later on connect. So const connect. This is going to be on a synchronous function and for now let's leave it empty. All right, now that you have this in place, let's think about it. What do we need to actually create a connection? Well, we need the device, I showed you that we have it. So let's declare a variable for it. Then we need a client that connects to that device. We're going to get one soon. And of course we need a protocol to make them communicate. And in our case that is the gate protocol. So I'm going to add the variable for that as well. Now if we go into the function, we can start putting stuff in our variables. So for the device, I'm going to use the Bluetooth library I just imported. And with this I'm going to request the device. And this will take some parameters I want to scan specifically for my device. Not everyone's phones in the audience today and for this, I'm going to pass some filters. Filters is an array and in this array I'm going to pass the first filter, which is services. And luckily our friends at Mutejs have the idea the service ID in a variable for us because otherwise it would have been much harder for me. All right, with this, actually, we have to wait for this. After we have the device, let's pull the gatt from it. So for the gatt we can type, wait and device gatt connect. All right, now we have the gate server and I'm going to instantiate the client. Okay. For the client, we are going to use our news library. All right. And we are going to wait for it to connect to the client, connect gatt and then we are going will also start it. All right, now that we have this, this should be all that we need to create a connection. And if I learned something from my last presentation, is that we also need to call a function. So I'm going to create an immediately invoked function expression and actually wait for it to connect. And then let's do a console dot log. I am now reading your mind using and let's say the device name. Device name. Okay, with this in place, it should work. Let me just go to my terminal. I have my terminal right here and I'm switching on the device and hopefully it will work. If not, I have backups. Okay, so you can see that we have here the Mute number and also the light on this is not blinking anymore. So this means we are connected. And now let's try and wear it and see what we can pull from it. All right, luckily we just need the front sensors because the year ones will be messed up by the headset. Okay, going back to the code, I'm going to add another function that's going to be called read mind. Guess what that does. And for this, I'm actually going to be using RxJS to subscribe to a stream that our client provides for us. So for this client, EEG readings. This is how the prop is named subscribe. And let's quickly see how the data looks before anything else. Now let's call the function. So there it is. And if you can take a look here, we already have readings and you could say that I already delivered on my promise. So this is how you read the mind using JavaScript. But now that we have these samples and I still have like two minutes left, let's try and do something with this. So in our function here, I'm going to quickly filter out the data I received from the electrodes placed on my forehead. Now, the way those work is if they sense some activity on that part of the brain, then it will give a higher value and I can trick that by blinking. So if I blink on my left eye, I'm going to pass, I'm going to make this electrode read a higher value. So let's see how that works. For this, I'm going to add an if statement. And in this if data that it was called electrode, electrode equals one. I'm going to take this array of data and pick the strongest signal I have. And for this, I'm going to have a const max variable. And in this one, I'm going to use the math max and pass the data samples. All right. And now if max is bigger than, let's say, $800, I'm going to console log left blink. So, as you might imagine, we can do this copy, paste and add the electrode with the index number two and switch this to right wing. And for this, I'm going to pull my camera. Okay. So hopefully you can see me blink. I have to warn you, though, the calmer you are the best. It works in terms of reading, and I'm really an anxious person. So we're going to get some different values here. Did you turn off? Let me just check. Luckily, I showed you the data before, so let's try it again. Okay. So there you go. It sort of works. If I would have been in a more Zen environment, it would have worked better. But, yeah, generally, this is how you read the mind using JavaScript. And I noticed I don't have an ending slide. But thank you all for having me.