Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Thu Oct 11 02:27:46 2018 +0000
Revision:
3:f3764f852aa8
Parent:
0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 """
kadonotakashi 0:8fdf9a60065b 2 mbed SDK
kadonotakashi 0:8fdf9a60065b 3 Copyright (c) 2011-2014 ARM Limited
kadonotakashi 0:8fdf9a60065b 4
kadonotakashi 0:8fdf9a60065b 5 Licensed under the Apache License, Version 2.0 (the "License");
kadonotakashi 0:8fdf9a60065b 6 you may not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 7 You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 8
kadonotakashi 0:8fdf9a60065b 9 http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 10
kadonotakashi 0:8fdf9a60065b 11 Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 12 distributed under the License is distributed on an "AS IS" BASIS,
kadonotakashi 0:8fdf9a60065b 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 14 See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 15 limitations under the License.
kadonotakashi 0:8fdf9a60065b 16
kadonotakashi 0:8fdf9a60065b 17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
kadonotakashi 0:8fdf9a60065b 18 """
kadonotakashi 0:8fdf9a60065b 19
kadonotakashi 0:8fdf9a60065b 20 import re
kadonotakashi 0:8fdf9a60065b 21 import MySQLdb as mdb
kadonotakashi 0:8fdf9a60065b 22
kadonotakashi 0:8fdf9a60065b 23 # Imports from TEST API
kadonotakashi 0:8fdf9a60065b 24 from tools.test_db import BaseDBAccess
kadonotakashi 0:8fdf9a60065b 25
kadonotakashi 0:8fdf9a60065b 26
kadonotakashi 0:8fdf9a60065b 27 class MySQLDBAccess(BaseDBAccess):
kadonotakashi 0:8fdf9a60065b 28 """ Wrapper for MySQL DB access for common test suite interface
kadonotakashi 0:8fdf9a60065b 29 """
kadonotakashi 0:8fdf9a60065b 30 def __init__(self):
kadonotakashi 0:8fdf9a60065b 31 BaseDBAccess.__init__(self)
kadonotakashi 0:8fdf9a60065b 32 self.DB_TYPE = 'mysql'
kadonotakashi 0:8fdf9a60065b 33
kadonotakashi 0:8fdf9a60065b 34 def detect_database(self, verbose=False):
kadonotakashi 0:8fdf9a60065b 35 """ detect database and return VERION data structure or string (verbose=True)
kadonotakashi 0:8fdf9a60065b 36 """
kadonotakashi 0:8fdf9a60065b 37 query = 'SHOW VARIABLES LIKE "%version%"'
kadonotakashi 0:8fdf9a60065b 38 rows = self.select_all(query)
kadonotakashi 0:8fdf9a60065b 39 if verbose:
kadonotakashi 0:8fdf9a60065b 40 result = []
kadonotakashi 0:8fdf9a60065b 41 for row in rows:
kadonotakashi 0:8fdf9a60065b 42 result.append("\t%s: %s"% (row['Variable_name'], row['Value']))
kadonotakashi 0:8fdf9a60065b 43 result = "\n".join(result)
kadonotakashi 0:8fdf9a60065b 44 else:
kadonotakashi 0:8fdf9a60065b 45 result = rows
kadonotakashi 0:8fdf9a60065b 46 return result
kadonotakashi 0:8fdf9a60065b 47
kadonotakashi 0:8fdf9a60065b 48 def parse_db_connection_string(self, str):
kadonotakashi 0:8fdf9a60065b 49 """ Parsing SQL DB connection string. String should contain:
kadonotakashi 0:8fdf9a60065b 50 - DB Name, user name, password, URL (DB host), name
kadonotakashi 0:8fdf9a60065b 51 Function should return tuple with parsed (host, user, passwd, db) or None if error
kadonotakashi 0:8fdf9a60065b 52 E.g. connection string: 'mysql://username:password@127.0.0.1/db_name'
kadonotakashi 0:8fdf9a60065b 53 """
kadonotakashi 0:8fdf9a60065b 54 result = BaseDBAccess().parse_db_connection_string(str)
kadonotakashi 0:8fdf9a60065b 55 if result is not None:
kadonotakashi 0:8fdf9a60065b 56 (db_type, username, password, host, db_name) = result
kadonotakashi 0:8fdf9a60065b 57 if db_type != 'mysql':
kadonotakashi 0:8fdf9a60065b 58 result = None
kadonotakashi 0:8fdf9a60065b 59 return result
kadonotakashi 0:8fdf9a60065b 60
kadonotakashi 0:8fdf9a60065b 61 def is_connected(self):
kadonotakashi 0:8fdf9a60065b 62 """ Returns True if we are connected to database
kadonotakashi 0:8fdf9a60065b 63 """
kadonotakashi 0:8fdf9a60065b 64 return self.db_object is not None
kadonotakashi 0:8fdf9a60065b 65
kadonotakashi 0:8fdf9a60065b 66 def connect(self, host, user, passwd, db):
kadonotakashi 0:8fdf9a60065b 67 """ Connects to DB and returns DB object
kadonotakashi 0:8fdf9a60065b 68 """
kadonotakashi 0:8fdf9a60065b 69 try:
kadonotakashi 0:8fdf9a60065b 70 self.db_object = mdb.connect(host=host, user=user, passwd=passwd, db=db)
kadonotakashi 0:8fdf9a60065b 71 # Let's remember connection credentials
kadonotakashi 0:8fdf9a60065b 72 self.db_type = self.DB_TYPE
kadonotakashi 0:8fdf9a60065b 73 self.host = host
kadonotakashi 0:8fdf9a60065b 74 self.user = user
kadonotakashi 0:8fdf9a60065b 75 self.passwd = passwd
kadonotakashi 0:8fdf9a60065b 76 self.db = db
kadonotakashi 0:8fdf9a60065b 77 except mdb.Error, e:
kadonotakashi 0:8fdf9a60065b 78 print "Error %d: %s"% (e.args[0], e.args[1])
kadonotakashi 0:8fdf9a60065b 79 self.db_object = None
kadonotakashi 0:8fdf9a60065b 80 self.db_type = None
kadonotakashi 0:8fdf9a60065b 81 self.host = None
kadonotakashi 0:8fdf9a60065b 82 self.user = None
kadonotakashi 0:8fdf9a60065b 83 self.passwd = None
kadonotakashi 0:8fdf9a60065b 84 self.db = None
kadonotakashi 0:8fdf9a60065b 85
kadonotakashi 0:8fdf9a60065b 86 def connect_url(self, db_url):
kadonotakashi 0:8fdf9a60065b 87 """ Connects to database using db_url (database url parsing),
kadonotakashi 0:8fdf9a60065b 88 store host, username, password, db_name
kadonotakashi 0:8fdf9a60065b 89 """
kadonotakashi 0:8fdf9a60065b 90 result = self.parse_db_connection_string(db_url)
kadonotakashi 0:8fdf9a60065b 91 if result is not None:
kadonotakashi 0:8fdf9a60065b 92 (db_type, username, password, host, db_name) = result
kadonotakashi 0:8fdf9a60065b 93 if db_type == self.DB_TYPE:
kadonotakashi 0:8fdf9a60065b 94 self.connect(host, username, password, db_name)
kadonotakashi 0:8fdf9a60065b 95
kadonotakashi 0:8fdf9a60065b 96 def reconnect(self):
kadonotakashi 0:8fdf9a60065b 97 """ Reconnects to DB and returns DB object using stored host name,
kadonotakashi 0:8fdf9a60065b 98 database name and credentials (user name and password)
kadonotakashi 0:8fdf9a60065b 99 """
kadonotakashi 0:8fdf9a60065b 100 self.connect(self.host, self.user, self.passwd, self.db)
kadonotakashi 0:8fdf9a60065b 101
kadonotakashi 0:8fdf9a60065b 102 def disconnect(self):
kadonotakashi 0:8fdf9a60065b 103 """ Close DB connection
kadonotakashi 0:8fdf9a60065b 104 """
kadonotakashi 0:8fdf9a60065b 105 if self.db_object:
kadonotakashi 0:8fdf9a60065b 106 self.db_object.close()
kadonotakashi 0:8fdf9a60065b 107 self.db_object = None
kadonotakashi 0:8fdf9a60065b 108 self.db_type = None
kadonotakashi 0:8fdf9a60065b 109
kadonotakashi 0:8fdf9a60065b 110 def escape_string(self, str):
kadonotakashi 0:8fdf9a60065b 111 """ Escapes string so it can be put in SQL query between quotes
kadonotakashi 0:8fdf9a60065b 112 """
kadonotakashi 0:8fdf9a60065b 113 con = self.db_object
kadonotakashi 0:8fdf9a60065b 114 result = con.escape_string(str)
kadonotakashi 0:8fdf9a60065b 115 return result if result else ''
kadonotakashi 0:8fdf9a60065b 116
kadonotakashi 0:8fdf9a60065b 117 def select_all(self, query):
kadonotakashi 0:8fdf9a60065b 118 """ Execute SELECT query and get all results
kadonotakashi 0:8fdf9a60065b 119 """
kadonotakashi 0:8fdf9a60065b 120 con = self.db_object
kadonotakashi 0:8fdf9a60065b 121 cur = con.cursor(mdb.cursors.DictCursor)
kadonotakashi 0:8fdf9a60065b 122 cur.execute(query)
kadonotakashi 0:8fdf9a60065b 123 rows = cur.fetchall()
kadonotakashi 0:8fdf9a60065b 124 return rows
kadonotakashi 0:8fdf9a60065b 125
kadonotakashi 0:8fdf9a60065b 126 def insert(self, query, commit=True):
kadonotakashi 0:8fdf9a60065b 127 """ Execute INSERT query, define if you want to commit
kadonotakashi 0:8fdf9a60065b 128 """
kadonotakashi 0:8fdf9a60065b 129 con = self.db_object
kadonotakashi 0:8fdf9a60065b 130 cur = con.cursor()
kadonotakashi 0:8fdf9a60065b 131 cur.execute(query)
kadonotakashi 0:8fdf9a60065b 132 if commit:
kadonotakashi 0:8fdf9a60065b 133 con.commit()
kadonotakashi 0:8fdf9a60065b 134 return cur.lastrowid
kadonotakashi 0:8fdf9a60065b 135
kadonotakashi 0:8fdf9a60065b 136 def get_next_build_id(self, name, desc='', location='', type=None, status=None):
kadonotakashi 0:8fdf9a60065b 137 """ Insert new build_id (DB unique build like ID number to send all test results)
kadonotakashi 0:8fdf9a60065b 138 """
kadonotakashi 0:8fdf9a60065b 139 if status is None:
kadonotakashi 0:8fdf9a60065b 140 status = self.BUILD_ID_STATUS_STARTED
kadonotakashi 0:8fdf9a60065b 141
kadonotakashi 0:8fdf9a60065b 142 if type is None:
kadonotakashi 0:8fdf9a60065b 143 type = self.BUILD_ID_TYPE_TEST
kadonotakashi 0:8fdf9a60065b 144
kadonotakashi 0:8fdf9a60065b 145 query = """INSERT INTO `%s` (%s_name, %s_desc, %s_location, %s_type_fk, %s_status_fk)
kadonotakashi 0:8fdf9a60065b 146 VALUES ('%s', '%s', '%s', %d, %d)"""% (self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 147 self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 148 self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 149 self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 150 self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 151 self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 152 self.escape_string(name),
kadonotakashi 0:8fdf9a60065b 153 self.escape_string(desc),
kadonotakashi 0:8fdf9a60065b 154 self.escape_string(location),
kadonotakashi 0:8fdf9a60065b 155 type,
kadonotakashi 0:8fdf9a60065b 156 status)
kadonotakashi 0:8fdf9a60065b 157 index = self.insert(query) # Provide inserted record PK
kadonotakashi 0:8fdf9a60065b 158 return index
kadonotakashi 0:8fdf9a60065b 159
kadonotakashi 0:8fdf9a60065b 160 def get_table_entry_pk(self, table, column, value, update_db=True):
kadonotakashi 0:8fdf9a60065b 161 """ Checks for entries in tables with two columns (<TABLE_NAME>_pk, <column>)
kadonotakashi 0:8fdf9a60065b 162 If update_db is True updates table entry if value in specified column doesn't exist
kadonotakashi 0:8fdf9a60065b 163 """
kadonotakashi 0:8fdf9a60065b 164 # TODO: table buffering
kadonotakashi 0:8fdf9a60065b 165 result = None
kadonotakashi 0:8fdf9a60065b 166 table_pk = '%s_pk'% table
kadonotakashi 0:8fdf9a60065b 167 query = """SELECT `%s`
kadonotakashi 0:8fdf9a60065b 168 FROM `%s`
kadonotakashi 0:8fdf9a60065b 169 WHERE `%s`='%s'"""% (table_pk,
kadonotakashi 0:8fdf9a60065b 170 table,
kadonotakashi 0:8fdf9a60065b 171 column,
kadonotakashi 0:8fdf9a60065b 172 self.escape_string(value))
kadonotakashi 0:8fdf9a60065b 173 rows = self.select_all(query)
kadonotakashi 0:8fdf9a60065b 174 if len(rows) == 1:
kadonotakashi 0:8fdf9a60065b 175 result = rows[0][table_pk]
kadonotakashi 0:8fdf9a60065b 176 elif len(rows) == 0 and update_db:
kadonotakashi 0:8fdf9a60065b 177 # Update DB with new value
kadonotakashi 0:8fdf9a60065b 178 result = self.update_table_entry(table, column, value)
kadonotakashi 0:8fdf9a60065b 179 return result
kadonotakashi 0:8fdf9a60065b 180
kadonotakashi 0:8fdf9a60065b 181 def update_table_entry(self, table, column, value):
kadonotakashi 0:8fdf9a60065b 182 """ Updates table entry if value in specified column doesn't exist
kadonotakashi 0:8fdf9a60065b 183 Locks table to perform atomic read + update
kadonotakashi 0:8fdf9a60065b 184 """
kadonotakashi 0:8fdf9a60065b 185 result = None
kadonotakashi 0:8fdf9a60065b 186 con = self.db_object
kadonotakashi 0:8fdf9a60065b 187 cur = con.cursor()
kadonotakashi 0:8fdf9a60065b 188 cur.execute("LOCK TABLES `%s` WRITE"% table)
kadonotakashi 0:8fdf9a60065b 189 table_pk = '%s_pk'% table
kadonotakashi 0:8fdf9a60065b 190 query = """SELECT `%s`
kadonotakashi 0:8fdf9a60065b 191 FROM `%s`
kadonotakashi 0:8fdf9a60065b 192 WHERE `%s`='%s'"""% (table_pk,
kadonotakashi 0:8fdf9a60065b 193 table,
kadonotakashi 0:8fdf9a60065b 194 column,
kadonotakashi 0:8fdf9a60065b 195 self.escape_string(value))
kadonotakashi 0:8fdf9a60065b 196 cur.execute(query)
kadonotakashi 0:8fdf9a60065b 197 rows = cur.fetchall()
kadonotakashi 0:8fdf9a60065b 198 if len(rows) == 0:
kadonotakashi 0:8fdf9a60065b 199 query = """INSERT INTO `%s` (%s)
kadonotakashi 0:8fdf9a60065b 200 VALUES ('%s')"""% (table,
kadonotakashi 0:8fdf9a60065b 201 column,
kadonotakashi 0:8fdf9a60065b 202 self.escape_string(value))
kadonotakashi 0:8fdf9a60065b 203 cur.execute(query)
kadonotakashi 0:8fdf9a60065b 204 result = cur.lastrowid
kadonotakashi 0:8fdf9a60065b 205 con.commit()
kadonotakashi 0:8fdf9a60065b 206 cur.execute("UNLOCK TABLES")
kadonotakashi 0:8fdf9a60065b 207 return result
kadonotakashi 0:8fdf9a60065b 208
kadonotakashi 0:8fdf9a60065b 209 def update_build_id_info(self, build_id, **kw):
kadonotakashi 0:8fdf9a60065b 210 """ Update additional data inside build_id table
kadonotakashi 0:8fdf9a60065b 211 Examples:
kadonotakashi 0:8fdf9a60065b 212 db.update_build_id_info(build_id, _status_fk=self.BUILD_ID_STATUS_COMPLETED, _shuffle_seed=0.0123456789):
kadonotakashi 0:8fdf9a60065b 213 """
kadonotakashi 0:8fdf9a60065b 214 if len(kw):
kadonotakashi 0:8fdf9a60065b 215 con = self.db_object
kadonotakashi 0:8fdf9a60065b 216 cur = con.cursor()
kadonotakashi 0:8fdf9a60065b 217 # Prepare UPDATE query
kadonotakashi 0:8fdf9a60065b 218 # ["`mtest_build_id_pk`=[value-1]", "`mtest_build_id_name`=[value-2]", "`mtest_build_id_desc`=[value-3]"]
kadonotakashi 0:8fdf9a60065b 219 set_list = []
kadonotakashi 0:8fdf9a60065b 220 for col_sufix in kw:
kadonotakashi 0:8fdf9a60065b 221 assign_str = "`%s%s`='%s'"% (self.TABLE_BUILD_ID, col_sufix, self.escape_string(str(kw[col_sufix])))
kadonotakashi 0:8fdf9a60065b 222 set_list.append(assign_str)
kadonotakashi 0:8fdf9a60065b 223 set_str = ', '.join(set_list)
kadonotakashi 0:8fdf9a60065b 224 query = """UPDATE `%s`
kadonotakashi 0:8fdf9a60065b 225 SET %s
kadonotakashi 0:8fdf9a60065b 226 WHERE `mtest_build_id_pk`=%d"""% (self.TABLE_BUILD_ID,
kadonotakashi 0:8fdf9a60065b 227 set_str,
kadonotakashi 0:8fdf9a60065b 228 build_id)
kadonotakashi 0:8fdf9a60065b 229 cur.execute(query)
kadonotakashi 0:8fdf9a60065b 230 con.commit()
kadonotakashi 0:8fdf9a60065b 231
kadonotakashi 0:8fdf9a60065b 232 def insert_test_entry(self, build_id, target, toolchain, test_type, test_id, test_result, test_output, test_time, test_timeout, test_loop, test_extra=''):
kadonotakashi 0:8fdf9a60065b 233 """ Inserts test result entry to database. All checks regarding existing
kadonotakashi 0:8fdf9a60065b 234 toolchain names in DB are performed.
kadonotakashi 0:8fdf9a60065b 235 If some data is missing DB will be updated
kadonotakashi 0:8fdf9a60065b 236 """
kadonotakashi 0:8fdf9a60065b 237 # Get all table FK and if entry is new try to insert new value
kadonotakashi 0:8fdf9a60065b 238 target_fk = self.get_table_entry_pk(self.TABLE_TARGET, self.TABLE_TARGET + '_name', target)
kadonotakashi 0:8fdf9a60065b 239 toolchain_fk = self.get_table_entry_pk(self.TABLE_TOOLCHAIN, self.TABLE_TOOLCHAIN + '_name', toolchain)
kadonotakashi 0:8fdf9a60065b 240 test_type_fk = self.get_table_entry_pk(self.TABLE_TEST_TYPE, self.TABLE_TEST_TYPE + '_name', test_type)
kadonotakashi 0:8fdf9a60065b 241 test_id_fk = self.get_table_entry_pk(self.TABLE_TEST_ID, self.TABLE_TEST_ID + '_name', test_id)
kadonotakashi 0:8fdf9a60065b 242 test_result_fk = self.get_table_entry_pk(self.TABLE_TEST_RESULT, self.TABLE_TEST_RESULT + '_name', test_result)
kadonotakashi 0:8fdf9a60065b 243
kadonotakashi 0:8fdf9a60065b 244 con = self.db_object
kadonotakashi 0:8fdf9a60065b 245 cur = con.cursor()
kadonotakashi 0:8fdf9a60065b 246
kadonotakashi 0:8fdf9a60065b 247 query = """ INSERT INTO `%s` (`mtest_build_id_fk`,
kadonotakashi 0:8fdf9a60065b 248 `mtest_target_fk`,
kadonotakashi 0:8fdf9a60065b 249 `mtest_toolchain_fk`,
kadonotakashi 0:8fdf9a60065b 250 `mtest_test_type_fk`,
kadonotakashi 0:8fdf9a60065b 251 `mtest_test_id_fk`,
kadonotakashi 0:8fdf9a60065b 252 `mtest_test_result_fk`,
kadonotakashi 0:8fdf9a60065b 253 `mtest_test_output`,
kadonotakashi 0:8fdf9a60065b 254 `mtest_test_time`,
kadonotakashi 0:8fdf9a60065b 255 `mtest_test_timeout`,
kadonotakashi 0:8fdf9a60065b 256 `mtest_test_loop_no`,
kadonotakashi 0:8fdf9a60065b 257 `mtest_test_result_extra`)
kadonotakashi 0:8fdf9a60065b 258 VALUES (%d, %d, %d, %d, %d, %d, '%s', %.2f, %.2f, %d, '%s')"""% (self.TABLE_TEST_ENTRY,
kadonotakashi 0:8fdf9a60065b 259 build_id,
kadonotakashi 0:8fdf9a60065b 260 target_fk,
kadonotakashi 0:8fdf9a60065b 261 toolchain_fk,
kadonotakashi 0:8fdf9a60065b 262 test_type_fk,
kadonotakashi 0:8fdf9a60065b 263 test_id_fk,
kadonotakashi 0:8fdf9a60065b 264 test_result_fk,
kadonotakashi 0:8fdf9a60065b 265 self.escape_string(test_output),
kadonotakashi 0:8fdf9a60065b 266 test_time,
kadonotakashi 0:8fdf9a60065b 267 test_timeout,
kadonotakashi 0:8fdf9a60065b 268 test_loop,
kadonotakashi 0:8fdf9a60065b 269 self.escape_string(test_extra))
kadonotakashi 0:8fdf9a60065b 270 cur.execute(query)
kadonotakashi 0:8fdf9a60065b 271 con.commit()