KanoComputing / community-sdk

Scripts, tools and libraries to help you to interact with your Kano Devices in your favourite programming language.
MIT License
31 stars 10 forks source link

Kickstarter #13

Open dpkano opened 5 years ago

dpkano commented 5 years ago

Hi, I'm trying to send some javascript code to the kickstarter pixel kit. Ideally I'd like to send code like this:

global.when('start', function () {
  devices.get('lightboard').turnOn({
    type: 'single',
    x: 1 - 1,
    y: 1 - 1
  }, '#4CAF50');
}); 

When I export the "app" from the desktop application I get a .kcode file which is a json file. In this file I can see ".code.snapshot.javascript". If I change that content and import again nothing works... Is there a way I can auto generate the block part from the javascript I just generated? Or simply just push the javascript code?

Thanks, Daniel

murilopolese commented 5 years ago

Hey, @dpkano!

If you are using the app to generate the code, perhaps the best idea would be to use the app to upload the code. Currently the SDK does not support the Kickstarter Pixel Kit but even if it does, it will probably not focus on uploading code generated from Kano Code App. Not because we don't want it to happen but because right now the idea is to have an independent API to draw on screen and get button events.

Your code would probably look more like this example of how to stream a frame to the Pixel Kit.

Having said that, there is an "RPC" method to upload animations to the Retail Pixel Kit and code (like yours) to the Kickstarter Pixel Kit. Those methods are not yet implemented by this SDK for technical reasons: They require way more configuration and a more complex approach then just sending a "frame" to the screen.

If you are up to interacting with the Kickstarter Pixel Kit with the communitysdk API I can try to add the Kickstarter vendor/product id to the device manager so it will list your Pixel Kit as a valid communitysdk device. If you want to give a quick try, you could try to connect to your device using the following lines:

// If your script is running from the root folder
const PixelKit = require('./src/retailpixelkit.js');

// First you need to find your Pixel Kit serial port. That changes depending of your OS.
let pk = new PixelKit({
  path: 'YOUR PIXEL KIT SERIAL PORT PATH HERE',
});

// Connect to your Pixel Kit, interact with it (check the examples) and handle errors
pk.connect()
  .then((connectedPixelKit) => {
    // Do stuff with your Pixel Kit here!
  })
  .catch((err) => console.log(err))

Let me know if you give it a try and what kind of errors and ideas you get!

dpkano commented 5 years ago

Nice. I'll give it a try. In addition to that I tapped the network communication between the app and the kit. Instead of using port 9998, I saw the port 3000 was being used. So I should that port here, right? So aside from the port, is the protocol in both types of devices (kickstarted and retail) the same?

Thanks, Daniel

murilopolese commented 5 years ago

Oh nice! Well spotted!!!! :tada: :1st_place_medal:

In addition to that I tapped the network communication between the app and the kit. Instead of using port 9998, I saw the port 3000 was being used. So I should that port here, right?

Yes, in case you want to connect via WebSocket to your Pixel Kit you should definitely change that port.

So aside from the port, is the protocol in both types of devices (kickstarted and retail) the same?

Yes, they both implement an RPC protocol. If you want more detail on how that works, this file is the implementation we are using.

If you send a JSON on a very specific format to the Pixel Kit (over WebSocket or USB Serial) it will respond with another JSON. That is also true for hardware events like button and joystick presses. Although both Pixel Kits are RPC servers, they have a few "methods" that are different. Unfortunately I don't have in hands a full specification of this API but if you sniff the packets Kano Code App sends you are definitely going to find them all!

dpkano commented 5 years ago

Quick update: 1) I tried the WS: I keep receiving ECONNRESET 2) I tried to install the npm serial port dependency on windows... and I keep having errors.... like "Error: gyp failed with exit code: 1"... :(

I did not have time to fix these issues... maybe on the weekend... :(

murilopolese commented 5 years ago

Hey, @dpkano ! Thanks a lot for the update: You are a star 🌟!

The ECONNRESET might be something getting stuck on the Pixel Kit side. I hate when I have to say that, it feels like I'm dumbing down other people but unfortunately I have to do it: Have you tried to turn the Pixel Kit off and on again 🤣

This could be that the previous WebSocket connection wasn't closed properly so the Pixel Kit rejects any new connection before the previous one is closed. I have been fiddling around with WebSockets for a while now and I still don't have a good answer on how to gracefully close it but I hope one day we'll find it.

Now about the gyp failing... That is an old friend of mine...

I'm not sure how familiar you are with gyp but it is a tool to compiling native binaries on the user machine. For example: Nodejs doesn't have native support for serial communication so the nodeserialport module has some C++ code that gets compiled by your machine to do this job. The reason for that is every platform implements serial communication in a different way so compiling from C is a must. If you are lucky enough npm will find out that the module has "precompiled" versions and will automatically download it for you, without having to compile. If there are no "precompiled" binaries it will try to compile.

There is a list of things that could be wrong and perhaps it's worth to give a look at Installing SerialPort from their official documentation. It might be a nodejs, npm or node-gyp version problem. It could also be a matter of installing some dependencies on your computer (such as VisualStudio and the Windows SDK). It's hard to say but let me know how it goes and perhaps there is something on the code or docs I can update to help!