py: Move all global state (variables) to a common place.

This is allows to keep track of where all the global variables are, and will help if we ever want to have a global state object to pass to each function.  Although code size was not a reason for doing this, the change actually gives some savings of code size: about 60 bytes on x64, and 200 bytes (!) on stmhal.

This is for review only, and is still a bit messy.  It also includes a partial move away from Plan 9 style header includes.

Comments welcome!