venerdì 28 agosto 2015

Arduino e CH376S (2)

Questa e' un miglioramento di quanto visto al precedente post (e decisamente il circuito piu' complicato che abbia mai fatto dopo un sistema di guida di un ROV sottomarino)
L'idea e' che quando viene inserita la chiavetta, viene generato un file sulla chiavetta con all'interno le informazioni di ora e data e viene mandato un segnale acustico e luminoso dell'avvenuta scrittura


Nello schema Fritzing, dato che non ci sono tutti i componenti nella libreria, ho messo un buzzer passivo a due contatti al posto di un buzzer attivo a tre contatti e un RN-41 al posto di CH376S (tanto entrambi hanno una connessione seriale a 4 contatti Vcc/Gnd/Tx/Rx)


Nel dettaglio
D06 = DS1302 SCLK
D07 = DS1302 DATA IO
D08 = DS1302 CE RESET
D09 = Speaker Signal
D10 = CH376S TXD
D11 = CH376S RXD

D13 = led

tutte le alimentazioni a Vcc 5.0 V


i componenti sono
Arduino nano (cinese) : circa 2.5 euro
CH376S (cinese) : circa 3 euro
DS1302 (cinese) : circa 1 euro
buzzer attivo (cinese) : circa 0.5 euro
1 led rosso
1 resistenza 220 Ohm


Circuito di prova (non proprio ordinato) con una Arduino Uno al posto della Nano


il programma completo e' il seguente (circa 10 Kb compilato)
----------------------------------------------------------------------------------
#include <Wire.h>
#include <SoftwareSerial.h>

// lettore USB
byte computerByte;           //used to store data coming from the computer
byte USB_Byte;               //used to store data coming from the USB stick
int LED = 13;                //the LED is connected to digital pin 13 
int timeOut = 2000;          //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module.

SoftwareSerial USB(10, 11);                           // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module
                                                      // Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module
                                                      // GND on Arduino to GND on CH376S module
                                                      // 5V on Arduino to 5V on CH376S module
                                                      
int connessione;
int chiave_presente;

// DT1302
#define DS1302_SCLK_PIN   6    // Arduino pin for the Serial Clock
#define DS1302_IO_PIN     7    // Arduino pin for the Data I/O
#define DS1302_CE_PIN     8    // Arduino pin for the Chip Enable


#define bcd2bin(h,l)    (((h)*10) + (l))
#define bin2bcd_h(x)   ((x)/10)
#define bin2bcd_l(x)    ((x)%10)


#define DS1302_SECONDS           0x80
#define DS1302_MINUTES           0x82
#define DS1302_HOURS             0x84
#define DS1302_DATE              0x86
#define DS1302_MONTH             0x88
#define DS1302_DAY               0x8A
#define DS1302_YEAR              0x8C
#define DS1302_ENABLE            0x8E
#define DS1302_TRICKLE           0x90
#define DS1302_CLOCK_BURST       0xBE
#define DS1302_CLOCK_BURST_WRITE 0xBE
#define DS1302_CLOCK_BURST_READ  0xBF
#define DS1302_RAMSTART          0xC0
#define DS1302_RAMEND            0xFC
#define DS1302_RAM_BURST         0xFE
#define DS1302_RAM_BURST_WRITE   0xFE
#define DS1302_RAM_BURST_READ    0xFF



#define DS1302_D0 0
#define DS1302_D1 1
#define DS1302_D2 2
#define DS1302_D3 3
#define DS1302_D4 4
#define DS1302_D5 5
#define DS1302_D6 6
#define DS1302_D7 7

#define DS1302_READBIT DS1302_D0 // READBIT=1: read instruction
#define DS1302_RC DS1302_D6
#define DS1302_CH DS1302_D7   // 1 = Clock Halt, 0 = start
#define DS1302_AM_PM DS1302_D5 // 0 = AM, 1 = PM
#define DS1302_12_24 DS1302 D7 // 0 = 24 hour, 1 = 12 hour
#define DS1302_WP DS1302_D7   // 1 = Write Protect, 0 = enabled
#define DS1302_ROUT0 DS1302_D0
#define DS1302_ROUT1 DS1302_D1
#define DS1302_DS0   DS1302_D2
#define DS1302_DS1   DS1302_D2
#define DS1302_TCS0  DS1302_D4
#define DS1302_TCS1  DS1302_D5
#define DS1302_TCS2  DS1302_D6
#define DS1302_TCS3  DS1302_D7

typedef struct ds1302_struct
{
  uint8_t Seconds:4;      // low decimal digit 0-9
  uint8_t Seconds10:3;    // high decimal digit 0-5
  uint8_t CH:1;           // CH = Clock Halt
  uint8_t Minutes:4;
  uint8_t Minutes10:3;
  uint8_t reserved1:1;
  union
  {
    struct
    {
      uint8_t Hour:4;
      uint8_t Hour10:2;
      uint8_t reserved2:1;
      uint8_t hour_12_24:1; // 0 for 24 hour format
    } h24;
    struct
    {
      uint8_t Hour:4;
      uint8_t Hour10:1;
      uint8_t AM_PM:1;      // 0 for AM, 1 for PM
      uint8_t reserved2:1;
      uint8_t hour_12_24:1; // 1 for 12 hour format
    } h12;
  };
  uint8_t Date:4;           // Day of month, 1 = first day
  uint8_t Date10:2;
  uint8_t reserved3:2;
  uint8_t Month:4;          // Month, 1 = January
  uint8_t Month10:1;
  uint8_t reserved4:3;
  uint8_t Day:3;            // Day of week, 1 = first day (any day)
  uint8_t reserved5:5;
  uint8_t Year:4;           // Year, 0 = year 2000
  uint8_t Year10:4;
  uint8_t reserved6:7;
  uint8_t WP:1;             // WP = Write Protect
};

// Speaker
int speakerPin = 9;
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
// Fine speaker

void setup()
{      
  ds1302_struct rtc;
  
  pinMode(speakerPin, OUTPUT);
  
  Serial.begin(9600);
  Serial.println(F("Start Puncher"));
  //Serial.println(F("Version 2, March 2013"));
  
  USB.begin(9600);                                    // Setup serial communication with the CH376S module (using the default baud rate of 9600)
  pinMode(LED,OUTPUT);                                // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED
  digitalWrite(LED,LOW);                              // Turn off the LED
  
  
  
  DS1302_write (DS1302_ENABLE, 0);
  DS1302_write (DS1302_TRICKLE, 0x00);

// Remove the next define, 
// after the right date and time are set.
//#define SET_DATE_TIME_JUST_ONCE
#ifdef SET_DATE_TIME_JUST_ONCE  

  int seconds, minutes, hours, dayofweek, dayofmonth, month, year;
  seconds    = 0;
  minutes    = 07;
  hours      = 10;
  dayofweek  = 3;  // Day of week, any day can be first, counts 1...7
  dayofmonth = 26; // Day of month, 1...31
  month      = 8;  // month 1...12
  year       = 2015;

  memset ((char *) &rtc, 0, sizeof(rtc));

  rtc.Seconds    = bin2bcd_l( seconds);
  rtc.Seconds10  = bin2bcd_h( seconds);
  rtc.CH         = 0;      // 1 for Clock Halt, 0 to run;
  rtc.Minutes    = bin2bcd_l( minutes);
  rtc.Minutes10  = bin2bcd_h( minutes);
  rtc.h24.Hour   = bin2bcd_l( hours);
  rtc.h24.Hour10 = bin2bcd_h( hours);
  rtc.h24.hour_12_24 = 0; // 0 for 24 hour format
  rtc.Date       = bin2bcd_l( dayofmonth);
  rtc.Date10     = bin2bcd_h( dayofmonth);
  rtc.Month      = bin2bcd_l( month);
  rtc.Month10    = bin2bcd_h( month);
  rtc.Day        = dayofweek;
  rtc.Year       = bin2bcd_l( year - 2000);
  rtc.Year10     = bin2bcd_h( year - 2000);
  rtc.WP = 0;  
  DS1302_clock_burst_write( (uint8_t *) &rtc);
#endif
}


void loop()
{
  ds1302_struct rtc;
  char buffer[80];     // the code uses 70 characters.
  
  connessione = 0;
  chiave_presente = 0;
  checkConnection(0x01);
  if (connessione == 1)
    {
      Serial.println("Connessione Presente");
      set_USB_Mode(0x06);    
      if (chiave_presente == 1)
        {
        // prende il tempo dall'orologio  
        DS1302_clock_burst_read( (uint8_t *) &rtc);

        sprintf( buffer, "Time = %02d:%02d:%02d, ", \
        bcd2bin( rtc.h24.Hour10, rtc.h24.Hour), \
        bcd2bin( rtc.Minutes10, rtc.Minutes), \
        bcd2bin( rtc.Seconds10, rtc.Seconds));
        Serial.println(buffer);
        resetALL(); 
        //scrive il file
        writeFile("lanterna1.txt", buffer);         // emette il suono di conferma

        sprintf(buffer, "Date(day of month) = %d, Month = %d, " \
        "Day(day of week) = %d, Year = %d", \
        bcd2bin( rtc.Date10, rtc.Date), \
        bcd2bin( rtc.Month10, rtc.Month), \
        rtc.Day, \
        2000 + bcd2bin( rtc.Year10, rtc.Year));
        Serial.println( buffer);
        resetALL(); 
        //scrive il file
        appendFile("lanterna1.txt", buffer);         // emette il suono di conferma
       delay(1000);
       playTone(400,100);
       blinkLED();
       Serial.println("************FINE*************");
       }
  }
  
  if(USB.available()){                                 // This is here to capture any unexpected data transmitted by the CH376S module
    Serial.print("CH376S has just sent this code:");
    Serial.println(USB.read(), HEX);
  }  
  delay(5000);
}


void DS1302_clock_burst_read( uint8_t *p)
{
  int i;
  _DS1302_start();
  _DS1302_togglewrite( DS1302_CLOCK_BURST_READ, true);  
  for( i=0; i<8; i++)
  {
    *p++ = _DS1302_toggleread();
  }
  _DS1302_stop();
}


void DS1302_clock_burst_write( uint8_t *p)
{
  int i;
  _DS1302_start();
  _DS1302_togglewrite( DS1302_CLOCK_BURST_WRITE, false);  
  for( i=0; i<8; i++)
  {
    _DS1302_togglewrite( *p++, false);  
  }
  _DS1302_stop();
}

uint8_t DS1302_read(int address)
{
  uint8_t data;
  bitSet( address, DS1302_READBIT);  
  _DS1302_start();
  _DS1302_togglewrite( address, true);  
  data = _DS1302_toggleread();
  _DS1302_stop();
  return (data);
}

void DS1302_write( int address, uint8_t data)
{
  bitClear( address, DS1302_READBIT);   
  _DS1302_start();
  _DS1302_togglewrite( address, false); 
  _DS1302_togglewrite( data, false); 
  _DS1302_stop();  
}

void _DS1302_start( void)
{
  digitalWrite( DS1302_CE_PIN, LOW); // default, not enabled
  pinMode( DS1302_CE_PIN, OUTPUT);  
  digitalWrite( DS1302_SCLK_PIN, LOW); // default, clock low
  pinMode( DS1302_SCLK_PIN, OUTPUT);
  pinMode( DS1302_IO_PIN, OUTPUT);
  digitalWrite( DS1302_CE_PIN, HIGH); // start the session
  delayMicroseconds( 4);           // tCC = 4us
}


void _DS1302_stop(void)
{
  digitalWrite( DS1302_CE_PIN, LOW);
  delayMicroseconds( 4);           // tCWH = 4us
}


uint8_t _DS1302_toggleread( void)
{
  uint8_t i, data;
  data = 0;
  for( i = 0; i <= 7; i++)
  {
    digitalWrite( DS1302_SCLK_PIN, HIGH);
    delayMicroseconds( 1);
    digitalWrite( DS1302_SCLK_PIN, LOW);
    delayMicroseconds( 1);        // tCL=1000ns, tCDD=800ns
    bitWrite( data, i, digitalRead( DS1302_IO_PIN)); 
  }
  return( data);
}


void _DS1302_togglewrite( uint8_t data, uint8_t release)
{
  int i;
  for( i = 0; i <= 7; i++)
  { 
    digitalWrite( DS1302_IO_PIN, bitRead(data, i));  
    delayMicroseconds( 1);     // tDC = 200ns
    digitalWrite( DS1302_SCLK_PIN, HIGH);     
    delayMicroseconds( 1);     // tCH = 1000ns, tCDH = 800ns

    if( release && i == 7)
    {
      pinMode( DS1302_IO_PIN, INPUT);
    }
    else
    {
      digitalWrite( DS1302_SCLK_PIN, LOW);
      delayMicroseconds( 1);       // tCL=1000ns, tCDD=800ns
    }
  }
}


// funzioni USB
void checkConnection(byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x06);
  USB.write(value);
  
  if(waitForResponse("checking connection")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==(255-value)){
       //Serial.println(">Connection to CH376S was successful.");
       connessione = 1;
    } else {
      //Serial.print(">Connection to CH376S - FAILED.");
      connessione = 0;
    }
  }
}

void set_USB_Mode (byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x15);
  USB.write(value);
  
  delay(20);
  
  if(USB.available()){
    USB_Byte=USB.read();
    //Check to see if the command has been successfully transmitted and acknowledged.
    if(USB_Byte==0x51){                                   // If true - the CH376S has acknowledged the command.
        //Serial.println("set_USB_Mode command acknowledged"); //The CH376S will now check and monitor the USB port
        USB_Byte = USB.read();
        
        //Check to see if the USB stick is connected or not.
        if(USB_Byte==0x15){                               // If true - there is a USB stick connected
          //Serial.println("USB is present");
          chiave_presente = 1;
          blinkLED();                                     // If the process was successful, then turn the LED on for 1 second 
        } else {
          chiave_presente = 0;
          //Serial.print("USB Not present. Error code:");   // If the USB is not connected - it should return an Error code = FFH
          //Serial.print(USB_Byte, HEX);
          //Serial.println("H");
        }
        
    } else {
        //Serial.print("CH3765 error!   Error code:");
        //Serial.print(USB_Byte, HEX);
        //Serial.println("H");
        chiave_presente = 0;
    }   
  }
  delay(20);
}

//resetALL=========================================================================================
//This will perform a hardware reset of the CH376S module - which usually takes about 35 msecs =====
void resetALL(){
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x05);
    //Serial.println("The CH376S module has been reset !");
    delay(200);
}

void writeFile(String fileName, String data){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  if(fileCreate()){               //Try to create a new file. If file creation is successful
    fileWrite(data);              //write data to the file.
  } else {
    Serial.println("File could not be created, or it already exists");
  }
  fileClose(0x01);
}

  
void setFileName(String fileName){
  Serial.print("Setting filename to:");
  Serial.println(fileName);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x2F);
  USB.write(0x2F);         // Every filename must have this byte to indicate the start of the file name.
  USB.print(fileName);     // "fileName" is a variable that holds the name of the file.  eg. TEST.TXT
  USB.write((byte)0x00);   // you need to cast as a byte - otherwise it will not compile.  The null byte indicates the end of the file name.
  delay(20);
}

void diskConnectionStatus(){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x30);

  if(waitForResponse("Connecting to USB disk")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){               //CH376S will send 0x14 if this command was successful
    } else {
    }
  }
}

void USBdiskMount(){
  Serial.println("Mounting USB disk");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x31);

  if(waitForResponse("mounting USB disk")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){               //CH376S will send 0x14 if this command was successful
    } else {
    }
  }
}

void fileOpen(){
  Serial.println("Opening file.");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x32);
  if(waitForResponse("file Open")){                 //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){                 //CH376S will send 0x14 if this command was successful  
    } else {
    }
  }
}

//setByteRead=====================================================================================
//This function is required if you want to read data from the file. 
boolean setByteRead(byte numBytes){
  boolean bytesToRead=false;
  int timeCounter = 0;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3A);
  USB.write((byte)numBytes);   //tells the CH376S how many bytes to read at a time
  USB.write((byte)0x00);
  if(waitForResponse("setByteRead")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x1D){         //read the CH376S message. If equal to 0x1D, data is present, so return true. Will return 0x14 if no data is present.
      bytesToRead=true;
    }
  }
  return(bytesToRead);


//getFileSize()===================================================================================
//writes the file size to the serial Monitor.
int getFileSize(){
  int fileSize=0;
  Serial.println("Getting File Size");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x0C);
  USB.write(0x68);
  delay(100);
  Serial.print("FileSize =");
  if(USB.available()){
    fileSize = fileSize + USB.read();
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255*255);
  }     
  Serial.println(fileSize);
  delay(10);
  return(fileSize);
}


//fileRead========================================================================================
//read the contents of the file
void fileRead(){
  Serial.println("Reading file:");
  byte firstByte = 0x00;                     //Variable to hold the firstByte from every transmission.  Can be used as a checkSum if required.
  byte numBytes = 0x40;                      //The maximum value is 0x40  =  64 bytes

  while(setByteRead(numBytes)){              //This tells the CH376S module how many bytes to read on the next reading step. In this example, we will read 0x10 bytes at a time. Returns true if there are bytes to read, false if there are no more bytes to read.
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x27);                          //Command to read ALL of the bytes (allocated by setByteRead(x))
    if(waitForResponse("reading data")){      //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true.
        firstByte=USB.read();                 //Read the first byte
        while(USB.available()){
          Serial.write(USB.read());           //Send the data from the USB disk to the Serial monitor
          delay(1);                           //This delay is necessary for successful Serial transmission
        }
    }
    if(!continueRead()){                       //prepares the module for further reading. If false, stop reading.
      break;                                   //You need the continueRead() method if the data to be read from the USB device is greater than numBytes.
    }
  }
  Serial.println();
  Serial.println("NO MORE DATA");
}

//fileWrite=======================================================================================
//are the commands used to write to the file
void fileWrite(String data){
  Serial.println("Writing to file:");
  byte dataLength = (byte) data.length();         // This variable holds the length of the data to be written (in bytes)
  Serial.println(data);
  Serial.print("Data Length:");
  Serial.println(dataLength);
  delay(100);
  // This set of commands tells the CH376S module how many bytes to expect from the Arduino.  (defined by the "dataLength" variable)
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3C);
  USB.write((byte) dataLength);
  USB.write((byte) 0x00);
  if(waitForResponse("setting data Length")){      // Wait for an acknowledgement from the CH376S module before trying to send data to it
    if(getResponseFromUSB()==0x1E){                // 0x1E indicates that the USB device is in write mode.
      USB.write(0x57);
      USB.write(0xAB);
      USB.write(0x2D);
      USB.print(data);                             // write the data to the file
  
      if(waitForResponse("writing data to file")){   // wait for an acknowledgement from the CH376S module
      }
      Serial.print("Write code (normally FF and 14): ");
      Serial.print(USB.read(),HEX);                // code is normally 0xFF
      Serial.print(",");
      USB.write(0x57);
      USB.write(0xAB);
      USB.write(0x3D);                             // This is used to update the file size. Not sure if this is necessary for successful writing.
      if(waitForResponse("updating file size")){   // wait for an acknowledgement from the CH376S module
      }
      Serial.println(USB.read(),HEX);              //code is normally 0x14
    }
  }
}

//appendFile()====================================================================================
//is used to write data to the end of the file, without erasing the contents of the file.
void appendFile(String fileName, String data){
    resetALL();                     //Reset the module
    set_USB_Mode(0x06);             //Set to USB Mode
    diskConnectionStatus();         //Check that communication with the USB device is possible
    USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
    setFileName(fileName);          //Set File name
    fileOpen();                     //Open the file
    filePointer(false);             //filePointer(false) is to set the pointer at the end of the file.  filePointer(true) will set the pointer to the beginning.
    fileWrite(data);                //Write data to the end of the file
    fileClose(0x01);                //Close the file using 0x01 - which means to update the size of the file on close. 
}

//continueRead()==================================================================================
//continue to read the file : I could not get this function to work as intended.
boolean continueRead(){
  boolean readAgain = false;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3B);
  if(waitForResponse("continueRead")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
     if(getResponseFromUSB()==0x14){         //CH376S will send 0x14 if this command was successful
       readAgain=true;
     }
  }
  return(readAgain);


//fileCreate()========================================================================================
//the command sequence to create a file
boolean fileCreate(){
  boolean createdFile = false;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x34);
  if(waitForResponse("creating file")){       //wait for a response from the CH376S. If file has been created successfully, it will return true.
     if(getResponseFromUSB()==0x14){          //CH376S will send 0x14 if this command was successful
       createdFile=true;
     }
  }
  return(createdFile);
}


//fileDelete()========================================================================================
//the command sequence to delete a file
void fileDelete(String fileName){
  setFileName(fileName);
  delay(20);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x35);
  if(waitForResponse("deleting file")){       //wait for a response from the CH376S. If file has been created successfully, it will return true.
     if(getResponseFromUSB()==0x14){          //CH376S will send 0x14 if this command was successful
       Serial.println("Successfully deleted file");
     }
  }
}
  

//filePointer========================================================================================
//is used to set the file pointer position. true for beginning of file, false for the end of the file.
void filePointer(boolean fileBeginning){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x39);
  if(fileBeginning){
    USB.write((byte)0x00);             //beginning of file
    USB.write((byte)0x00);
    USB.write((byte)0x00);
    USB.write((byte)0x00);
  } else {
    USB.write((byte)0xFF);             //end of file
    USB.write((byte)0xFF);
    USB.write((byte)0xFF);
    USB.write((byte)0xFF);
  }
  if(waitForResponse("setting file pointer")){       //wait for a response from the CH376S. 
     if(getResponseFromUSB()==0x14){                 //CH376S will send 0x14 if this command was successful
       Serial.println("Pointer successfully applied");
     }
  }
}


//fileClose=======================================================================================
//closes the file
void fileClose(byte closeCmd){
  Serial.println("Closing file:");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x36);
  USB.write((byte)closeCmd);                                // closeCmd = 0x00 = close without updating file Size, 0x01 = close and update file Size

  if(waitForResponse("closing file")){                      // wait for a response from the CH376S. 
     byte resp = getResponseFromUSB();
     if(resp==0x14){                                        // CH376S will send 0x14 if this command was successful
       Serial.println(">File closed successfully.");
     } else {
       Serial.print(">Failed to close file. Error code:");
       Serial.println(resp, HEX);
     }  
  }
}

//waitForResponse===================================================================================
//is used to wait for a response from USB. Returns true when bytes become available, false if it times out.
boolean waitForResponse(String errorMsg){
  boolean bytesAvailable = true;
  int counter=0;
  while(!USB.available()){     //wait for CH376S to verify command
    delay(1);
    counter++;
    if(counter>timeOut){
      Serial.print("TimeOut waiting for response: Error while: ");
      Serial.println(errorMsg);
      bytesAvailable = false;
      break;
    }
  }
  delay(1);
  return(bytesAvailable);
}

//getResponseFromUSB================================================================================
//is used to get any error codes or messages from the CH376S module (in response to certain commands)
byte getResponseFromUSB(){
  byte response = byte(0x00);
  if (USB.available()){
    response = USB.read();
  }
  return(response);
}



//blinkLED==========================================================================================
//Turn an LED on for 1 second
void blinkLED(){
  digitalWrite(LED, HIGH);
  delay(100);
  digitalWrite(LED,LOW);
}

Trivial FTP su Debian

Puo' servire, specialmente per aggiornare firmware di apparati di rete, dover configurare un server TFtp, una sorta di Ftp con servizi decisamente alleggeriti.
Il server lavora su porta UPD 69 e supporta solo le funzioni di base (download/upload, non e' supportata l'autenticazione utente, non sono supportate le sottodirectory ed il listing della directory ...quindi se non ci conosce il nome del file non si lavora)

Su Debian esistono varie scelte per un server Tftp, in questo caso ho usato quello piu' semplice
Per l'installazione si digita semplicemente (server piu' client)

apt-get install tftp tfpd

al contrario di altri servizi, questo viene gestito direttamente da /etc/inet.conf dalla seguente riga

tftp dgram udp wait nobody /usr/sbin/tcpd /usr/sbin/in.tftpd /srv/tftp
l'ultimo parametro e' la directory in cui saranno contenuti e saranno salvati i file del server Tftp

per lo stop ed il restart non c'e' uno script in /etc/init.d. Si deve riavviare tutto il servizio di rete


giovedì 27 agosto 2015

Battery Pack con Arduino e Raspberry

Ho fatto qualche prova per vedere quanto puo' funzionare una scheda Raspberry od una scheda Arduino alimentandola semplicemente via USB con una batteria esterna

Da un primo semplice confronto si vede che Raspberry ha una richiesta, per la verita' variabile in funzione anche del carico di lavoro, di circa 350 mA
Raspberry
Al contrario Arduino mostra valori sempre inferiori a 10 mA (spesso il display indicava 0.00)

Arduino
In modo molto empirico

Raspberry: alimentando Raspberry con una batteria da 5000 mAh (partendo da carica completa) il sistema e' rimasto acceso 8 ore

Arduino: alimentando una Arduino Nano con una batteria da 2000 mAh il sistema e' rimasto acceso per circa  32 ore (per essere precisi  la batteria non alimentava solo Arduino ma anche un CH376 ed il voltmetro USB che anche loro un po' consumano)

C'e' da fare una osservazione: non tutte le batterie esterne sono risultate idonee ad alimentare Arduino


Da sinistra a destra tre pacchi batteria rispettivamente SBS da 2000 mAh, una cinesata (dovrebbe ricaricarsi mediante il pannello solare ma di fatto non funziona) dichiarato da 5000 mAh ed un da 10000 mAh. Il secondo ed il terzo hanno un pulsante di accensione mentre quello piu' a sinistra basta connetterlo e funziona.
Ho visto che Arduino funziona bene con il primo ed il terzo mentre con la batteria da 5000 mAh, la scheda si accende da dopo circa un secondo si spenge tutto (sia i led sulla scheda che sulla batteria), mi e' venuto il sospetto che non sentendo un carico significativo sulla porta USB il circuito sulla batteria stacchi l'alimentazione; infatti connettendola ad una Raspberry, la cui richiesta di corrente e' decisamente superiore, la batteria funziona regolarmente

martedì 25 agosto 2015

Arduino e CH376S

Questo post e' a seguito di quanto gia' visto nella precedente prova
Lo scopo e' sempre il solito: creare un sistema che permette di scrivere in automatico un file su una chiavetta USB all'inserimento della stessa.
Il sistema era gia' funzionante con Raspberry ma una delle richieste del sistema e' quella di essere alimentato a batteria e la batteria deve durare almeno 48 ore; visto che con Raspberry una batteria da 5000 mA e' durata circa una notte ho modificato il progetto per usare Arduino (nello specifico una Nano) ed il modulo CH376S sperando in consumi minori


Il modulo CH376S e' chip specifico per effettuare scrittura e lettura di file system che puo' essere pilotato da Arduino.
I collegamenti sono banali (4 fili)

Fonte : http://arduinobasics.blogspot.it/2015/05/ch376s-usb-readwrite-module.html

Per questa prova ho modificato il codice ripreso da questo link
Il nucleo del programma e' evidenziato in giallo: prima controllare che il modulo USB sia connesso, dopo controlla che ci sia un dispositivo USB inserito, infine scrive il file

-----------------------------------------------------------------------------------
#include <SoftwareSerial.h>

byte computerByte;           //used to store data coming from the computer
byte USB_Byte;               //used to store data coming from the USB stick
int LED = 13;                //the LED is connected to digital pin 13 
int timeOut = 2000;          //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module.
String wrData = "Lanterna 2000";     //We will write this data to a newly created file.

SoftwareSerial USB(10, 11);                           // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module
                                                      // Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module
                                                      // GND on Arduino to GND on CH376S module
                                                      // 5V on Arduino to 5V on CH376S module
                                                      
int connessione;
int chiave_presente;


//==============================================================================================================================================
void setup() {
  Serial.begin(9600);                                 // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor)
  USB.begin(9600);                                    // Setup serial communication with the CH376S module (using the default baud rate of 9600)
  pinMode(LED,OUTPUT);                                // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED
  digitalWrite(LED,LOW);                              // Turn off the LED
  Serial.println("Iniziato");
}

//================================================================================================================================================
void loop() {
connessione = 0;
chiave_presente = 0;
checkConnection(0x01);
if (connessione == 1)
    {
      Serial.println("Connessione Presente");
      set_USB_Mode(0x06);    
      if (chiave_presente == 1)
        {
          resetALL(); 
          writeFile("lant200.txt", wrData); 
          Serial.println("************FINE*************");
        }
  }
  
if(USB.available()){                                 // This is here to capture any unexpected data transmitted by the CH376S module
    Serial.print("CH376S has just sent this code:");
    Serial.println(USB.read(), HEX);
  }  
delay(5000);
}

//END OF LOOP FUNCTION ========================================================================================================================================


void checkConnection(byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x06);
  USB.write(value);
  
  if(waitForResponse("checking connection")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==(255-value)){
       //Serial.println(">Connection to CH376S was successful.");
       connessione = 1;
    } else {
      //Serial.print(">Connection to CH376S - FAILED.");
      connessione = 0;
    }
  }
}

void set_USB_Mode (byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x15);
  USB.write(value);
  
  delay(20);
  
  if(USB.available()){
    USB_Byte=USB.read();
    //Check to see if the command has been successfully transmitted and acknowledged.
    if(USB_Byte==0x51){                                   // If true - the CH376S has acknowledged the command.
        //Serial.println("set_USB_Mode command acknowledged"); //The CH376S will now check and monitor the USB port
        USB_Byte = USB.read();
        
        //Check to see if the USB stick is connected or not.
        if(USB_Byte==0x15){                               // If true - there is a USB stick connected
          //Serial.println("USB is present");
          chiave_presente = 1;
          blinkLED();                                     // If the process was successful, then turn the LED on for 1 second 
        } else {
          chiave_presente = 0;
          //Serial.print("USB Not present. Error code:");   // If the USB is not connected - it should return an Error code = FFH
          //Serial.print(USB_Byte, HEX);
          //Serial.println("H");
        }
        
    } else {
        //Serial.print("CH3765 error!   Error code:");
        //Serial.print(USB_Byte, HEX);
        //Serial.println("H");
        chiave_presente = 0;
    }   
  }
  delay(20);
}

//resetALL=========================================================================================
//This will perform a hardware reset of the CH376S module - which usually takes about 35 msecs =====
void resetALL(){
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x05);
    //Serial.println("The CH376S module has been reset !");
    delay(200);
}

void writeFile(String fileName, String data){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  if(fileCreate()){               //Try to create a new file. If file creation is successful
    fileWrite(data);              //write data to the file.
  } else {
    Serial.println("File could not be created, or it already exists");
  }
  fileClose(0x01);
}

  
void setFileName(String fileName){
  Serial.print("Setting filename to:");
  Serial.println(fileName);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x2F);
  USB.write(0x2F);         // Every filename must have this byte to indicate the start of the file name.
  USB.print(fileName);     // "fileName" is a variable that holds the name of the file.  eg. TEST.TXT
  USB.write((byte)0x00);   // you need to cast as a byte - otherwise it will not compile.  The null byte indicates the end of the file name.
  delay(20);
}

void diskConnectionStatus(){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x30);

  if(waitForResponse("Connecting to USB disk")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){               //CH376S will send 0x14 if this command was successful
    } else {
    }
  }
}

void USBdiskMount(){
  Serial.println("Mounting USB disk");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x31);

  if(waitForResponse("mounting USB disk")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){               //CH376S will send 0x14 if this command was successful
    } else {
    }
  }
}

void fileOpen(){
  Serial.println("Opening file.");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x32);
  if(waitForResponse("file Open")){                 //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){                 //CH376S will send 0x14 if this command was successful  
    } else {
    }
  }
}

//setByteRead=====================================================================================
//This function is required if you want to read data from the file. 
boolean setByteRead(byte numBytes){
  boolean bytesToRead=false;
  int timeCounter = 0;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3A);
  USB.write((byte)numBytes);   //tells the CH376S how many bytes to read at a time
  USB.write((byte)0x00);
  if(waitForResponse("setByteRead")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x1D){         //read the CH376S message. If equal to 0x1D, data is present, so return true. Will return 0x14 if no data is present.
      bytesToRead=true;
    }
  }
  return(bytesToRead);


//getFileSize()===================================================================================
//writes the file size to the serial Monitor.
int getFileSize(){
  int fileSize=0;
  Serial.println("Getting File Size");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x0C);
  USB.write(0x68);
  delay(100);
  Serial.print("FileSize =");
  if(USB.available()){
    fileSize = fileSize + USB.read();
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255*255);
  }     
  Serial.println(fileSize);
  delay(10);
  return(fileSize);
}


//fileRead========================================================================================
//read the contents of the file
void fileRead(){
  Serial.println("Reading file:");
  byte firstByte = 0x00;                     //Variable to hold the firstByte from every transmission.  Can be used as a checkSum if required.
  byte numBytes = 0x40;                      //The maximum value is 0x40  =  64 bytes

  while(setByteRead(numBytes)){              //This tells the CH376S module how many bytes to read on the next reading step. In this example, we will read 0x10 bytes at a time. Returns true if there are bytes to read, false if there are no more bytes to read.
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x27);                          //Command to read ALL of the bytes (allocated by setByteRead(x))
    if(waitForResponse("reading data")){      //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true.
        firstByte=USB.read();                 //Read the first byte
        while(USB.available()){
          Serial.write(USB.read());           //Send the data from the USB disk to the Serial monitor
          delay(1);                           //This delay is necessary for successful Serial transmission
        }
    }
    if(!continueRead()){                       //prepares the module for further reading. If false, stop reading.
      break;                                   //You need the continueRead() method if the data to be read from the USB device is greater than numBytes.
    }
  }
  Serial.println();
  Serial.println("NO MORE DATA");
}

//fileWrite=======================================================================================
//are the commands used to write to the file
void fileWrite(String data){
  Serial.println("Writing to file:");
  byte dataLength = (byte) data.length();         // This variable holds the length of the data to be written (in bytes)
  Serial.println(data);
  Serial.print("Data Length:");
  Serial.println(dataLength);
  delay(100);
  // This set of commands tells the CH376S module how many bytes to expect from the Arduino.  (defined by the "dataLength" variable)
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3C);
  USB.write((byte) dataLength);
  USB.write((byte) 0x00);
  if(waitForResponse("setting data Length")){      // Wait for an acknowledgement from the CH376S module before trying to send data to it
    if(getResponseFromUSB()==0x1E){                // 0x1E indicates that the USB device is in write mode.
      USB.write(0x57);
      USB.write(0xAB);
      USB.write(0x2D);
      USB.print(data);                             // write the data to the file
  
      if(waitForResponse("writing data to file")){   // wait for an acknowledgement from the CH376S module
      }
      Serial.print("Write code (normally FF and 14): ");
      Serial.print(USB.read(),HEX);                // code is normally 0xFF
      Serial.print(",");
      USB.write(0x57);
      USB.write(0xAB);
      USB.write(0x3D);                             // This is used to update the file size. Not sure if this is necessary for successful writing.
      if(waitForResponse("updating file size")){   // wait for an acknowledgement from the CH376S module
      }
      Serial.println(USB.read(),HEX);              //code is normally 0x14
    }
  }
}

//continueRead()==================================================================================
//continue to read the file : I could not get this function to work as intended.
boolean continueRead(){
  boolean readAgain = false;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3B);
  if(waitForResponse("continueRead")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
     if(getResponseFromUSB()==0x14){         //CH376S will send 0x14 if this command was successful
       readAgain=true;
     }
  }
  return(readAgain);


//fileCreate()========================================================================================
//the command sequence to create a file
boolean fileCreate(){
  boolean createdFile = false;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x34);
  if(waitForResponse("creating file")){       //wait for a response from the CH376S. If file has been created successfully, it will return true.
     if(getResponseFromUSB()==0x14){          //CH376S will send 0x14 if this command was successful
       createdFile=true;
     }
  }
  return(createdFile);
}


//fileDelete()========================================================================================
//the command sequence to delete a file
void fileDelete(String fileName){
  setFileName(fileName);
  delay(20);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x35);
  if(waitForResponse("deleting file")){       //wait for a response from the CH376S. If file has been created successfully, it will return true.
     if(getResponseFromUSB()==0x14){          //CH376S will send 0x14 if this command was successful
       Serial.println("Successfully deleted file");
     }
  }
}
  

//filePointer========================================================================================
//is used to set the file pointer position. true for beginning of file, false for the end of the file.
void filePointer(boolean fileBeginning){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x39);
  if(fileBeginning){
    USB.write((byte)0x00);             //beginning of file
    USB.write((byte)0x00);
    USB.write((byte)0x00);
    USB.write((byte)0x00);
  } else {
    USB.write((byte)0xFF);             //end of file
    USB.write((byte)0xFF);
    USB.write((byte)0xFF);
    USB.write((byte)0xFF);
  }
  if(waitForResponse("setting file pointer")){       //wait for a response from the CH376S. 
     if(getResponseFromUSB()==0x14){                 //CH376S will send 0x14 if this command was successful
       Serial.println("Pointer successfully applied");
     }
  }
}


//fileClose=======================================================================================
//closes the file
void fileClose(byte closeCmd){
  Serial.println("Closing file:");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x36);
  USB.write((byte)closeCmd);                                // closeCmd = 0x00 = close without updating file Size, 0x01 = close and update file Size

  if(waitForResponse("closing file")){                      // wait for a response from the CH376S. 
     byte resp = getResponseFromUSB();
     if(resp==0x14){                                        // CH376S will send 0x14 if this command was successful
       Serial.println(">File closed successfully.");
     } else {
       Serial.print(">Failed to close file. Error code:");
       Serial.println(resp, HEX);
     }  
  }
}

//waitForResponse===================================================================================
//is used to wait for a response from USB. Returns true when bytes become available, false if it times out.
boolean waitForResponse(String errorMsg){
  boolean bytesAvailable = true;
  int counter=0;
  while(!USB.available()){     //wait for CH376S to verify command
    delay(1);
    counter++;
    if(counter>timeOut){
      Serial.print("TimeOut waiting for response: Error while: ");
      Serial.println(errorMsg);
      bytesAvailable = false;
      break;
    }
  }
  delay(1);
  return(bytesAvailable);
}

//getResponseFromUSB================================================================================
//is used to get any error codes or messages from the CH376S module (in response to certain commands)
byte getResponseFromUSB(){
  byte response = byte(0x00);
  if (USB.available()){
    response = USB.read();
  }
  return(response);
}



//blinkLED==========================================================================================
//Turn an LED on for 1 second
void blinkLED(){
  digitalWrite(LED, HIGH);
  delay(100);
  digitalWrite(LED,LOW);
}
-----------------------------------------------------------------------------------

lunedì 24 agosto 2015

Tempo di risposta App Store

Un breve riassunto sulla mia esperienza sulla pubblicazioni di applicazioni sui vari Store 



Google Play
Il costo della licenza era (circa 4 anni fa) di 18 euro e non ha scadenza temporale. Il sistema di pubblicazione e' di tipo sostanzialmente automatico e passano poche ore dall'upload dell'apk alla presenza sul Google Play. L'applicazione risulta presente nel motore di ricerca nelle 24 ore successive

Apple Store
Il costo della licenza e' aumentata a 100 euro l'anno. In generale dal momento dell'upload dell'applicazione passano 6 giorni prima che venga presa in carico dal sistema di validazione (rigorosamente umana). Se tutto va bene il programma viene esaminato nelle 24 ore successive ed entra nel motore di ricerca nelle 48 ore successive. C'e' da dire che a seconda della persona che esamina l'applicazione ci possono essere i piu' svariati metri di misura (per esempio ho aspettato due mesi prima di pubblicare una applicazione quando, una volta cambiato esaminatore, e' stata immediatamente accettata)

Windows Phone
Il costo della licenza da sviluppatore e' di circa 15 euro e la durata e' di un anno. La pubblicazione e' sottoposta a validazione (non e' chiaro se umana od automatica) che e' giunta il giorno successivo alla richiesta di pubblicazione. L'applicazione e' entrata nel motore di ricerca dello store nelle 24 ore successive

Firefox MarketPlace
Non ci sono costi per la licenza di sviluppatore. La pubblicazione e' particolarmente lenta (l'esame e' di tipo umano e normalmente fatto da volontari) , la mia ultima applicazione e' stata sottoposta a validazione il 29 luglio ed e' stata pubblicata il 22 agosto 

Amazon Store
Non pervenuto. Ho provato una sola volta a pubblicare su questo store. Il sistema di compilazione del form mi indica un errore sulla presenza di una traduzione non accettata. Non riuscendo a capire quale fosse il problema ho aperto un ticket verso l'assistenza tecnica che non ha mai avuto risposta. Ho lasciato perdere

venerdì 21 agosto 2015

Arduino Nano e usb2.0-serial

Nella foto ci sono i due cloni di Arduino Nano che ho nella mia collezione.
La prima a sinistra, l'ho comprata qualche tempo fa e non mi mai dato problemi.
Quella a destra invece e' un recentissimo acquisto e quando lo ho connessa ad Arduino IDE (sotto Windows e Mac) ho avuto la spiacevole sorpresa che non era presente la porta seriale e su Windows era richiesto il driver usb2.0-serial (i driver Arduino era gia' correttamente installati)



La soluzione risiede nell'installare i pacchetti scaricabili a questo indirizzo
http://www.arduined.eu/ch340g-converter-windows-7-driver-download/
perche' sui cloni Arduino recenti viene montato un convertitore CH340G USB/Serial al posto del normale FTDI

giovedì 20 agosto 2015

Midi over Usb con Arduino

Arduino non puo' diventare un vero e proprio dispositivo Midi a meno che non si faccia una modifica al firmware (nel qual caso poi non diventi poi piu' programmabile) ma puo' comunque inviare stringhe MIDI via seriale che sono poi convertite, via software, da programmi Serial->Midi.

Questa prova e' stata fatta su Mac ma in linea di principio e' ripetibile anche su Windows (con altri software) ma io non ci sono riuscito





(da importare la libreria Arduino Midi Library, codice modificato dal primo esempio)
-------------------------------------------------
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

#define LED 13      

void setup()
{
    pinMode(LED, OUTPUT);
    MIDI.begin(1);         
    Serial.begin(115200);
}

void loop()
{
        digitalWrite(LED,HIGH);
        MIDI.sendNoteOn(42,127,1);  // Send a Note (pitch 42, velo 127 on channel 1)
        delay(1000);        // Wait for a second
        MIDI.sendNoteOff(42,0,1);   // Stop the note
        digitalWrite(LED,LOW);
}
-------------------------------------------------

Poi si va in Applicazioni/Utility e si carica Configurazioni Midi Audio, Mostra Studio Midi Driver IAC e si attiva il driver

Per la conversione del segnale seriale derivante da Arduino in segnale Midi si puo' usare Hairless-MidiSerial. Il funzionamento e' quanto mai semplice. A sinistra si imposta la porta seriale dell'Arduino e sulla destra gli input ed output Midi (nel mio caso era solo un input in quanto Arduino spedisce solo dati)
Come destinatario del segnale Midi, non avendo altro a disposizione, ho provato Virtual Midi Piano Keyboard
Tramite questa catena e' possibile far suonare ad Arduino il pianoforte Midi




E' ovviamente noioso ma basta inserire un po' di pulsanti e riprogrammare Arduino per rendere tutto piu' interattivo

Samba su Centos 7

Per installare Samba su Centos 7 ho seguito questi passi (la macchina ha Selinux disattivato)

yum install samba samba-client samba-common

ed in seguito

systemctl enable smb.service
systemctl enable nmb.service
systemctl restart smb.service
systemctl restart nmb.service

ad un primo tentativo il servizio nmd non veniva tirato su, ho risolto facendo uno yum update (e' quindi consigliabile farlo da subito prima dell'installazione dei pacchetti)

dato che il server su cui lo ho installato ha due interfacce (una con ip pubblico ed una con ip privato)  ho modificato il file /etc/samba/smb.conf per mettere in ascolto solo sull'interfaccia interna modificando

[global] 
interfaces = 10.1.1.16 lo 
bind interfaces only = yes

per verificare che tutto sia andando a buon fine si puo' digitare netstat -tapn | grep smbd

Rimane da aprire la porta del firewall 

firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd reload

visto che era richiesto l'accesso autorizzato ad una cartella condivisa e' stato creato un utente samba generico che permette di scrivere sulla directory del web server

smbpasswd -a www_user


il file smb.conf finale e' di questo tipo
---------------------------------------------------
[global]
workgroup = WORKGROUP
server string = Samba Server %v
netbios name = centos
security = user
map to guest = bad user
dns proxy = no
interfaces = 10.1.1.16 lo
bind interfaces only = yes
#============= Share Definitions ==============
[www]
path = /var/www/html
valid users = www_user
read only = No
create mask = 0777
directory mask = 0777

mercoledì 19 agosto 2015

Errore collect2.exe: error: ld returned 5 Arduino IDE



Su Arduino IDE 1.6.5 per Windows XP (non ho provato sulle altre piattaforme) in fase di compilazione di un codice leggittimo (che si compilava in precedenza) viene generato il seguente errore

collect2.exe: error: ld returned 5 exit status
Errore durante la compilazione

dopo un po' di prove ho scoperto che la riga incriminata che genera l'errore e' relativa all'istruzione delay (commentata la riga tutto funziona). Non ho trovato soluzione se non installare una versione piu' vecchia dell'IDE

martedì 18 agosto 2015

NFC Mifare e Nexus

Un paio di anni fa mi ero interessato all'uso degli Rfid su Android per un progetto che non e' mai partito ed allo scopo avevo comprato un centinaio di tag solo leggibili ed un tag riscrivibile in formato Mifare Classic 1K (con una memoria di 1024 byte, un po' costoso all'epoca devo ammettere)

Le prove le avevo eseguite con il Nexus S che era all'epoca il telefono di sviluppo


A sinistra Nexus 4, al centro Mifare Classic ed a destra Nexus S
Ho ripreso in mano il progetto per un nuovo possibile utilizzo stavolta utilizzando il Nexus 4.
Con mia sorpresa ho scoperto che il sensore di N4 non ha compatiblita' con il formato Mifare Classic 1K ed ho dovuto ordinare dei nuovi tag Ntag 203 (che dovrebbero essere universali)

NFC e' sempre rimasto un aspetto marginale di Android e di sicuro continuera' ad esserlo se non vengono definiti degli standard stabili nel tempo

lunedì 17 agosto 2015

Salvare pagine html on line per rassegna stampa

Uno dei problemi che si incontrano quando si cercano di salvare pagine html sui vari siti  (per esempio per creare una rassegna stampa) e' che la qualita' del risultato e' sempre scarsina, qualunque sia il formato di salvataggio

Per esempio partiamo da uno screenshot di una notizia sulla recensione della mia applicazione Abetone Trail Park


se si salva il contenuto in pdf mediante Chrome il risultato e' questo


Il tutto e' ovviamente non impaginato
Ho trovato utile usare il software Paparazzi, che riporta una impaginazione decisamente fedele del sito desiderato




venerdì 14 agosto 2015

Musescore e Songbook


Desktop
La versione desktop del programma puo' essere scaricata all'indirizzo https://musescore.org/it,e' opensource e multipiattaforma con sorgenti disponibili



Creando un nuovo spartito (File/Nuovo) viene chiesto il titolo, l'autore, lo o gli strumenti da usare, l'armatura di chiave,l'unita' di tempo (3/4, 4/4 per esempio) ed il numero di battute

Per inserire le note (modalita' di edit) si preme il tasto N oppure l'icona
Le note possono essere inserite dopo aver cliccato nella barra seguente indicando la durata direttamente posizionandole sul pentragramma

per inserire una pausa si entra in modalita' edit, si seleziona la lunghezza della nota e poi si clicca 


Attenzione : il programma in corso d'opera cerchera' di far sempre tornare la lunghezza della battuta. Per esempio se si ha una battuta in 4/4 e si e' aggiunta nota (per esempio una minima 1/2) aggiustera' il resto della battuta con una pausa di minima (1/2)

Per aggiungere elementi quali Articolazioni ed Abbellimenti (come staccati, crescenti ed altro) si deve uscire dalla modalita' edit (N) e si trascina l'elemento sulla nota interessata (cosi' come per le alterazioni non in chiave)

In automatico Musescore tende a raggruppare le note (per esempio di solito raggruppa le crome a  due a due) . Se non si e' soddisfatti si puo' usare in Tavolozze i comandi Proprieta' Gruppo note. Per raggruppare per esempio di clicca la seconda icona da sinistra nella file in alto e si trascina sulla seconda nota tra quelle che si devono raggruppare




E' possibile effettuare la trasposizione automatica della tonalita' utilizzando il menu' Note/Trasporto
Per esempio dalla tonalita' originale in Do maggiore



a Sol Maggiore



Una volta completato lo spartito (od anche in corso d'opera) si puo' ascoltare il brano con i tasti
(il play corrisponde al tasto spazio sulla tastiera)
Musescore viene distribuito con una libreria di suoni midi. Se si vuole un suono migliore si puo' scaricare una libreria SF2 piu' avanzata come quella per esempio FluidR3 GM2-2.SF2 (144 Mb!!)  reperibile qui e qui
Per installare la nuova libreria si devono aggiornare le Preferenze



ed in seguito Visualizza/Sintetizzatore/Aggiungi


(Curiosamente sono costretto ad impostare i suoni personalizzati ogni volta che apro il programma perche' si perdono le impostazioni)


Songbook
L'applicazione Musescore per Android ed IOS e' solo un player e visualizzatore dei file .mscz. Ne esistono due versioni: Musescore e' un programma che non permette di visualizzarea gli spartiti salvati sul proprio dispositivo portatile (ha quindi la necessita' del collegamento di rete per scaricare i dati dal sito di Musescore dopo aver fatto il login) e Songbook che permette di usare i propri file mscz.





Su Songbook non esiste la possibilta' di avere un file manager. Per aggiungere i propri spartiti si deve

  • su Android con un file manager si clicca sul .mscz e si indica "Apri con" Musescore Songbook
  • su IOS non e' possibile importare i dati mediante ITunes. Il sistema piu' semplice e' allegare i file degli spartiti anche una mail e poi "Apri con" l'allegato verso Musescore Songbook
Un paio di problemi: 
  • Solo su IOS l'applicazione portatile permette solo la modalita' portrait. Se si ruota lo schermo lo spartito non viene ridimensionato (su Android e' tutto a posto)
  • Esistono solo il tasto Play ed il tasto Ritorna all'inizio. Su brani lunghi questa e' una notevole limitazione ed esiste un trucco non immediato. Si deve premere sulla nota da cui si vuole partire in modo prolungato fino a che non viene emesso il suono della nota. A questo punto si puo' premere Play e l'esecuzione inizia dal punto prescelto


giovedì 13 agosto 2015

OpenCL e Mandelbrot

Il calcolo dell'insieme di Mandelbrot e' forse uno dei casi piu' semplici in cui applicare il calcolo parallelo perche' l'elaborazione di  ogni cella e' indipendente da quelle vicine.

Ai giorni d'oggi per effettuare il calcolo parallelo, invece di usare un cluster di computer, si possono usare i diversi core del processore oppure usare la scheda grafica con la sua GPU (mediante le librerie CUDA di NVidia o le piu' generiche OpenCL che possono girare su AMD, NVidia e Intel)



La strategia utilizzata in CUDA e OpenCL e' piu' o meno la stessa ovvero si scrive il codice di un kernel, ovvero della elaborazione che verra' eseguita su ogni unita' di elaborazione, e poi il codice che gestisce la coda dei processi e la distribuzione alle varie unita' di elaborazione

Un esempio di kernel per Mandelbrot e' il seguente
-------------------------------------------------------------------
#pragma OPENCL EXTENSION cl_khr_fp64 : enable

double trans_x(int x, int N)
{
    return 3.0 * ((double)x / N) - 2.0;
}
double trans_y(int y, int N)
{
    return 3.0 * ((double)y / N) - 1.5;
}
double mag2(double r, double i)
{
    return r * r + i * i;
}
__kernel void mandel(__global double* out, int N, int depth, double escape2)
{
    size_t idx = get_global_id(0);

    double z0_r = trans_x(idx % N, N);
    double z0_i = trans_y(idx / N, N);

    double z_r = 0;
    double z_i = 0;
    int k = 0;
    for(; k <= depth && mag2(z_r, z_i) < escape2 ; ++k)
    {
        double t_r = z_r; double t_i = z_i;
        z_r = t_r * t_r - t_i * t_i + z0_r;
        z_i = 2 * t_r * t_i + z0_i;
    }
    out[idx] = log(k + 1.0 - log(log(max(mag2(z_r, z_i), escape2)) / 2.0) / log(2.0));
}

-------------------------------------------------------------------


Per mettere alla prova quanto vantaggio c'e' nell'usare OpenCL per il calcolo di Mandelbrot ho usato un portatile Surface Pro 2 con scheda video Intel HD 4400 ed il programma reperibile a questo link.

Come test ho provato il  Miniset level 2 (1000 iterazioni per punto) con i seguenti risultati

- C float, single thread :1.71 s
- C float, 4 thread : 0.61 s
- C float, 8 thread : 0.59 s
- C fixed-point 128 bit  2 thread : 22.5 s
- OpenCl CPU float  : 0.36 s
- OpenCL CPU  double : 0.4 s
- OpenCL GPU float : 0.096 s



mercoledì 12 agosto 2015

Hyper-V e Virtualbox

Da quando ho installato l'SDK di Windows Phone (e l'annesso emulatore) ho avuto la sgradita sorpresa che Virtualbox (cosi' come WMWare) non risultano piu' funzionanti perche' e' possibile avere in esecuzione un solo hypervisor per volta...peccato che a me serva anche una Linux box anche se virtualizzata

La soluzione del problema e' questa (copiata da qui):

1) Si clicca  Tasto Windows+X e si apre una finestra di shell come amministratore

2) Si digita bcdedit /copy {current} /d "No Hyper-V".Questo comando riporta come risposta una stringa tra parentesi graffe. Il testo tra le graffe deve essere copiato perche' sara' utile per il comando successivo

3) Si digita bcdedit /set {inserire_qui_codice_copiato} hypervisorlaunchtype off

4) A questo punto si effettua il logout e si, tenendo premuto il tasto SHIFT, si clicca con il mouse per riavviare il sistema

5) Al reboot sara' presente una nuova opzione (non di default) di boot No Hyper-V che dovra' essere usata se si vuole successivamente impiegare Virtualbox od altro software di virtualizzazione


lunedì 10 agosto 2015

Abetone Trail Park sulla stampa

Abetone Trail Park e' la mia ultima applicazione ed e' riuscita a ritagliarsi anche un suo piccolo spazio sulla stampa locale

La Nazione ed. Pistoia 8 Agosto 2015

Il Tirreno ed. Pistoia 6 Agosto 2015

Sviluppata in PhoneGap ha come aspetto tecnico piu' significativo quello di avere una base cartografica per escursionismo completamente integrata senza la necessita' di scaricarla dalla rete (in montagna la connessione dati non e' assolutamente garantita)..un altro aspetto interessante e' che i 625 Kmq di cartografia, originariamente per circa 90Mb, sono riusciti a rientrare in 32 Mb (comprimendo i dati di base ed eliminando i livelli di zoom non di interesse)




In attesa del MarketPlace di Firefox (un po' lentino ad approvare le applicazioni) e di Amazon Store questi sono i link per scaricare l'applicazione

https://play.google.com/store/apps/details?id=com.lucainnocenti.abetonetrailpark&hl=it

https://itunes.apple.com/us/app/abetone-trail-park/id1014602667?mt=8

https://www.microsoft.com/en-us/store/apps/abetone-trail-park/9nblggh1rdq7


LLama3 Anita

A seguito di questo post ho provato a vedere ho provato a vedere cosa accadeva ad utilizzare un modello specifico per la lingua italiana in...