#
# Copyright (C) 20010-2012 Lucas Aníbal Tanure Alves - ME
# Contact   Lucas Tanure [[email protected]]
#   
# This file is part of GoGo Real.
#
# GoGo Real is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GoGo Real is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GoGo Real.  If not, see <http://www.gnu.org/licenses/>.
from register import registerBoard


class GoGoRealV2 ():

    def __init__(self):
        pass

    def connect(self, port):
        return False

registerBoard(GoGoRealV2, "GoGo Real v2")
        Step one: Split int that comes after 128 in compiled logo code. Now GoGo Real expects two bytes for this
        first set the pointer to burn
        then send the number of bytes to burn in 2 bytes [hibyte,lowbyte] = len(logo code) + 3
        send the bytes of logo code plus [128,0,0] at the end
        set the pointer to burn  the execution code at button adress
        send the number of bytes , allways 2 so [0,2]
        send the number of bytes of logo code less 3, because we add [128,0,0] to the end opf this code
        and this code is for jump to start adress and execute
        @param compiled_logo_code:
        """
        compiled_logo_code += [128, 0, 0]

        compiled_logo_code = self.split_bytes(compiled_logo_code)
        total_logo_bytes = len(compiled_logo_code) - compiled_logo_code.count(128)
        command_set_pointer = self.protocol['SET_WRITE_POINTER'] + self.protocol['CODE_START_ADDRESS']
        burn_logo_code = self.protocol['BURN_CODE'] + [total_logo_bytes / 256, total_logo_bytes % 256]
        set_exec_button = self.protocol['SET_WRITE_POINTER'] + self.protocol['REAL_BUTTON_ADDRESS']
        burn_exec_button = self.protocol['BURN_CODE'] + [0, 2]
        exec_button = [total_logo_bytes / 256, (total_logo_bytes-3) % 256]

        self.process_read_write(command_set_pointer)
        self.process_read_write(burn_logo_code+compiled_logo_code)
        self.process_read_write(set_exec_button)
        self.process_read_write(burn_exec_button+exec_button)

        return True

registerBoard(GoGoRealV1, "GoGo Real v1")