Maxim Integrated / Mbed OS MAXREFDES155#

Dependencies:   MaximInterface

InitWindow.cpp

Committer:
IanBenzMaxim
Date:
2017-04-06
Revision:
8:a0d75dff3c9b
Parent:
7:66c5dedc750b
Child:
10:71359af61af8

File content as of revision 8:a0d75dff3c9b:

/*******************************************************************************
* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************/

#include <string>
#include <wait_api.h>
#include "Bitmap.hpp"
#include "Text.hpp"
#include "WindowManager.hpp"
#include "RomId.h"
#include "CC3100.hpp"
#include "DS2476.hpp"
#include "SensorNode.hpp"
#include "Factory.hpp"
#include "ErrorWindow.hpp"
#include "InitWindow.hpp"
#include "HexConversions.hpp"

#ifdef HARDWARE_TEST
#include "HardwareTestWindow.hpp"
#else
#include "DisplayIdWindow.hpp"
#endif

extern DS2476 coproc;
extern SensorNode sensorNode;
extern std::string webId;

static const int maximLogoWidth = 128;
static const int maximLogoHeight = 39;
static const uint8_t maximLogoData[] = {
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x01,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x0f,0xff,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x1f,0xff,0xf8,0x00,0x00,0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x7f,0xff,0xfc,0x00,0x00,0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0xff,0xff,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x01,0xff,0xff,0xff,0x00,0x1b,0x9c,0x1f,0x18,0xcc,0x6e,0x70,0x00,0x00,0x00,0x00
    ,0x03,0xff,0xff,0xff,0x80,0x1f,0xfe,0x3f,0x98,0xcc,0x7f,0xf8,0x00,0x00,0x00,0x00
    ,0x07,0xff,0xff,0xff,0xc0,0x1c,0xe6,0x31,0x8d,0x8c,0x73,0x98,0x00,0x00,0x00,0x00
    ,0x0f,0xff,0xff,0xff,0xe0,0x18,0xc6,0x01,0x8f,0x8c,0x63,0x18,0x00,0x00,0x00,0x00
    ,0x0f,0x80,0xfe,0x03,0xe0,0x18,0xc6,0x1f,0x87,0x0c,0x63,0x18,0x00,0x00,0x00,0x00
    ,0x1f,0x00,0x7c,0x01,0xf0,0x18,0xc6,0x39,0x8f,0x8c,0x63,0x18,0x00,0x00,0x00,0x00
    ,0x1f,0x00,0x3c,0x01,0xf0,0x18,0xc6,0x31,0x8d,0x8c,0x63,0x18,0x00,0x00,0x00,0x00
    ,0x3f,0x00,0x38,0x01,0xf8,0x18,0xc6,0x3f,0x98,0xcc,0x63,0x18,0x00,0x00,0x00,0x00
    ,0x3f,0x04,0x10,0x41,0xf8,0x18,0xc6,0x1d,0x98,0xcc,0x63,0x18,0x00,0x00,0x00,0x00
    ,0x3f,0x06,0x30,0xe1,0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x7f,0x06,0x20,0xe1,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x7f,0x07,0x61,0xe1,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18
    ,0x7f,0x07,0xc1,0xe1,0xfc,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18
    ,0x7f,0x07,0xc3,0xe1,0xfc,0x18,0x00,0x18,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x18
    ,0x7f,0x07,0x83,0xe1,0xfc,0x00,0x00,0x18,0x00,0x00,0x40,0x00,0x06,0x00,0x00,0x18
    ,0x7f,0x07,0x81,0xe1,0xfc,0x18,0xde,0x3e,0x1e,0x0f,0xcd,0x8f,0x8f,0x87,0x81,0xd8
    ,0x3f,0x07,0x01,0xe1,0xf8,0x18,0xff,0x3e,0x3f,0x1f,0xcf,0x9f,0xcf,0x8f,0xc3,0xf8
    ,0x3f,0x07,0x00,0xe1,0xf8,0x18,0xe3,0x18,0x73,0x99,0x8e,0x18,0xc6,0x1c,0xe7,0x38
    ,0x3f,0x06,0x00,0xe1,0xf8,0x18,0xc3,0x18,0x61,0x99,0x8c,0x00,0xc6,0x18,0x66,0x18
    ,0x1f,0x06,0x10,0x61,0xf0,0x18,0xc3,0x18,0x7f,0x9f,0x8c,0x0f,0xc6,0x1f,0xe6,0x18
    ,0x1f,0x04,0x38,0x61,0xf0,0x18,0xc3,0x18,0x60,0x0f,0x0c,0x1c,0xc6,0x18,0x06,0x18
    ,0x1f,0x04,0x38,0x21,0xf0,0x18,0xc3,0x18,0x71,0x98,0x0c,0x18,0xc6,0x1c,0x67,0x38
    ,0x0f,0xff,0xff,0xff,0xe0,0x18,0xc3,0x1e,0x3f,0x9f,0x8c,0x1f,0xc7,0x8f,0xe3,0xf8
    ,0x07,0xff,0xff,0xff,0xe0,0x18,0xc3,0x0e,0x1f,0x3f,0xcc,0x0e,0xc3,0x87,0xc1,0xd8
    ,0x07,0xff,0xff,0xff,0xc0,0x00,0x00,0x00,0x00,0x30,0xc0,0x00,0x00,0x00,0x00,0x00
    ,0x03,0xff,0xff,0xff,0x80,0x00,0x00,0x00,0x00,0x3f,0xc0,0x00,0x00,0x00,0x00,0x00
    ,0x01,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0xff,0xff,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x3f,0xff,0xf8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x1f,0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x0f,0xff,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x01,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

InitWindow::InitWindow() : m_state(NotStarted)
{
    setFocused();
}

void InitWindow::doRender(Bitmap & bitmap) const
{       
    Bitmap maximLogo(maximLogoWidth, maximLogoHeight, maximLogoData, Bitmap::ScanLineFormat);
    Text title;
    title.setText("MAXREFDES155#");
    title.resize(title.preferredWidth(), title.preferredHeight());
    title.move(0, maximLogo.height() + 5);
    Text description;
    description.setText(m_state == Completed ? "Any key to begin" : "Initializing...");
    description.resize(description.preferredWidth(), description.preferredHeight());
    description.move(0, title.y() + title.height() + 5);
    bitmap.overlay(maximLogo, 0, 0);
    bitmap.overlay(title.render(), title.x(), title.y());
    bitmap.overlay(description.render(), description.x(), description.y());
}

void InitWindow::updated()
{       
    switch (m_state)
    {
    case NotStarted:
        m_state = Running;
        invalidate();
        break;
        
    case Running:
        {
            {        
                DS2476::Page page;
                
                // Enable coprocessor.
                DS2476::CmdResult result = coproc.readMemory(DS2476::GpioControl, page);
                if (result == DS2476::Success)
                {
                    page[0] = 0xAA;
                    result = coproc.writeMemory(DS2476::GpioControl, page);
                }
                if (result != DS2476::Success)
                {
                    if (windowManager() != NULL)
                    {
                        windowManager()->pop();
                        std::auto_ptr<Window> window(new ErrorWindow("Failed to enable coprocessor"));
                        windowManager()->push(window);
                    }
                    break;
                }
                
                // Enable ROM ID.
                result = coproc.readMemory(DS2476::RomOptions, page);
                if (result == DS2476::Success)
                {
                    page[0] = 0xAA;
                    result = coproc.writeMemory(DS2476::RomOptions, page);
                }
                if (result != DS2476::Success)
                {
                    if (windowManager() != NULL)
                    {
                        windowManager()->pop();
                        std::auto_ptr<Window> window(new ErrorWindow("Failed to enable ROM ID"));
                        windowManager()->push(window);
                    }
                    break;
                }
            }
            
            // Read ROM ID.
            {
                OneWire::RomId romId;
                DS2476::CmdResult result = DS2476::readRomId(coproc, romId);
                if (result != DS2476::Success)
                {
                    if (windowManager() != NULL)
                    {
                        windowManager()->pop();
                        std::auto_ptr<Window> window(new ErrorWindow("Failed to read ROM ID"));
                        windowManager()->push(window);
                    }
                    break;
                }
                if (!romId.valid())
                {
                    if (windowManager() != NULL)
                    {
                        windowManager()->pop();
                        std::auto_ptr<Window> window(new ErrorWindow("ROM ID is not valid"));
                        windowManager()->push(window);
                    }
                    break;
                }
                webId = byteArrayToHexString(romId.buffer.data(), romId.buffer.size());
            }

            // Provision DS2476 if necessary.
            {
                bool provisioned;
                bool result = checkCoprocessorProvisioned(coproc, provisioned);
                if (!result)
                {
                    if (windowManager() != NULL)
                    {
                        windowManager()->pop();
                        std::auto_ptr<Window> window(new ErrorWindow("Failed to check coprocessor provisioned"));
                        windowManager()->push(window);
                    }
                    break;
                }
                if (!provisioned)
                {
                    bool provisionResult = provisionCoprocessor(coproc);
                    if (!provisionResult)
                    {
                        if (windowManager() != NULL)
                        {
                            windowManager()->pop();
                            std::auto_ptr<Window> window(new ErrorWindow("Failed to provision coprocessor"));
                            windowManager()->push(window);
                        }
                        break;
                    }
                }
            }
            
            // Always start with laser disabled.
            sensorNode.setLaserEnabled(false);
            
            int result = CC3100::instance().start();
            if (result != 0)
            {
                if (windowManager() != NULL)
                {
                    windowManager()->pop();
                    std::auto_ptr<Window> window(new ErrorWindow("Failed to start WiFi interface"));
                    windowManager()->push(window);
                }
            }
            wait_ms(1500);
        }
        
        m_state = Completed;
        invalidate();
        break;
        
    case Completed:
        break;
    }
}

bool InitWindow::doProcessKey(Key)
{
    if (m_state == Completed)
    {
        if (windowManager() != NULL)
        {
            windowManager()->pop();
#ifdef HARDWARE_TEST
            std::auto_ptr<Window> window(new HardwareTestWindow);
#else
            std::auto_ptr<Window> window(new DisplayIdWindow(DisplayIdWindow::PreConnectMode));
#endif
            windowManager()->push(window);
        }
    }
    return true;
}