Use deviceorientation and devicemotion events

Hi there,
I came across the sensor-js demo and I am trying to figure out how to pull the orientation values in a p5 sketch running on a mobile.
How would you go about attaching and using the deviceorientation and devicemotion events?

Here is the source of the demo.

Thank you