SAA1064 4 Digit Library

SAA1064 4 Digit Library for SMD IoTKit Shield

Committer:
marcel1691
Date:
Mon Apr 20 07:36:41 2015 +0000
Revision:
1:e6f764285b44
Parent:
SAA1064/SAA1064.cpp@0:8b1f83a2ae0a
File location

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcel1691 0:8b1f83a2ae0a 1 /* SAA1064 4 Digit Library
marcel1691 0:8b1f83a2ae0a 2 * Copyright (c) 2015 Marcel (mc-b) Bernet
marcel1691 0:8b1f83a2ae0a 3 *
marcel1691 0:8b1f83a2ae0a 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcel1691 0:8b1f83a2ae0a 5 * you may not use this file except in compliance with the License.
marcel1691 0:8b1f83a2ae0a 6 * You may obtain a copy of the License at
marcel1691 0:8b1f83a2ae0a 7 *
marcel1691 0:8b1f83a2ae0a 8 * http://www.apache.org/licenses/LICENSE-2.0
marcel1691 0:8b1f83a2ae0a 9 *
marcel1691 0:8b1f83a2ae0a 10 * Unless required by applicable law or agreed to in writing, software
marcel1691 0:8b1f83a2ae0a 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcel1691 0:8b1f83a2ae0a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcel1691 0:8b1f83a2ae0a 13 * See the License for the specific language governing permissions and
marcel1691 0:8b1f83a2ae0a 14 * limitations under the License.
marcel1691 0:8b1f83a2ae0a 15 */
marcel1691 0:8b1f83a2ae0a 16
marcel1691 0:8b1f83a2ae0a 17 #include "mbed.h"
marcel1691 0:8b1f83a2ae0a 18 #include "SAA1064.h"
marcel1691 0:8b1f83a2ae0a 19
marcel1691 0:8b1f83a2ae0a 20 SAA1064::SAA1064( PinName sda, PinName scl, uint8_t deviceAddress )
marcel1691 0:8b1f83a2ae0a 21 {
marcel1691 0:8b1f83a2ae0a 22 i2c = new I2C( sda, scl );
marcel1691 0:8b1f83a2ae0a 23 slaveAddress = deviceAddress;
marcel1691 0:8b1f83a2ae0a 24 init();
marcel1691 0:8b1f83a2ae0a 25 }
marcel1691 0:8b1f83a2ae0a 26
marcel1691 0:8b1f83a2ae0a 27
marcel1691 0:8b1f83a2ae0a 28 SAA1064::SAA1064( I2C *i2c, uint8_t deviceAddress )
marcel1691 0:8b1f83a2ae0a 29 {
marcel1691 0:8b1f83a2ae0a 30 this->i2c = i2c;
marcel1691 0:8b1f83a2ae0a 31 slaveAddress = deviceAddress;
marcel1691 0:8b1f83a2ae0a 32 init();
marcel1691 0:8b1f83a2ae0a 33 }
marcel1691 0:8b1f83a2ae0a 34
marcel1691 0:8b1f83a2ae0a 35 /** Write digits
marcel1691 0:8b1f83a2ae0a 36 *
marcel1691 0:8b1f83a2ae0a 37 * @param digit1 LED segment pattern for digit1 (MSB)
marcel1691 0:8b1f83a2ae0a 38 * @param digit2 LED segment pattern for digit2
marcel1691 0:8b1f83a2ae0a 39 * @param digit3 LED segment pattern for digit3
marcel1691 0:8b1f83a2ae0a 40 * @param digit4 LED segment pattern for digit4 (LSB)
marcel1691 0:8b1f83a2ae0a 41 */
marcel1691 0:8b1f83a2ae0a 42 void SAA1064::write( uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4 )
marcel1691 0:8b1f83a2ae0a 43 {
marcel1691 0:8b1f83a2ae0a 44 digit1 = SAA1064_SEGM[digit1];
marcel1691 0:8b1f83a2ae0a 45 digit2 = SAA1064_SEGM[digit2];
marcel1691 0:8b1f83a2ae0a 46 digit3 = SAA1064_SEGM[digit3];
marcel1691 0:8b1f83a2ae0a 47 digit4 = SAA1064_SEGM[digit4];
marcel1691 0:8b1f83a2ae0a 48
marcel1691 0:8b1f83a2ae0a 49 data[0] = 1;
marcel1691 0:8b1f83a2ae0a 50 data[1] = ((digit4<<4) & 0xF0) | (digit2 & 0x0F);
marcel1691 0:8b1f83a2ae0a 51 data[2] = ((digit3<<4) & 0xF0) | (digit1 & 0x0F);
marcel1691 0:8b1f83a2ae0a 52 data[3] = ((digit2>>4) & 0x0F) | (digit4 & 0xF0);
marcel1691 0:8b1f83a2ae0a 53 data[4] = ((digit1>>4) & 0x0F) | (digit3 & 0xF0);
marcel1691 0:8b1f83a2ae0a 54 i2c->write( slaveAddress, (char*) data, 5 );
marcel1691 0:8b1f83a2ae0a 55 }
marcel1691 0:8b1f83a2ae0a 56
marcel1691 0:8b1f83a2ae0a 57 /** Write Integer
marcel1691 0:8b1f83a2ae0a 58 *
marcel1691 0:8b1f83a2ae0a 59 * @param value integer value to display, valid range -999...9999
marcel1691 0:8b1f83a2ae0a 60 */
marcel1691 0:8b1f83a2ae0a 61 void SAA1064::writeInt( int value )
marcel1691 0:8b1f83a2ae0a 62 {
marcel1691 0:8b1f83a2ae0a 63 write( value / 1000, (value % 1000) / 100, (value % 100) / 10, value % 10 );
marcel1691 0:8b1f83a2ae0a 64 }
marcel1691 0:8b1f83a2ae0a 65
marcel1691 0:8b1f83a2ae0a 66 /** Init I2C bus
marcel1691 0:8b1f83a2ae0a 67 */
marcel1691 0:8b1f83a2ae0a 68 void SAA1064::init( void )
marcel1691 0:8b1f83a2ae0a 69 {
marcel1691 0:8b1f83a2ae0a 70 // init
marcel1691 0:8b1f83a2ae0a 71 data[0] = 0x00;
marcel1691 0:8b1f83a2ae0a 72 data[1] = 0x47;
marcel1691 0:8b1f83a2ae0a 73 i2c->write( slaveAddress, (char*) data, 2 );
marcel1691 0:8b1f83a2ae0a 74
marcel1691 0:8b1f83a2ae0a 75 // blank display
marcel1691 0:8b1f83a2ae0a 76 data[0] = 1;
marcel1691 0:8b1f83a2ae0a 77 data[1] = 0;
marcel1691 0:8b1f83a2ae0a 78 data[2] = 0;
marcel1691 0:8b1f83a2ae0a 79 data[3] = 0;
marcel1691 0:8b1f83a2ae0a 80 data[4] = 0;
marcel1691 0:8b1f83a2ae0a 81 i2c->write( slaveAddress, (char*) data, 5 );
marcel1691 0:8b1f83a2ae0a 82 }