#!/usr/bin/env python
# coding=utf-8
import codecs
import sys
sys.path.append('..')

import canmatrix.exportall as ex
from canmatrix.canmatrix import *

#
# create target Matrix
#

db = CanMatrix()

db._BUs.add(BoardUnit("testBU"))
db._BUs.add(BoardUnit("recBU"))

myFrame = Frame(0x123, "testFrame1", 8, "testBU" )

mySignal = Signal("someTestSignal", 
          startBit = 9, 
          signalSize = 11,
          is_little_endian=False, 
          is_signed = False, 
          factor=5.0, 
          offset=1.0,
          min=0,
          max=500,
          unit="specialCharUnit°$".decode("utf-8"),
          receiver=["recBU"])
mySignal2 = Signal("Signal", 
          startBit = 20, 
          signalSize = 3,
          is_little_endian=True, 
          is_signed = False, 
          factor=1.0, 
          offset=0.0,
          min=0,
          max=6,
          unit="someUnit",
          receiver=["recBU"])

mySignal2.addValues(1, "one")
mySignal2.addValues(2, "two")
mySignal2.addValues(3, "three")

mySignal.addComment("Multi \n Line \n Signal comment with a-umlaut: ä".decode("utf-8"))
myFrame.addComment("Multi \n Line \n Frame comment")

myFrame.addSignal(mySignal)
myFrame.addSignal(mySignal2)

myFrame2 = Frame(0x12, "extendedFrame", 8, "testBU" )
myFrame2._extended = 1

db._fl.addFrame(myFrame)
db._fl.addFrame(myFrame2)

db.boardUnitByName("testBU").addComment("sender ECU")
db.boardUnitByName("testBU").addAttribute("NetworkNode", 0x111)
db.boardUnitByName("recBU").addComment("receiver ECU")

db.frameByName("testFrame1").addAttribute("GenMsgCycleTime", 100)

db.addFrameDefines("GenMsgCycleTime",  'INT 0 65535')
db.addBUDefines("NetworkNode", 'INT 0 65535')


#
#
# export the new (target)-Matrix for example as .dbc:
#

ex.exportDbc(db, "test.dbc", dbcExportEncoding='iso-8859-1', dbcExportCommentEncoding='iso-8859-1')


@@ -278,15 +278,8 @@
 ame(
-0x123, 
 %22tes
@@ -287,19 +287,48 @@
 Frame1%22,
- 8,
+Id = 0x123, dlc=8, transmitter =
  %22testBU
@@ -1246,14 +1246,8 @@
 ame(
-0x12, 
 %22ext
@@ -1259,18 +1259,50 @@
 Frame%22, 
-8,
+Id = 0x12,  dlc = 8, transmitter =
  %22testBU

upgrade to new api