Print.h and Print.cpp usefulness

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
822
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Visit site
Activity points
6,533
Please let me know that I am some changing in Print.h and Print.cpp, make another files

name SevSEG.h and SevSEG.cpp for displaying values on 3 digit 7-segment display by using

print function with Arduino UNO.

But some errors during compiling like Test:19: error: cannot declare variable 'Ss' to be of abstract type 'SevSEG'
E:\Arduino IDE\arduino-1.0.6\libraries\SevSEG/SevSEG.h:36: note: because the following virtual functions are pure within 'SevSEG':
E:\Arduino IDE\arduino-1.0.6\libraries\SevSEG/SevSEG.h:44: note: virtual size_t SevSEG::write(uint8_t).


but what can I do for fix this problem or how to make it proper?

Header File:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Arduino.h"
#include "Print.h"
#include "SevSEG.h"

// Public Methods //////////////////////////////////////////////////////////////

/* default implementation: may be overridden */
size_t SevSEG::write(const uint8_t *buffer, size_t size)
{
  size_t n = 0;
  while (size--) {
    n += write(*buffer++);
  }
  return n;
}

size_t SevSEG::print(const __FlashStringHelper *ifsh)
{
  PGM_P p = reinterpret_cast<PGM_P>(ifsh);
  size_t n = 0;
  while (1) {
    unsigned char c = pgm_read_byte(p++);
    if (c == 0) break;
    n += write(c);
  }
  return n;
}

size_t SevSEG::print(const String &s)
{
  return write(s.c_str(), s.length());
}

size_t SevSEG::print(const char str[])
{
  return write(str);
}

size_t SevSEG::print(char c)
{
  return write(c);
}

size_t SevSEG::print(unsigned char b, int base)
{
  return print((unsigned long) b, base);
}

size_t SevSEG::print(int n, int base)
{
  return print((long) n, base);
}

size_t SevSEG::print(unsigned int n, int base)
{
  return print((unsigned long) n, base);
}

size_t SevSEG::print(long n, int base)
{
  if (base == 0) {
    return write(n);
  } else if (base == 10) {
    if (n < 0) {
      int t = print('-');
      n = -n;
      return printNumber(n, 10) + t;
    }
    return printNumber(n, 10);
  } else {
    return printNumber(n, base);
  }
}

size_t SevSEG::print(unsigned long n, int base)
{
  if (base == 0) return write(n);
  else return printNumber(n, base);
}

size_t SevSEG::print(double n, int digits)
{
  return printFloat(n, digits);
}

size_t SevSEG::println(const __FlashStringHelper *ifsh)
{
  size_t n = print(ifsh);
  n += println();
  return n;
}

size_t SevSEG::print(const Printable& x)
{
  return x.printTo(*this);
}

size_t SevSEG::println(void)
{
  size_t n = print('\r');
  n += print('\n');
  return n;
}

size_t SevSEG::println(const String &s)
{
  size_t n = print(s);
  n += println();
  return n;
}

size_t SevSEG::println(const char c[])
{
  size_t n = print(c);
  n += println();
  return n;
}

size_t SevSEG::println(char c)
{
  size_t n = print(c);
  n += println();
  return n;
}

size_t SevSEG::println(unsigned char b, int base)
{
  size_t n = print(b, base);
  n += println();
  return n;
}

size_t SevSEG::println(int num, int base)
{
  size_t n = print(num, base);
  n += println();
  return n;
}

size_t SevSEG::println(unsigned int num, int base)
{
  size_t n = print(num, base);
  n += println();
  return n;
}

size_t SevSEG::println(long num, int base)
{
  size_t n = print(num, base);
  n += println();
  return n;
}

size_t SevSEG::println(unsigned long num, int base)
{
  size_t n = print(num, base);
  n += println();
  return n;
}

size_t SevSEG::println(double num, int digits)
{
  size_t n = print(num, digits);
  n += println();
  return n;
}

size_t SevSEG::println(const Printable& x)
{
  size_t n = print(x);
  n += println();
  return n;
}

// Private Methods /////////////////////////////////////////////////////////////

size_t SevSEG::printNumber(unsigned long n, uint8_t base) {
  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  char *str = &buf[sizeof(buf) - 1];

  *str = '\0';

  // prevent crash if called with base == 1
  if (base < 2) base = 10;

  do {
    unsigned long m = n;
    n /= base;
    char c = m - base * n;
    *--str = c < 10 ? c + '0' : c + 'A' - 10;
  } while(n);

  return write(str);
}

size_t SevSEG::printFloat(double number, uint8_t digits) 
{ 
  size_t n = 0;
  
  if (isnan(number)) return print("nan");
  if (isinf(number)) return print("inf");
  if (number > 4294967040.0) return print ("ovf");  // constant determined empirically
  if (number <-4294967040.0) return print ("ovf");  // constant determined empirically
  
  // Handle negative numbers
  if (number < 0.0)
  {
     n += print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding /= 10.0;
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  n += print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0) {
    n += print("."); 
  }

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    n += print(toPrint);
    remainder -= toPrint; 
  } 
  
  return n;
}

CPP File:
Code:
#ifndef SevSEG_h
#define SevSEG_h

#include <inttypes.h>
#include <stdio.h> // for size_t

#include "WString.h"
#include "Printable.h"
#include "Print.h"

#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2

class SevSEG : public Print
{
  private:
   
    size_t printNumber(unsigned long, uint8_t);
    size_t printFloat(double, uint8_t);
  
  public:
    
    virtual size_t write(uint8_t) = 0;
    virtual size_t write(const char *str);
    virtual size_t write(const uint8_t *buffer, size_t size);
    
    size_t print(const __FlashStringHelper *);
    size_t print(const String &);
    size_t print(const char[]);
    size_t print(char);
    size_t print(unsigned char, int = DEC);
    size_t print(int, int = DEC);
    size_t print(unsigned int, int = DEC);
    size_t print(long, int = DEC);
    size_t print(unsigned long, int = DEC);
    size_t print(double, int = 2);
    size_t print(const Printable&);

    size_t println(const __FlashStringHelper *);
    size_t println(const String &s);
    size_t println(const char[]);
    size_t println(char);
    size_t println(unsigned char, int = DEC);
    size_t println(int, int = DEC);
    size_t println(unsigned int, int = DEC);
    size_t println(long, int = DEC);
    size_t println(unsigned long, int = DEC);
    size_t println(double, int = 2);
    size_t println(const Printable&);
    size_t println(void);
};

#endif
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…