Photon OLED view controller

I come from a web development background, and have worked primarily with Node.js for the past view years. In fact, yesterday was the first time I’d ever written C++. The first application I built was pretty basic, and essentially displayed the time, date and application status on the OLED display, scanned for Wifi networks every few minutes and sent them to a web app.

In doing this, I discovered a problem that I haven’t found an answer to yet. I want to build a new application, which will have several different views on the display, and I need to navigate through them. An easy way to do it would be to have a “view” variable, and inside the loop function just have a switch and run whatever function depending on the view, but this isn’t a nice solution.

Are there any good examples, or can anyone find some documentation on designing a view controller for something like the Photon?

Hi Benedict,

to a beginner in c++ programming, I would recommend doing the simple way with different views (different functions) and a switch-case statement in the loop to cycle through them in a cyclic order:

  • Don’t use delays in the loop-function. Work with code like this:
    uint32_t interval = 10000;
    uint32_t current = 0;
    uint32_t previous = 0;

    void loop(){
        //Update locale once every 10 Seconds
        if (current - previous > interval){
           //place your switch case here

For code that runs longer you should use unsigned int or uint32_t for your timing variables!


Thanks for the heads up, I edited my post.

1 Like

Cheers for the advice.

If working with C++ is similar to other languages, I presume globals should be avoided as much as possible, which is part of the problem I had when trying to implement a view controller with no globals.

Can you advise on whether I should be trying to avoid globals?

I am fairly new to the Particle Photon system, but coming from the even less powerful arduino platform, i often use globale variables. Using object orientation often ended i rather bad performance.

For the Photon platform I think you can use some with no problem. In your case, you would only need one globale, right? The switch-case variable.
Everything else could be defined in the functions you are using to change the view and in objects you hand over when calling them.

For example:

 int stateSwitch = 0; //global
    weather weatherObject;
    void display_weather(weather object){
    void display_clock()
    void loop(){
        case 0;
        display_weather(weather object);
        case 1:

Globals are quite common in Photon and Arduino programming (I assume embedded programming in general). I wouldn’t worry about it too much.

I come from a OOP background and tend to use OOP in my Photon code but generally still end up with a fair number of globals. You could certainly create a sort of main class. But honestly, it’s probably not worth it.

1 Like

I’d second Jürgen and TJ here - globals don’t pose a problem in themselves and can be used, but with care.

One thing you should definetly avoid when using globals: "Don’t use non-descript names, like i,t,s useless!"
Use elaborate (not extra long) descriptive names and rather add a new one before reusing one for multiple purposes.
If you want a variable to retain its content between seperate calls to a function wherein it is used (but in no other function), you can use the static modifier instead of making it global.

1 Like