protobject code - bellinux/pcode GitHub Wiki


Welcome to the documentation for Protobject Code, a web application designed to build physical, distributed and advanced interactive systems. Protobject Code allows you to use both JavaScript and Python to program your applications, enabling flexibility and power in creating interactive components. These components can be programmed across different devices, facilitating complex and distributed system development.

Getting Started

Protobject Code provides seamless integration between JavaScript and Python. Below, you'll find links to the documentation that explain how devices can communicate with each other and detail the available components and their usage. Each component has specific methods and properties that you can use to create your interactive systems.

Communication between Devices

Each project in Protobject Code consists of multiple source files, each corresponding to a different device (smartphone, tablet, PC). These source files can have either .js or .py extensions, depending on the language used. This allows for the use of both JavaScript and Python within the same project. For example, a .js file on one device will use JavaScript, while a .py file on another device will use Python.

Devices can send and receive messages from each other using the Protobject Communication API. This facilitates seamless communication and interaction between different components of your interactive application.

Available components

Components are displayed in the right sidebar.

Example Usage

Create the Source Files

Create a source file lamp.js (JavaScript) with the following code:

import Protobject from './js/protobject.js'
import Lamp from './js/lamp.js'

let myLamp = new Lamp();

Protobject.onReceived(function(ev){
  if (ev === "myCustomMessageOn") {
    myLamp.setColor("white");
  } else if (ev === "myCustomMessageOff") {
    myLamp.setColor("black");
  }
});

Next, create a source file switch.py (Python) with the following code:

from protobject import Protobject
from switch import Switch

mySwitch = Switch()

mySwitch.onTurnedOn(lambda: Protobject.send("myCustomMessageOn").to("lamp.js"))
mySwitch.onTurnedOff(lambda: Protobject.send("myCustomMessageOff").to("lamp.js"))

Connect the Devices

Connect both source files to two devices, such as two smartphones, using the "Connect" button available in each source file within the Protobject Code interface. One device will run switch.py and act as the switch, while the other will run lamp.js and act as the lamp.

Execute the Application

Execute the application by pressing the "Play" button on the Protobject Code interface. Interacting with the switch on one smartphone will cause the lamp on the other smartphone to respond accordingly: turning white when the switch is turned on and black when the switch is turned off.

Mixing JavaScript and Python

Note that we mixed JavaScript and Python in the same project, but you are free to use either language interchangeably. Here is an example of how you can achieve the same functionality using Python instead of JavaScript and vice versa.

The Python version of lamp.js, named lamp.py, is as follows:

from protobject import Protobject
from lamp import Lamp

myLamp = Lamp()

def lampActions(ev):
  if ev == "myCustomMessageOn":
    myLamp.setColor("white")
  elif ev == "myCustomMessageOff":
    myLamp.setColor("black")

Protobject.onReceived(lampActions)

The JavaScript version of switch.py, named switch.js, is as follows:

import Protobject from './js/protobject.js'
import Switch from './js/switch.js'

let mySwitch = new Switch()

mySwitch.onTurnedOn(function(){
  Protobject.send("myCustomMessageOn").to("lamp.py")
})

mySwitch.onTurnedOff(function(){
  Protobject.send("myCustomMessageOff").to("lamp.py")
})

You can mix and match JavaScript and Python in your project based on your preferences.

⚠️ **GitHub.com Fallback** ⚠️